错误得到解决,谢谢大家 [英] Error Got solved Thank you every one

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

问题描述

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


此行所做的更改


Change done on this line

SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory,productname,productcode,price,munits,date)values('" + cmdproduct.SelectedValue.ToString() + "','" + txtsub.Text + "','" + txtcode.Text + "'," + txtprice.Text + ",'" + cmdmunits.SelectedValue.ToString() + "','" + date1.ToString() + "')", con);

 cnn.Open();



注释所有这些代码



Comment all this code

 //cmd.Parameters.AddWithValue("@productcategory", cmdproduct.SelectedItem.ToString());
 //cmd.Parameters.AddWithValue("@productname", txtsub.Text);
 //cmd.Parameters.AddWithValue("@productcode", txtcode.Text);
 //cmd.Parameters.AddWithValue("@price", txtprice.Text);
 //cmd.Parameters.AddWithValue("@munits", cmdmunits.SelectedItem.ToString());
 //cmd.Parameters.AddWithValue("@date", date1);

// string dt = (string)dateTimePicker1.Text;
 cnn.Close();

 MessageBox.Show("Record Successfully Generated");



这是我在提交"按钮上编写的代码,并且错误消息我正在截取异常字符串或二进制"数据.该声明已终止.

我的SQL查询是



This is the code which i have written on submit button and the error i am getting the exception "String or binary "data would be truncated. The statement has been terminated.

My sql query is

create table subproduct
(
productcategory varchar(50),
productname varchar(50),
productcode varchar(20),
price int,
munits varchar(20),
date datetime
)



Yupiiiiiiii错误解决方:laugh::laugh::-O:-O:rolleyes::rolleyes:


在此先感谢:rose :: rose :: rose:



Yupiiiiiiii Error Solved Party :laugh: :laugh: :-O :-O :rolleyes: :rolleyes:


Thanks in Advance:rose::rose::rose:

推荐答案

尝试从SQL Server查询分析器窗口运行sql插入查询.看看是否将值插入到数据库中,然后尝试使用代码中的同一组值........希望这可以让您解决问题.由于您没有使用任何DAL或BLL,因此问题仅出在您的表示层上,因此应该不难发现它.
Try running the sql insert query from your SQL server query analyzer window. See if the values get inserted to the database, then try with the same set of value from your code...........hope this will let you solve the problem. As you are not using any DAL or BLL, the problem lies in your presentation layer only, so it should not be hard to track it out.


更改以下行并尝试

SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory ,productname ,productcode,price ,munits,date )values(@productcategory,@productname ,@productcode,@price ,@munits,#date )", cnn);


问候
ak.naser
change the following line and try it

SqlCommand cmd = new SqlCommand("insert into subproduct(productcategory ,productname ,productcode,price ,munits,date )values(@productcategory,@productname ,@productcode,@price ,@munits,#date )", cnn);


regards
ak.naser


您的代码非常适合我.
请检查,

-textBox1.Text仅返回数值
-cmdmunits.SelectedItem.ToString()仅返回数字值
-date1仅返回DateTime(从我认为仅是数字的代码中)

我怀疑textBox1.Text 和cmdunits.SelectedItem

检查字符串的长度是否超过.

请参考此链接 http://www.dotnetspider. com/resources/30543-SQL-Error-String-or-binary-data-would-be.aspx [
You code works perfectly for me.
Please check,

- textBox1.Text returns only numerical values
- cmdmunits.SelectedItem.ToString() return only numerical values
- date1 returns only DateTime (from the code I believe it is numerical only)

I suspect textBox1.Text and cmdunits.SelectedItem

Check if the length of the string exceeds.

Please refer this link http://www.dotnetspider.com/resources/30543-SQL-Error-String-or-binary-data-would-be.aspx[^]

The problem should be in this line.

cmd.Parameters.AddWithValue("@price", textBox1.Text);



price是一个int字段.使用Command发送的参数是默认的字符串数据类型.您需要为上述行指定数据类型.

选项1:



price is a int field. Parameters you send using Command are defaulted string datatype. You need to specify the data type for the above line.

Option1:

cmd.Parameters.AddWithValue("@price", Convert.ToInt16(textBox1.Text));



选项2:



Option 2:

cmd.Parameters.Add(new SqlParameter("@price", SqlDbType.Int));
cmd.Parameters["@price"].Value = Convert.ToInt16(textBox1.Text);



这也适用于日期字段.



This applies to the date field also.


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

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