如何在asp.net中将空的textox转换为NULL值 [英] How can we convert empty textox to NULL Value in asp.net

查看:90
本文介绍了如何在asp.net中将空的textox转换为NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;

如何将空文本框的值转换为NULL值?
我想将文本框中的数据(NULL)存储到数据库中.
代码:

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.

推荐答案

您能在这里解释您要尝试什么吗:Convert.DBNull.Equals(TextBox11.Text);
当然,它看起来与您想要的不一样.

顺便说一句,一个简单的VS调试器会告诉您错误行,错误本身是不言而喻的.

我想将文本框中的数据(NULL)存储到数据库中.
试试:
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;


或者您也可以采用其他方式-

Or you can also go the other way -

<pre lang="sql">if (!String.IsNullorEmpty(TextBox11.Text))<br />
{<br />
      cmd.Parameters.Add("@p", SqlDbType.Float).Value = TextBox11.Text.Trim();<br />
}</pre><br />



当textbox1为空时,根据您的需要,将不会简单地传递此参数,并且您的数据库将为NULL.



HTH-
拉杰夫

如果有帮助,请投票并标记为接受.



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.


您可以尝试
You can try
<code>
cmd.Parameters.AddWithValue("@p", (TextBox11.Text.Trim() == string.Empty) ?
                DBNull.Value :  (object)TextBox11.Text.Trim());

</code>


这篇关于如何在asp.net中将空的textox转换为NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆