【发布时间】: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 作为参数传递。