Click here to Skip to main content
15,662,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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))

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.

Thanks for the helps!

What I have tried:

sql insert by Addwithvalue command for Integer datatype.
Updated 30-May-17 9:10am
Richard MacCutchan 30-May-17 14:58pm    
Please show the definition of your table and the code of your INSERT statement.

1 solution

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

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!

Then you can write the code, and perform the rest of the stuff and add the parameter values to the query, then it would execute properly.

Int32.TryParse Method (String, Int32) (System)[^]
Share this answer
Paramu1973 31-May-17 1:42am    

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