1

I have one TextBox (Name:TB_Test) If I do not input any string.... In SQL DB "test" field will get "NULL" How can i do,"test" field change blank string ,not "NULL" thank you

Michael Wu from Taiwan

Code :

SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.UpdateParameters.Clear();
SqlDataSource1.UpdateParameters.Add("test",String.IsNullOrEmpty(TB_Test.Text.ToString()) ? "": TB_Test.Text.ToString());
SqlDataSource1.Update();
1
  • Just replace "test" with "@test" and Add with AddWithValue- rest of your code is fine. Commented Apr 27, 2017 at 6:51

3 Answers 3

1

Add default value to your Column.

ALTER TABLE TableName ADD CONSTRAINT DF_SomeName DEFAULT N'' FOR test;
Sign up to request clarification or add additional context in comments.

1 Comment

Although I can set the default value "blank" But after the update becomes NULL again
0

Change the DB as @Amit Kumar suggested or use the Add(Parameter) overload of ParameterCollection. Something like this:

var parameter = new Parameter("test")
{
  DefaultValue = String.IsNullOrEmpty(TB_Test.Text.ToString()) 
     ? "" : TB_Test.Text.ToString()),

  ConvertEmptyStringToNull = false
};
SqlDataSource1.UpdateParameters.Add(parameter);

The important part is the ConvertEmptyStringToNull property of the Parameter class which defaults to true.

Comments

0

try String.Empty as below.

String.IsNullOrEmpty(TB_Test.Text.ToString()) ? String.Empty : TB_Test.Text.ToString()

1 Comment

Use "String.Empty" code,The result is the same "NULL"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.