2016-05-05 13 views
1

データを動的にバインドするドロップダウンリストがあります。私はそれを探そう。 私はインデックス0が選択されている場合、null値を渡す必要があります私は5つのドロップダウンリストを使用しました。 私はこれを試してみましたが、空の文字列を返します。asp.net C#からnull値を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 Statementにnull値を渡すか、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(@サブストリーム、サブストリーム)'と 'da.SelectCommand.Parameters.AddWithValue(" @サブストリーム "、_Substream);' 'if(!String.IsNullOrEmpty(substream) ) ' – Emanuele

+0

あなたはそれを詳しく説明できますか? – shashank

関連する問題