开发者

How can I update a database string value from a TextBox?

I cant update my database through textbox.in the case of numeric value i can update but cant with a string value. my coding is...

Sql开发者_如何转开发Command cmdup= new SqlCommand("UPDATE [port1] SET [prt1]=@prt1 WHERE [no]= 1",cn);
cmdup.Parameters.Add("@prt1", TextBox1.Text);  
cmdup.ExecuteNonQuery();

if any one know the ans: reply me


I'd recommend explicitly defining the parameter type to match the type in the DB, and passing the value to Parameters.Add as the appropriate type.

e.g. at the moment, you are passing a string typed value to Parameters.Add, and not defining the type explicitly. Therefore, it will assume the data type from the type of value supplied...so the @prt1 type will be passed in to the DB as NVARCHAR I believe.

If the prt1 field is an INTEGER for example, much safer IMHO to do something like:

cmd.Parameters.Add("@prt", SqlDbType.Int,4).Value = Int32.Parse(TextBox1.Text);

or

cmd.Parameters.AddWithValue("@prt", Int32.Parse(TextBox1.Text));

I always like to fully define the type of the parameters I pass in to SQL to rule out any potential, unexpected issues.


Check the datatype for the @prt1 to see if it is a Int value because you are setting to the same column two different values.


Parse the textBox to Integer as you want to insert a numeric value i.e. Int64.Parse("Textbox1.Text"); well with string it does have problems.


Just a wild guess: Does prt maybe only accept numeric values?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜