【问题标题】:How to pass null value from asp.net c# to sql query?如何将空值从 asp.net c# 传递给 sql 查询?
【发布时间】:2016-05-05 17:36:54
【问题描述】:

我有一个动态绑定数据的下拉列表。让我探索一下。 我使用了五个下拉列表,如果选择了索引 0,我必须传递空值。 我试过这样但返回空字符串。

 drp_sub_stream.Items.Insert(0, new ListItem("Select a Sub Stream", null));
 drp_sub_stream.Items.Insert(0, new ListItem("Select a Sub Stream", DBNull.Value.ToString()));

然后我就这样尝试了

drp_sub_stream.Items.Insert(0, new ListItem("Select a Sub Stream", "NULL"));

但它返回 null 作为字符串。我必须在我正在搜索数据的地方单击按钮时传递此值。 按钮点击代码:

 public void _SearchCollege(string _CountryName,string _University, string _LevelName, string _Interest,string _Substream)
    {

        using (SqlConnection con = new SqlConnection(CS))
        {

            con.Open();

            //string s = "select * from edu_college_desc where (country= @country and university=@university and leveln= @level and interest= @interest and substream=@substream) or (country=@country or university=@university or leveln=@level or interest=@interest or substream=@substream) ";

            string s= "select * from edu_college_desc where country = ISNULL(@country ,country) and university = ISNULL(@university ,university)   and leveln      =   ISNULL(@level ,leveln) and interest    =   ISNULL(@interest ,interest) and substream   =   ISNULL(@substream,substream)";
            SqlDataAdapter da = new SqlDataAdapter(s, con);
            da.SelectCommand.Parameters.AddWithValue("@country", _CountryName);
            da.SelectCommand.Parameters.AddWithValue("@university", _University);
            da.SelectCommand.Parameters.AddWithValue("@level",_LevelName);
            da.SelectCommand.Parameters.AddWithValue("@interest", _Interest);
            da.SelectCommand.Parameters.AddWithValue("@substream", _Substream);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                HttpContext.Current.Session["edusearch"] = dt;
                HttpContext.Current.Response.Redirect("edusearch.aspx");

            }
            else
            {
                HttpContext.Current.Session["edusearch"] = null;
                HttpContext.Current.Response.Redirect("edusearch.aspx");
            }
        }
    }

需要解决方案。

【问题讨论】:

  • 你真正想做的是什么?将null值传给Sql Statement,还是在Parameter中使用?
  • 您可以简单地传递DBNull.Value。例如- da.SelectCommand.Parameters.AddWithValue("@substream", DBNull.Value); 您需要将 null 作为参数传递。

标签: c# asp.net


【解决方案1】:
drp_sub_stream.Items.Insert(0, new ListItem("Select a Sub Stream", "0"));

drp_sub_stream.Items.Insert(0, new ListItem("Select a Sub Stream", String.Empty));

然后您可以使用简单的 if SelectedValue 等于零或 string.Empty 并决定是否应用和过滤(sql查询中的连接)

string s= "select * from edu_college_desc where country = ISNULL(@country ,country) and university = ISNULL(@university ,university)   and leveln      =   ISNULL(@level ,leveln) and interest    =   ISNULL(@interest ,interest)";
if(!String.IsNullOrEmpty(_SubStream))
    s+= " and substream   =   ISNULL(@substream,substream)";
...


if(!String.IsNullOrEmpty(_Substream)) 
   da.SelectCommand.Parameters.AddWithValue("@substream", _Substream);

这样你也可以使用必需的验证器来检查你是否选择了一个值(在零的情况下,你必须添加InitialValue="0"

【讨论】:

  • 没错。然后你编辑你的方法来添加and substream = ISNULL(@substream,substream)da.SelectCommand.Parameters.AddWithValue("@substream", _Substream);只有if (!String.IsNullOrEmpty(substream))
  • 你能详细说明一下吗?
  • 我不明白,请你写正确的sn-p代码。
猜你喜欢
  • 2018-06-16
  • 2010-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
  • 1970-01-01
  • 2022-12-02
相关资源
最近更新 更多