Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
UpdateCommand.Parameters.AddWithValue("@post", SqlDbType.VarChar).Value = Convert.ToInt32(textBox7.Text.Trim());
Posted
Comments
[no name] 9-Mar-15 5:14am    
What do u get in textBox7.Text ?
[no name] 9-Mar-15 5:15am    
number value
Rajesh waran 9-Mar-15 5:17am    
Your @post Sqldbtype is varchar,but you are passing integer value to it. Check it out.
Also you have to check textbox value. Bcoz you are using Convert.Int32() so you have to check that textbox values is in integer or not?

You should try this code instead, to make sure this error goes away.

C#
int number;
if(int.TryParse(textBox7.Text.Trim(), out number) {
    // If the number can be parsed
    UpdateCommand.Parameters.AddWithValue("@post", SqlDbType.VarChar).Value =       
    Convert.ToInt32(number);
}


Above code would check if the number can be converted, if it can be then it would execute the code block and the number would have the value that was converted from string to int. Also, the error won't occur, since you would execute the code only if the type is number.

Also as mentioned, you would also need to change the type of your data being passed to the database.

Int32.TryParse[^], Convert.ToInt32[^] on MSDN.
 
Share this answer
 
C#
//will get the error while converting empty string to int, so make the condition accordingly 
if(textBox7.Text.Trim() != null || textBox7.Text.Trim() != String.Empty)
{
UpdateCommand.Parameters.AddWithValue("@post", SqlDbType.VarChar).Value =   Convert.ToInt32(textBox7.Text.Trim());
}
 
Share this answer
 
v2

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