Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my button code and i am getting exception on cmd.ExecuteNonQuery();
please let me know where is my mistake :doh: :(( :(( :confused: :(( :((

private void btnsubmit_Click(object sender, EventArgs e)
       {
           string Strmod = textBox5.Text;
           string strPer = comboBox1.SelectedItem.ToString();
           string strFinalPer = Strmod + strPer;


           DateTime date1 = Convert.ToDateTime(dateTimePicker1.Text);
           DateTime date2 = Convert.ToDateTime(dateTimePicker2.Text);
           SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");

           SqlCommand cmd = new SqlCommand("insert into orderbooking(orderdate ,clientname ,deliverylocation,deliverydate,deliverytime)values(@orderdate,@clientname ,@deliverylocation,@deliverydate,@deliverytime)", cnn);
           cnn.Open();
           cmd.Parameters.AddWithValue("@orderdate", date1);

           cmd.Parameters.AddWithValue("@clientname ", txtclient.Text);
           cmd.Parameters.AddWithValue("@deliverylocation ", txtlocation.Text);
          // cmd.Parameters.AddWithValue("@order_ID", textBox1.Text);
          cmd.Parameters.AddWithValue("@deliverydate ", date2);
          cmd.Parameters.AddWithValue("@deliverytime", strFinalPer);


           cmd.ExecuteNonQuery();
           cnn.Close();

           SqlConnection cnn1 = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");
           SqlCommand cmd1 = new SqlCommand("insert into orderdetails(productcode,productname,productcatagory,quantity,spldiscount,units)values(@textBox26.Text,@label12.Text,@label23.Text,@textBox3.Text,@textBox4.Text ,@label17.Text");
           //cnn1.Open();

           cmd1.Parameters.AddWithValue("@productcode",textBox26.Text);

           cmd1.Parameters.AddWithValue("@Quantity", textBox3.Text);
           cmd1.Parameters.AddWithValue("@spldiscount", textBox4.Text);

           cnn1.Open();


           MessageBox.Show ("You Successfully Submited");

           cmd1.ExecuteNonQuery();
           cnn.Close();



       }



i have did connection on text box so that when user put one info rest all three label will display this is working fine but there are three text box which take the data and put in database put it is not happening :mad: :(( :doh: :confused:
private void textBox26_TextChanged(object sender, EventArgs e)
       {
           SqlConnection cnn = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True");
           //SqlCommand cmd = new SqlCommand("select * from subproduct where productcode= textBox26");
           cnn.Open();
           DataSet ds = new DataSet();

           string str=textBox26.Text;
           string query = "select * from subproduct where productcode ='"+str+"'";
           SqlDataAdapter adapter = new SqlDataAdapter(query, cnn);
           adapter.Fill(ds);
         //  if (ds.Tables[0].Rows.Count > 0)
           for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
           {
               label12.Text = ds.Tables[0].Rows[i]["productname"].ToString();
               label17.Text = ds.Tables[0].Rows[i]["productcategory"].ToString();
               label23.Text = ds.Tables[0].Rows[i]["units"].ToString();


           }




please help thanks in Advance :rose::rose:
Posted
Updated 20-Oct-10 0:27am
v2

i think you missed the command connection to the sqlconnection "cnn1"

SqlCommand cmd1 = new SqlCommand("insert into orderdetails(productcode,productname,productcatagory,quantity,spldiscount,units)values(@textBox26.Text,@label12.Text,@label23.Text,@textBox3.Text,@textBox4.Text ,@label17.Text",cnn1);
 
Share this answer
 
Comments
aayu 20-Oct-10 5:59am    
this error is solve.
but now it is telling me must declare scalar the variable @textbox26
DavidKiryazi 20-Oct-10 6:28am    
You can't use the '@' keyword infront of textbox26 unless you have textbox26 declared with the '@' symbol in the first place.

ie. What normally happens is you declare a object such as TextBox textbox26 = new TextBox(). This is all well and good. However, it is also possible to delcare it as TextBox @textbox26 = new TextBox(). Then you have an actual variable called @textbox26. Can you see the problem? The "@" has special meanings in C# and one of them is that it allows you to use C# keywords as variable or object names. For example, int @if = 5; is valid.

In short, get rid of the "@" symbols in your command string and all will be well.
Rajesh Anuhya 20-Oct-10 6:41am    
See the below answer given by me..
In your code.
SqlCommand cmd1 = new SqlCommand("insert into orderdetails(productcode,productname,productcatagory,quantity,spldiscount,units)values(@textBox26.Text,@label12.Text,@label23.Text,@textBox3.Text,@textBox4.Text ,@label17.Text");


If you are using Direct textbox value then Remove '@' sign in @textBox.Text,
OR
If you are using @textBox as parameter then remove '.Text' Properties,

Like only use TextBox.Text.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
aayu 20-Oct-10 6:53am    
thank you
Use It U will get the Result.
Just Copy and Paste.

SqlConnection cnn1 = new SqlConnection(@"Server=NGENIOUS-WSSDEV\SQLEXPRESS;Database=MIC;Trusted_Connection= True")
SqlCommand cmd1 = new SqlCommand("insert into orderdetails
(productcode,productname,productcatagory,quantity,spldiscount,units)
values(@productcode,@productname,@productcatagory,@quantity,@spldiscoun ,@units"),cnn1);
//cnn1.Open();
MIDL
cmd1.Parameters.AddWithValue("@productcode",textBox26.Text);
            cmd1.Parameters.AddWithValue("@productname", label12.Text);
            cmd1.Parameters.AddWithValue("@productcatagory", label23.Text");

            cmd1.Parameters.AddWithValue("@quantity",textBox3.Text);
            
            cmd1.Parameters.AddWithValue("@spldiscoun", textBox4.Text);
            cmd1.Parameters.AddWithValue("@units", label17.Text);

            cnn1.Open();
            

            MessageBox.Show ("You Successfully Submited");

            cmd1.ExecuteNonQuery();
            cnn1.Close();
 
Share this answer
 
v3
Comments
aayu 20-Oct-10 6:45am    
its still giving me error
the error is The name "te4xtbox26.text" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some context) variables. Column names are not permitted.

Now what i do
Mohd Wasif 20-Oct-10 6:55am    
Aarti Just Check it all the values of controls are passing in same parameter

like in @productname we are inserting label12.Text.
Here U have shown be label12.Text. will be used for productname or productcatagory or quantity etc.i gave u code u need to pass related controls values as per parameter.

So check it .u will not find any error
aayu 20-Oct-10 7:02am    
nope now it is solve
Mohd Wasif 20-Oct-10 7:02am    
Plz copy and paste from
SqlConnection cnn1 ...
......
....
To cnn1.Cloase();
aayu 20-Oct-10 7:05am    
mohd wasif error is solve
Use the below code


SqlCommand cmd1 = new SqlCommand("insert into orderdetails(productcode,productname,productcatagory,quantity,spldiscount,units)values('" + 
textBox26.Text.ToString() + "','" +
label12.Text.ToString() + "','" +
label23.Text.ToString() + "','" +
textBox3.Text.ToString() + "','" +
textBox4.Text .ToString() + "','" +
label17.Text .ToString() + "')",cnn1);
 
Share this answer
 
Comments
aayu 20-Oct-10 6:53am    
thank you thank you thank you very much

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