Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 14-Jan-13 22:30pm
Edited 14-Jan-13 22:32pm
v2
Comments
Krunal Rohit at 15-Jan-13 3:36am
   
What error ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
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)
  Permalink  
Comments
arvindnitin7 at 15-Jan-13 3:48am
   
how this function will work? b'coz there is no function calling
arvindnitin7 at 15-Jan-13 3:51am
   
ok mr. griff i'll follow ur all steps bt tell me how can i salve my problem.
OriginalGriff at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 382
1 DamithSL 275
2 OriginalGriff 245
3 Zoltán Zörgő 149
4 Dave Kreskowiak 110
0 OriginalGriff 7,575
1 DamithSL 5,529
2 Sergey Alexandrovich Kryukov 5,279
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100