[英]parameterized with session and query string
我正面臨一條錯誤消息,但我不知道行中的sql語句的原因:
DACatPgeVIPLIST.Fill(dsCatPgeVIPLIST);
它顯示此消息,您可以幫我嗎:
參數化查詢
(@Country nvarchar(7),@Category nvarchar(4000))SELECT a.[AdsID],
期望未提供參數@Category
。
碼:
if (Session["location"] != null)
{
using (SqlConnection CatPgeVIPLISTsqlCON = new SqlConnection(cs))
{
CatPgeVIPLISTsqlCON.Open();
SqlDataAdapter DACatPgeVIPLIST = new SqlDataAdapter("SELECT a.[AdsID], a.[Country], a.[State], a.[City], a.[AdsTit], SUBSTRING(a.[AdsDesc], 1, 70) as AdsDesc, a.[AdsPrice], a.[Img1] FROM [ads] as a INNER JOIN [UserInfo] as u on u.UID = a.UID WHERE a.[Country] = @Country and a.[Category] = @Category and u.VIP = 'Yes'", cs);
string location = Convert.ToString(Session["location"]);
string category = Request.QueryString["category"];
DACatPgeVIPLIST.SelectCommand.Parameters.AddWithValue("@Country", location);
DACatPgeVIPLIST.SelectCommand.Parameters.AddWithValue("@Category", category);
DataSet dsCatPgeVIPLIST = new DataSet();
DACatPgeVIPLIST.Fill(dsCatPgeVIPLIST);
CatPgeVIPLIST.DataSource = dsCatPgeVIPLIST.Tables[0];
CatPgeVIPLIST.DataBind();
}
}
以下代碼行可能會將null
分配給類別:
string category = Request.QueryString["category"];
您可能會這樣解決,它將null
轉換為空字符串:
string category = Convert.ToString(Request.QueryString["category"]);
或者,您可以嘗試傳遞DBNull.Value
而不是null
(未經測試):
DACatPgeVIPLIST.SelectCommand.Parameters
.AddWithValue("@Category", (object)category ?? DBNull.Value);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.