0

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...

SqlCommand 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

4
  • And what error do you receive? Commented Dec 3, 2009 at 14:29
  • I will never understand why people like to roll their questions back to poorly written sentences... Commented Dec 3, 2009 at 14:35
  • Not everyone knows or has a full grasp of the English language Commented Dec 3, 2009 at 15:22
  • Ahh I understand what you were saying, you edited the question to make it easier to understand, and then they rolled it back. Got it, sorry :) Commented Dec 3, 2009 at 15:24

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

2 Comments

or with Int32 or int.Parse("txt1.Text"); but 64 is much preferabble. . . 32 or 64 are bits, as c sharp strongly typed language so it has strict specification every datatypes
you can edit your answer, rather than putting it in bits in the comment section.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.