データを動的にバインドするドロップダウンリストがあります。私はそれを探そう。 私はインデックス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");
}
}
}
解決策が必要です。この中
あなたは本当に何をしていますか? Sql Statementにnull値を渡すか、Parameter? – ali
単に 'DBNull.Value'を渡すことができます。例えば - 'da.SelectCommand.Parameters.AddWithValue(" @ substream "、DBNull.Value);ここで、パラメータとしてnullを渡す必要があります。 – Yogi