如何INSERT到SQL表整数数据类型 - 列? [英] How to INSERT to SQL table integer datatype- column ?
问题描述
嗨
我有一个小项目练习,而我希望插入我的数据,收到以下错误。
其实我希望保存,int数据栏
我的代码: -
Hi
I have a small project for practice, while I wish to insert my data, received the following errors.
Actually I wish to save, int data column
My Code :-
insert.Parameters.AddWithValue("@mobile", Convert.ToInt32(TextBox6.Text))
我尝试其他方式< br $> b $ b
And I try the other way
int val2 = 0;
val2= 1 * System.Convert.ToInt32(TextBox6.Text);
insert.Parameters.AddWithValue("@mobile", val2)
也没有成功!
错误消息: -
Also not succeeded!
Error Message :-
An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code
Additional information: Input string was not in a correct format.
感谢您的帮助!
我尝试过:
通过Addwithvalue命令为Integer数据类型插入sql。
Thanks for the helps!
What I have tried:
sql insert by Addwithvalue command for Integer datatype.
推荐答案
也许,您的字符串无法转换为整数。你传递了什么字符串?由于您使用的是文本框的值,因此它可能包含一些非数字字符,即使空格也可能导致问题。
另外,更改,
Perhaps, your string cannot be converted to integer. What string are you passing? Since you are using the value of a textbox, it is likely to contain some non-numeric characters there, even a space can cause issues.
Also, change,
int val2 = 0;
val2 = 1 * ...
以下,
To the following,
int val2 = System.Convert.ToInt32(TextBox6.Text);
更安全的是使用 int.TryParse(字符串值,输出int变量);
,如下所示:
Even safer would be to use a int.TryParse(string value, out int variable);
, which would look like the following,
int value;
if(int.TryParse(TextBox6.Text, out value)) {
// Correct integer
} else {
// Problem!
}
然后你可以编写代码,执行其余的东西并将参数值添加到查询,然后它将正确执行。
Int32 .TryParse方法(String,Int32)(系统) [ ^ ]
这篇关于如何INSERT到SQL表整数数据类型 - 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!