我有一个数据动态绑定的下拉列表。让我探索它。 我使用了五个dropdownlist,如果选择了索引0,我必须做的是必须传递null值。 我试过这样但返回空字符串。如何将空值从asp.net c#传递给sql查询?
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 [email protected] and leveln= @level and interest= @interest and [email protected]) or ([email protected] or [email protected] or [email protected] or [email protected] or [email protected]) ";
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");
}
}
}
需求的解决方案。
你真的想做什么?试图将空值传递给Sql语句,或者在Parameter中使用? – ali
您只需传递'DBNull.Value'即可。例如 - 'da.SelectCommand.Parameters.AddWithValue(“@ substream”,DBNull.Value);'你需要传递null作为参数。 – Yogi