2016-05-05 45 views
1

我有一个数据动态绑定的下拉列表。让我探索它。 我使用了五个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"); 
      } 
     } 
    } 

需求的解决方案。

+0

你真的想做什么?试图将空值传递给Sql语句,或者在Parameter中使用? – ali

+0

您只需传递'DBNull.Value'即可。例如 - 'da.SelectCommand.Parameters.AddWithValue(“@ substream”,DBNull.Value);'你需要传递null作为参数。 – Yogi

回答

0
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)); 

然后你可以使用一个简单的,如果的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"

+0

我已经试过但它返回空字符串 – shashank

+0

这是正确的。然后你编辑你的方法来添加'和substream = ISNULL(@ substream,substream)'和'da.SelectCommand.Parameters.AddWithValue(“@ substream”,_Substream);' 只有'if(!String.IsNullOrEmpty(substream) )' – Emanuele

+0

你能说说吗? – shashank