错误:将nvarchar值'd'转换为数据类型int时转换失败。 [英] Error :Conversion failed when converting the nvarchar value 'd' to data type int.

查看:82
本文介绍了错误:将nvarchar值'd'转换为数据类型int时转换失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想将数据从某个文本框插入到sqldatabase的表中,但有些字段是int,有些是nvarchar如何将文本框的文本转换为它们我在下面的代码:



Hi I want to insert data from some text box to table of sqldatabase but some field is int and some one is nvarchar how to convert text of text box to them i do below code :

constr = "Data Source=SAFA4-PC;Initial Catalog=StudentDb;Integrated Security=True";
 System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection();
 Conn.ConnectionString = constr;
 Conn.Open();
 System.Data.SqlClient.SqlCommand Comm = new System.Data.SqlClient.SqlCommand();
 Comm.CommandText = "insert into Tbl_Student (Code ,Name ,LastName ,CityCode ,Address ,BirthDay ,FatherName ,ID,CodeMeli,tel ) values( @Code,@Name,@LastName,@FatherName,@Id,@MeliCode,@Tel,@Address,@BirthDay,@CityCombo)";
 Comm.CommandType = System.Data.CommandType.Text;
 Comm.Connection = Conn;
 Comm.Parameters.Add("@Code", SqlDbType.Int);//, int.Parse(StudentCode.Text));
 Comm.Parameters["@Code"].Value = int.Parse(StudentCode.Text);
 Comm.Parameters.Add("@Name", SqlDbType.NVarChar);//Name.Text);
 Comm.Parameters["@Name"].Value = Name.Text;
 Comm.Parameters.Add("@LastName", SqlDbType.NVarChar);// LastName.Text);
 Comm.Parameters["@LastName"].Value = LastName.Text;
 Comm.Parameters.Add("@FatherName", SqlDbType.NVarChar);// FatherName.Text);
 Comm.Parameters["@FatherName"].Value =FatherName.Text;
 Comm.Parameters.Add("@Id", SqlDbType.Int); //int.Parse(Id.Text));
 Comm.Parameters["@Id"].Value = int.Parse(Id.Text);
 Comm.Parameters.Add("@MeliCode",SqlDbType.Int);
 Comm.Parameters["@MeliCode"].Value = int.Parse(MeliCode.Text);
 Comm.Parameters.Add("@Tel",SqlDbType.Int);
 Comm.Parameters["@Tel"].Value = int.Parse(Tel.Text);
 Comm.Parameters.Add("@Address",SqlDbType.NVarChar);
 Comm.Parameters["@Address"].Value = Address.Text;
 Comm.Parameters.Add("@BirthDay", SqlDbType.Char);// BirthDay.Text);
 Comm.Parameters["@BirthDay"].Value = BirthDay.Text.ToString();
 Comm.Parameters.AddWithValue("@CityCombo", CityCombo.ValueMember);
 Comm.ExecuteNonQuery();
 Conn.Close();



但我有这个错误:

转换nvarchar值''d'时转换失败'到数据类型int。



请帮帮我

Tanx


but i have this error :
Conversion failed when converting the nvarchar value ''d'' to data type int.

Please help me
Tanx

推荐答案

检查解析为int的值。其中一个是字符串,你试图将值转换为整数。

因此你得到这个错误。
Check the values where you parse to int. One of them is a string and you are trying to convert the value to integer.
Thus you get this error.


一旦你应用了一个断点并调试了代码,你会知道错误到底在哪里。
Once you apply a breakpoint and debug the code, you will come to know where exactly the error is encountering.


异常消息是自解释的,因为我们看不到你的数据或调试,我们可以建议你错误是在你解析为 int 的其中一行(你在 int 上调用Parse的4行,你应该把它放在哪里断点)。



另外你可以使用TryParse并分配值,如果它被解析,然后你会看到你的缺失值(制造麻烦的那个)。



您也可以使用三元运算符,以免膨胀这样的代码:

The exception message is self explanatory and since we can not see your data or debug we can suggest you that the error is at one of the lines where you parse to int (totally 4 lines where you call Parse on int, where you should put the breakpoint).

Additionally you could use TryParse and assign value if it is parsed and then you will see where is your missing value (the one that makes trouble).

You can also make use of ternary operator in order not to bloat the code like this:
//make temp int for parsing, just once (outside of the loop if you are looping).
int tmpInt;

//chose one of these

//assign value if successfully parsed
int.TryParse(StudentCode.Text, out tmpInt)??Comm.Parameters["@Code"].Value = tmpInt;

//or if value is not parsed assign -1
Comm.Parameters["@Code"].Value = int.TryParse(StudentCode.Text, out tmpInt)?tmpInt:-1;





快乐编码。



Happy coding.


这篇关于错误:将nvarchar值'd'转换为数据类型int时转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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