帮我解决这个错误 [英] Help me to solve this error

查看:57
本文介绍了帮我解决这个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的按钮代码,我在cmd.ExecuteNonQuery();上遇到异常.
请让我知道我的错误在哪里:doh::((:((:confused::((:((

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();



       }




我已经在文本框上进行了连接,所以当用户放置一个信息时,所有三个标签都将显示工作正常,但是有三个文本框将数据放入数据库中却没有发生:mad::((: doh::confused:




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();


           }





请帮助预先感谢:rose :: rose:





please help thanks in Advance :rose::rose:

推荐答案

我认为您错过了与sqlconnection"cnn1"的命令连接

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);


在您的代码中.
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");



如果您使用的是直接文本框值,则在@ textBox.Text中删除"@"符号,

如果使用@textBox作为参数,则删除``.Text''属性,

只喜欢使用TextBox.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.


使用它U将得到结果.
只需复制和粘贴.

SqlConnection cnn1 =新的SqlConnection(@"Server = NGENIOUS-WSSDEV \ SQLEXPRESS; Database = MIC; Trusted_Connection = True")
SqlCommand cmd1 =新的SqlCommand(插入orderdetails
(产品代码,产品名称,产品类别,数量,折扣,单位)
值(@ productcode,@ productname,@ productcatagory,@ quantity,@ spldiscoun,@ units),cnn1);
//cnn1.Open();
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();
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();


这篇关于帮我解决这个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆