2014-07-02 28 views
0

我面臨着一個錯誤信息,我不知道我的原因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(); 
    } 
} 
+0

@Grant Winney其與價值,因爲在同一個頁面的另一個列表視圖而顯示的項目,並使用同一個會話位置 – user3716314

+0

什麼是它在執行查詢之前'location'和'category'的值?必要時放置一個斷點。 –

+0

您是否檢查過「類別」有價值? –

回答

1

這有可能爲下面的代碼行分配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);