Click here to Skip to main content
15,886,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int a;
        private void button2_Click(object sender, EventArgs e)
        {
            //comboBox1.Items.Add(comboBox1.Text);
            string val = textBox10.Text.ToString();
            if (val == "")
            {
                textBox10.Text = "1";
            }
            else
            {
                
                a = Convert.ToInt32(textBox10.Text.ToString());
                a = a + 1;
                textBox10.Text = a.ToString();
                //}
            }
            string cmdtext = "insert into ITEM(ITEM_ID,ITEM_INNER_D,ITEM_OUTER_D,ITEM_NAME,ITEM_UNIT,ITEM_PRI_MEAS_UNIT,ITEM_SEC_MEAS_UNIT,ITEM_TYPE,ITEM_COLOR,ITEM_WR,ITEM_RAW,ITEM_FINISH,ITEM_MATERIAL,ITEM_IG_ID) values('" + textBox10.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox7.Text + "','" + textBox6.Text + "','" + comboBox4.SelectedItem + "','" + comboBox1.SelectedItem + "','" + checkBox1.Checked + "','" + textBox8.Text + "','" + textBox9.Text + "','" + comboBox3.SelectedItem + "','" + textBox5.Text + "')";
            //string cmdtext = "insert into ACCOUNT(ACC_ID,ACC_NAME,ACC_TYPE,ACC_ADD1) VALUES ('" + textBox17.Text + "','" + textBox1.Text + "','" + comboBox1.SelectedItem + "','" + richTextBox1.Text + "')";
            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=TWPLSOFTWARE;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(cmdtext, con);
            try
            {
                //if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "" && textBox8.Text != "" && textBox9.Text != "" && textBox10.Text != "" && textBox11.Text != "")
                if (textBox1.Text == "")
                {
                    MessageBox.Show("PLEASE FILL ALL ENTRIES");
                    

                }
                
                else
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    MessageBox.Show(" Data Sucessfully Inserted");
                    textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";textBox4.Text = ""; textBox5.Text = "";textBox6.Text = "";textBox7.Text = "";textBox8.Text = "";textBox9.Text = "";comboBox1.text="";comboBox3.text="";comboBox4.text="";
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 14-Jan-13 21:32pm
v2
Comments
[no name] 15-Jan-13 3:36am    
What error ?

1 solution

First off, when you report a problem, put the actual question in the body, not teh subject - the subject gets truncated and we can't see what most of your problem is!

Secondly, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Thirdly, Your code won't even compile like that: there is no "text" property of a combobox as C# is case sensitive:
textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = "";textBox4.Text = ""; textBox5.Text = "";textBox6.Text = "";textBox7.Text = "";textBox8.Text = "";textBox9.Text = "";comboBox1.text="";comboBox3.text="";comboBox4.text="";


Fourthly, try making your code a little cleaner: Move all those clearing operations into a separate method:
C#
private void ClearInputs()
    {
    textBox1.Text = "";
    textBox2.Text = "";
    textBox3.Text = "";
    textBox4.Text = "";
    textBox5.Text = "";
    textBox6.Text = "";
    textBox7.Text = "";
    textBox8.Text = "";
    textBox9.Text = "";
    comboBox1.Text = "";
    comboBox3.Text = "";
    comboBox4.Text = "";
    }
And then call that wherever you need to clear the inputs.

Fifthly, stop using the VS default names for controls! You may remember today that textBox8 holds the invoice detail, but when you look at this code in two weeks you won't, and it will make things a lot more awkward. Call it tbInvoiceDetail or something else which relates to it's function and your code becomes a lot clearer, easier to read, and more reliable (since you can tell more easily that you are using the right data)
 
Share this answer
 
Comments
arvindnitin7 15-Jan-13 3:48am    
how this function will work? b'coz there is no function calling
arvindnitin7 15-Jan-13 3:51am    
ok mr. griff i'll follow ur all steps bt tell me how can i salve my problem.
OriginalGriff 15-Jan-13 3:56am    
When you have followed them, try it again. If you have the same problem, then post exactly the code you tested here (cut'n'paste) so I can see it. Don't post code that won't compile because you may have missed out important details in the code that runs but doesn't work!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900