I am trying to insert value from combo box to database !! but this is not working

Posted by user3675488 on Stack Overflow See other posts from Stack Overflow or by user3675488
Published on 2014-05-26T08:31:17Z Indexed on 2014/06/07 9:25 UTC
Read the original article Hit count: 278

Filed under:

At first I have taken some string in which I am trying to save the input values of textboxs and there is a combo box, I am trying to save the input value of the combo box to database, but this following code is unable to do that !!! what is the prob please help me.

private void OK_Click(object sender, EventArgs e)

    {
        string sTxtcmpnyName = "";
        string sTxtcmpnyAdd = "";
        string sPhoneno = "";
        string sTxtFax = "";
        string sPanNo = "";
        string sTxtTin = "";
        string sTax = "";
        string sAccYr = "";
        string sComboSt = "";



        foreach (Control c in this.Controls) ////*Adding Validation to the textbox.*//   // Here the control is entered into GroupBox Grpcmp where  c is denoting the name of the control into the groupbox.



        {
            // c1 is another control which denotes the textboxes under the GroupBox Grpcmp.

            foreach (Control c1 in c.Controls)
            {
                /////Now this following code snippets reperesents that the name of the company should not be blank.


                if (c1 is TextBox == true)   // simpler that what you've done there
                {
                    TextBox temp = (TextBox)c1;

                    //The control is entering into Txtcompany.
                    if (temp.Name == "Txtcompanyname")
                    {
                        //Condition checking is the TextBox is empty or Null then the following message will be shown.

                        if ((temp.Text == "") || (temp.Text == "NULL"))
                        {
                            MessageBox.Show("Company Name should not be Blank");
                        }

                        sTxtcmpnyName = temp.Text;
                    }


                    else if (c1.Name == "TxtcompanyAddress")
                    {
                        sTxtcmpnyAdd = c1.Text;

                    }


                    else if (c1.Name == "Txtphoneno")
                    {
                        sPhoneno = c1.Text;

                    }
                    else if (c1.Name == "TxtFax ")
                    {
                        sTxtFax = c1.Text;

                    }
                    else if (c1.Name == "Txtpanno")
                    {
                        sPanNo = c1.Text;
                    }
                    else if (c1.Name == "TxtTin")
                    {
                        sTxtTin = c1.Text;
                    }
                    else if (c1.Name == "Txtservicetax")
                    {
                        sTax = c1.Text;
                    }
                    //Now I am converting the TxtAcYr into Date format. 
                    //For this purpose two conditions are checked first.
                    //First If the TextBox TxtAcYr is Null or empty it will show the message to enter the accountyear!!
                    //Second If the length of the TextBox TxtAcYr is less than 10, it will again generate a message The date format should be in DD/MM/YYYY 
                    // Then the value of the use input will be picked using a For loop.

                    if (c1.Name == "TxtAcYr")
                    {
                        sAccYr = c1.Text;
                        //Here a string is taken named as yearlength and the value of the TxtAcYr is assigned to it by using control c1.

                        //Condition Checking If the TextBox TxtAcYr is Null or empty it will show the message to enter the accountyear!!

                        if ((c1.Text == "") || (c1.Text == "NULL"))
                        {

                            MessageBox.Show("Account Year should be entered!!");
                        }

                            //Condition 2 is checking. Here the length of the string yearlength is whether equals to 10 or not is checked.
                        //Because there are total 10 characters in Date Format along with special character.

                        else
                            // MessageBox.Show(yearlength.Length.ToString());

                            if (sAccYr.Length != 10)
                            {
                                MessageBox.Show("The Data Format DD-MM-YYYY");
                            }
                            //Now the value of user will be picked by using the code snippets.

                            else
                            {
                                //A string named as JK is taken for further use.

                                String JK = "";

                                //This following loop is initiated to pick the user input.
                                //The loop will check wheather the value of i is less than the length of string yearlength or not.
                                //If Yes then it will go further.

                                for (int i = 0; i < sAccYr.Length; i++)
                                {

                                    //This condition is checking special characters.
                                    //The positions of special characters(Here '-') are placed at 2nd and 5th numbers.  
                                    //So, the value of i can not be equals to 2 && 5.

                                    if ((i != 2) && (i != 5))
                                    {
                                        //The new of value of year length i is assinged to the variable JK.

                                        JK = JK + sAccYr[i];
                                    }

                                    //If the value of i is equals to 1, then enter the following.

                                    if (i == 1)
                                    {
                                        //*Should add the function of TOInt32*
                                        // If ToInt32(JK)>= the maximum length of days of a month then the following alert message will be shown.

                                        if (Convert.ToInt32(JK) >= 32)
                                        {
                                            MessageBox.Show("The Data Format DD-MM-YYYY");
                                        }
                                        //**Comment should be added.**

                                        JK = "";

                                    }
                                    else
                                        //If the value of i is equals to 4, then enter the following.
                                        if (i == 4)
                                        {
                                            //*Should add the function of TOInt32*
                                            // If ToInt32(JK)>= the maximum length of month then the following alert message will be shown.

                                            if (Convert.ToInt32(JK) >= 13)
                                            {
                                                MessageBox.Show("The Data Format DD-MM-YYYY");
                                            }
                                            JK = "";

                                        }
                                }
                            }
                    }
                }

                else if (c1.Name == "state_cmb")
                {
                    //sTxttate = c1.Text.ToString();
                    sComboSt = c1.Text;
                    MessageBox.Show(c1.Text);

                } 


            }
        }
              //////DATABASE CONNECTION/////
        try
        {
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            conn.ConnectionString = ("Data Source =192.168.0.2 ;database= Mee_Company; Persist Security Info =true; User ID =sa;Password = soso654321@");
            conn.Open();

            cmd.CommandText = ("INSERT INTO CompanyMaster(CompanyName,Address,State,Phone,Fax,PAN,TIN,STAX,AccountsYear)values('" + sTxtcmpnyName + "','" + sTxtcmpnyAdd + "','" + sComboSt + "','" + sPhoneno + "','" + sTxtFax + "','" + sPanNo + "','" + sTxtTin + "','" + sTax + "','" + sAccYr + "')");
            //('" + sTxtcmpnyName + "', '" + TxtcompanyAddress.Text + "', '" + Txtphoneno.Text + "', '" + TxtFax.Text + "', '" + Txtservicetax.Text + "','" + TxtAcYr.Text + "')");

            cmd.Connection = conn;
            cmd.ExecuteNonQuery();
            conn.Close();
            //cmd.Parameters.AddWithValue

        }

        catch (Exception ee)
        {

            MessageBox.Show(ee.ToString());

        }

    }



    //An event is created here so that when the user will click on the Cancel Button, the Form will be closed.

    private void BtmCancle_Click(object sender, EventArgs e)
    {
        //this means the form.
        this.Close();
    }


    //Another Event is created here named as TxtAcYr_KeyPress.
    //It is for making the TextBox TxtAcYr only allowance of numeric input along with special character '-'.

    private void TxtAcYr_KeyPress(object sender, KeyPressEventArgs e)
    {
        //If the input is number or '-' is checked 
        //And also the backspace and delete option is enabled here.

        if (char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete || e.KeyChar == '-')
        {
            e.Handled = false; //ok
        }
        else
        {
            e.Handled = true; //not ok
        }
    }

© Stack Overflow or respective owner

Related posts about c#