0

I want to make value data from usercontrol will be save on database where the triggers is to click save button from other form. It should be a form that contain button save and usercontrol will be drag/put to that form.

My code us running without error, but the result in the database is not right.

Note: my code is running perfectly when I place save button on usercontrol but when I put save button on another form the result like on the image.

Code on form:

namespace REMAS.Forms.Account
{
    public partial class frmCreateFixedAssetCategories : Form
    {
        SqlConnection conn = new SqlConnection("Server=localhost;Database=TESTCRUD;User Id=sa;Password=123456");
        SqlCommand cmd;
        SqlDataAdapter adapter;
        SqlDataReader reader;
        DataTable dt;
        string sql, sAction;
        int iID;
        private uctInformation uctInformationInstance;
        public frmCreateFixedAssetCategories()
        {
            InitializeComponent();
            uctInformationInstance = new uctInformation();
            // Assuming you have a panel or some container to host the UserControl
          

        }

        private void btnSaveActions_Click(object sender, EventArgs e)
        {
            uctInformationInstance.savedata();
        }
    }
}

code on usercontrol:

namespace REMAS.Forms.Purchase
{
    public partial class uctInformation : UserControl
    {
        SqlConnection conn = new SqlConnection("Server=localhost;Database=TESTCRUD;User Id=sa;Password=123456");
        SqlCommand cmd;
        SqlDataAdapter adapter;
        SqlDataReader reader;
        DataTable dt;
        string sql, sAction;
        int iID;
        public uctInformation()
        {
            InitializeComponent();
        }

        private void uctInformation_Load(object sender, EventArgs e)
        {
            ClearText();
        }

        void ClearText()
        {
            txbAddresses.Clear();
            txbFixedPhone.Clear();
            txbFaxuctInf.Clear();
            txbMobileuctInf.Clear();
            txbEmailuctInf.Clear();
            txbWebuctInf.Clear();
            txbRegCode.Clear();
            txbTaxN.Clear();
            txbLanguageuctInf.Clear();
            txbAccOwner.Clear();
            txbTeamuctInf.Clear();
            cmbCategoryuctInf.Items.Clear();
            cmbIndustryuctInf.Items.Clear();
            cmbSourceuctInf.Items.Clear();
            cmbDeptuctInf.Items.Clear();
            cmbCompaniesAssoci.Items.Clear();
            nudTurnoveructInf.Value = 0;
            nudEmployeesuctInf.Value = 0;
            nudPaymentuctInf.Value = 0;
            rtbNotes.Clear();
        }

        private void txbFixedPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back;

        }

        private void txbFaxuctInf_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back;

        }

        private void txbMobileuctInf_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && e.KeyChar != (char)Keys.Back;

        }

        public void savedata()
        {
            
                try
                {
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    sql = "sp_InsertInformation";
                    cmd = new SqlCommand(sql, conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@addresses", txbAddresses.Texts.ToString());
                    cmd.Parameters.AddWithValue("@fixedphone", txbFixedPhone.Texts.ToString());
                    cmd.Parameters.AddWithValue("@fax", txbFaxuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@mobilephone", txbMobileuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@email", txbEmailuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@website", txbWebuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@category", cmbCategoryuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@industrisector", cmbIndustryuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@turnover", nudTurnoveructInf.Text.ToString());
                    cmd.Parameters.AddWithValue("@employee", nudEmployeesuctInf.Text.ToString());
                    cmd.Parameters.AddWithValue("@source", cmbSourceuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@deptdiv", cmbDeptuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@regcode", txbRegCode.Texts.ToString());
                    cmd.Parameters.AddWithValue("@taxn", txbTaxN.Texts.ToString());
                    cmd.Parameters.AddWithValue("@paymentdelay", nudPaymentuctInf.Text.ToString());
                    cmd.Parameters.AddWithValue("@lang", txbLanguageuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@accowner", txbAccOwner.Texts.ToString());
                    cmd.Parameters.AddWithValue("@team", txbTeamuctInf.Texts.ToString());
                    cmd.Parameters.AddWithValue("@companyassoci", cmbCompaniesAssoci.Texts.ToString());
                    cmd.Parameters.AddWithValue("@note", rtbNotes.Text.ToString());


                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Insert data successfully", "Information");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Information");
                    conn.Close();
                    return;
                }
            
        }


    }
}

Result from SSMS:

enter image description here

Design from Visual Studio:

enter image description here

5
  • 2
    Please use your eyes to look at the preview of your question and don't submit it if the formatting is a mess. It's your responsibility to make your question as readable as possible to help us help you. If your code is too hard to read, many people will just not bother helping, so you lose out. Commented May 20, 2024 at 10:04
  • @jmcilhinney hi ty for replying, can u highlight it please? Its my first time to ask a question, and english is not my first language. Any advice is truly help me, or perhaps i could take down/delete this question, thank you? Commented May 20, 2024 at 10:19
  • Look at the original revision of your post, @Yud , notice, for example, that you have a sentence (not code) that starts with "namespace REMAS.Forms.Account { public partial". This is why you are asked to "format them [your code, images, data] appropriately" when posting a question. Commented May 20, 2024 at 10:45
  • @thoma ahh i see, sorry for making mistakes, i didn't mean too and thanks for the reference i will take a notes, thank you very much Commented May 20, 2024 at 11:04
  • Slight detour. The sp_ prefix a poor naming choice. sqlperformance.com/2012/10/t-sql-queries/sp_prefix Also...dbdelta.com/addwithvalue-is-evil Commented May 20, 2024 at 15:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.