文本框为空时输入空值 [英] Enter null value when text box is empty

查看:95
本文介绍了文本框为空时输入空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过不同的方法,但没有一种方法可以使用:



I have tried different method to do this but none is working:

<pre lang="c#">
string strListOthLog = ((TextBox)fvAddBookingInfo.FindControl("txtOtherLogList")).Text;
string strComment = ((TextBox)fvAddBookingInfo.FindControl("txtComment")).Text;
.......
 cmd.Parameters.Add("@other_logon_list", SqlDbType.NVarChar).Value = string.IsNullOrEmpty(strListOthLog) ? (object)DBNull.Value : strListOthLog;
   
if (string.IsNullOrEmpty(strComment))
            {
                cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = System.DBNull.Value;
            }
            else
            {
                cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = strComment;
            }





我的尝试:



System.DBNull.Value以不同的方式。



What I have tried:

System.DBNull.Value in different ways.

推荐答案

使用 null 而不是 DBNull.Value



use null instead of DBNull.Value

cmd.Parameters.Add("@other_logon_list", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(strListOthLog) ? null : strListOthLog;
cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(strComment) ? null : strComment;







DBNull.Value:
DBNull.Value:

DBNull.Value用于处理数据库。数据库列可以包含空值。但是,当在代码中将该数据选择到对象(例如DataTable)中时,就代码而言,有一个对象可供引用。但是,该对象包含数据库中空值的表示。因此,DBNull.Value存在以表示该区别。它基本上意味着这里有一个C#对象从数据库中获取一个值,但该值为空。

DBNull.Value is for the purpose of working with databases. A database column can contain a null value. However, when that data is selected into an object (such as a DataTable) in code, there is an object there to reference as far as the code is concerned. However, that object contains a representation of a null value from the database. Thus, DBNull.Value exists to represent that distinction. It basically means "There is a C# object here which grabbed a value from a database, but that value is null."





参考 null vs DBNull.Value [ ^ ]


此代码将起作用

This code will work
if (strListOthLog == "")
  cmd.Parameters.AddWithValue("@other_logon_list", DBNull.Value);
else
  cmd.Parameters.AddWithValue("@other_logon_list", strListOthLog);


谢谢大家的帮助。我一次尝试了上述所有方法,但没有成功输入空值而不是表格中的空白空格。最后我通过执行以下操作来实现它:

在存储过程中,我为不需要的字段设置默认值null(用户可以选择不输入值)。

@additional_comments varchar(500)= null

然后在lbInsert_Click事件中我有以下内容:

string strComment =((TextBox)fvAddBookingInfo.FindControl(txtComment ))。文字;

....

if(strComment!= String.Empty)

{

cmd.Parameters.Add(@ additional_comments,SqlDbType.VarChar).Value = strComment;

}

这对我有用。

再次感谢大家的支持。
I thank you all for helping me. I tried all the above mentioned ways one at a time and was not successful entering null value instead of blank empty space in a table. Finally I got it worked by doing the following:
In the stored procedure I set the default value null for the fields that are not required (user may choose not to enter a value).
@additional_comments varchar (500) = null
Then in the lbInsert_Click event I have following:
string strComment = ((TextBox)fvAddBookingInfo.FindControl("txtComment")).Text;
....
if (strComment != String.Empty)
{
cmd.Parameters.Add("@additional_comments", SqlDbType.VarChar).Value = strComment;
}
This is working for me.
Thanks again to all of you for your support.


这篇关于文本框为空时输入空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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