1

i have textbox1 in form as "NOTE" when i type text in textbox1 and press insert button it's work but when i get that text again from DB to textbox1 and try to remove the text (to remove the NOTE from DB) and the cursor is in the textbox1 .. and press insert button again i get this error :

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size. enter image description here

how i can use if statement to cheek if textbox1 IsNullOrEmpty or IsNullOrWhiteSpace i have tried them both not working ..

3
  • What have you tried that isn't working? if (textbox1.Text == "") { ... } is an easy enough check... Commented Jun 3, 2017 at 0:00
  • i want to insert null value when i remove the text .. how to do it broh ? Commented Jun 3, 2017 at 0:02
  • i am using update query in sql server .. update value with null value . Commented Jun 3, 2017 at 0:03

1 Answer 1

2

SqlCommand.Prepare method requires all variable length parameters to have an explicitly set non-zero Size.

this means that when you are pressing the button for the next time the NOTE.Length = 0 and p11.Size is set to zero, which is not acceptable and giving rise to the issue.

This being said you can try something like this:

if(NOTE.Length > 0)
  p11.Size = NOTE.Length;
else
  p11.Size = 1;//<-non-zero Size in case the NOTE length happens to be zero.
//also same check for NOTE itself as well if you wish. check whether it is NULL or not and set p11 Value accordingly

Hope this helps.

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

1 Comment

thank you so much it worked great in my code i put the code like this in my code: myCommand.Parameters.Add(p11); if (NOTE.Length > 0) p11.Size = NOTE.Length; else p11.Size = 1; myCommand.Prepare(); myCommand.ExecuteNonQuery(); now i can remove and insert and keep coding hhhh thank you brother

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.