Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello;

How can we convert the value of empty textbox to NULL value?
I want to store the data(NULL) from textbox to data base.
CODE:

if (TextBox11.Text == String.Empty)
{
    cmd.Parameters.Add("@p", SqlDbType.Float).Value = /* WHAT I WRITE HERE TO TAKE NULL FORM TEXTBOX TO STORE
                                                         IN DATABASE  */
}


This shows an error:
"Failed to convert parameter value from a String to a Double."

Please help me.
Posted
Updated 7-Mar-11 17:53pm
v4

Can you explain what are you trying here: Convert.DBNull.Equals(TextBox11.Text);
Surely, it does not look like what you intend to.

BTW, a simple VS Debugger would had told you the error line and error in itself is quite self-explanatory.

I want to store the data(NULL) from textbox to data base.
Try:
cmd.Parameters.Add("@p", SqlDbType.Float).Value = System.DBNull.Value;
 
Share this answer
 
Comments
fjdiewornncalwe 8-Mar-11 16:11pm    
+5. Correct. This error is completely self explanatory
Or you can also go the other way -

SQL
if (!String.IsNullorEmpty(TextBox11.Text))
{
      cmd.Parameters.Add("@p", SqlDbType.Float).Value = TextBox11.Text.Trim();
}


When the textbox1 is empty, this parameter won't be passed simply and your database will have NULL, as per your need.



HTH -
Rajeev

Please vote and mark the answer as accepted if this helps you.
 
Share this answer
 
You can try
<code>
cmd.Parameters.AddWithValue("@p", (TextBox11.Text.Trim() == string.Empty) ?
                DBNull.Value :  (object)TextBox11.Text.Trim());

</code>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900