1

In reference to this question that I just asked -- SQL - Select all when filter value is empty -- it appears that for some reason, an empty TextBox's value is not being fed to SQL Server as NULL, as it ought to be.

Any ideas on how to fix this?

10
  • What makes you think an empty text box should become NULL to SQL Server? Commented Mar 16, 2010 at 2:04
  • Well, logic would state that when a text box is empty, its value is NULL. I mean, what else would its value be? Commented Mar 16, 2010 at 2:06
  • 1
    null in a programming language isn't necessarily related to NULL in a relational database. Commented Mar 16, 2010 at 2:06
  • You also might want to say how you're sending the data to SQL Server. Commented Mar 16, 2010 at 2:07
  • Re: how I'm sending the data to SQL Server: ASP.net AJAX. The DataList is in an UpdatePanel that has TextBox.TextChanged set as one of its triggers. This will all have to change, seeing as I obviously need to do some C#-level stuff to the Textbox value. Commented Mar 16, 2010 at 2:13

5 Answers 5

16

An empty text box will contain string.Empty, not null.

From the TextBox.Text documentation:

Property Value
Type: System.String
The text displayed in the TextBox control. The default is an empty string ("").

If you want to make sure it goes to the database as null, then convert it on the way in:

string value = !string.IsNullOrEmpty(tb.Text) ? tb.Text: null;
Sign up to request clarification or add additional context in comments.

Comments

4

just to enlighten you :)

this is from reflector:

public virtual string Text
{
    get
    {
        string str = (string) this.ViewState["Text"];
        if (str != null)
        {
            return str;
        }
        return string.Empty;
    }
    set
    {
        this.ViewState["Text"] = value;
    }
}

1 Comment

Thanks for showing us what the Framework actually does, and eliminating any doubt.
0

You can do this without any C#.

Simply set the parameters ConvertEmptyStringToNull property to true, eg:

<asp:ObjectDataSource runat="server" ID="ObjectDataSourceExample" TypeName="Example.ExampleTableAdapter" SelectMethod="GetDataBySomething">
    <SelectParameters>
        <asp:ControlParameter Name="Something" ConvertEmptyStringToNull="true" ControlID="tbSomething" />
    </SelectParameters>
</asp:ObjectDataSource>

2 Comments

Anyway, the default value is true: "true if the value that the Parameter is bound to should be converted to nullNothingnullptra null reference (Nothing in Visual Basic) when it is String..::.Empty; otherwise, false. The default value is true." (msdn.microsoft.com/en-us/library/…
Is the parameter of your query set as nullable in your dataset?
0

"""Property Value Type: System.String The text displayed in the TextBox control. The default is an empty string ("")."""

Aaronaught is correct.

Null is a very loosely used term and shouldn't be, and can easily be confused with 'Nothing' which is far better to use in programming. A textbox.text value will never be null, it will be "". Nothing refers to whether you have initialized the string variable or not. sooooo

the textbox itself could = Nothing
the textbox.text can only equal "" (or string.empty - function does the same thing) if the textbox isnot nothing

It's a general good rule of thumb to use string.empty or "" when referring to whether a string contains any characters

Obviously the Paramater you use in ur sqlcommand needs to be null or not so...

In VB:

If TextBox.Text = "" Then
VariableUsedInSqlCommand = Nothing
End If

From her you can set the default paramater value in SP or Inline SqlCommand to be Null, so when it recieves a paramter that Is Nothing, it reverts the paramater to = Null

In Stored Procedure (if you're sending the textbox.text value directly to a SP Paramater):

If (@Paramater = "") 
Begin

end
else
begin

end

Comments

-1

Set the default parameter value in the ASP code to null.

1 Comment

I don't get it...I somehow got 10 points for this, then negative 2. I'm guessing it was voted up, then voted down?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.