2016-01-23 47 views
-1

我得到一個錯誤「必須聲明標量變量@IdAlbum」。我已經通過了關於這個主題的類似帖子,但它沒有幫助。有人可以看看下面的代碼嗎?當我使用命令參數時,它給了我錯誤。如果我不使用命令參數,它工作正常。我已在代碼中加入評論以突出問題所在。異常|必須聲明標量變量@IdAlbum

public DataSet databind() 
    { 
     DataSet ds = new DataSet(); 
     string con_str = ConfigurationManager.ConnectionStrings["con_nc"].ConnectionString; 
     SqlConnection con = new SqlConnection(con_str); 
     try 
     { 

      con.Open(); 

      string albumId = Request.QueryString["AlbumId"].Trim(); 
      this.albumName = Request.QueryString["AlbumName"]; 

      //getting the count of the pictures from DB and assigning it to pagesize so that all pictures of single album come in 1 page only. 

      string photoCountQuery = "select count(*) from gallery_photos where [email protected]"; // THIS WORKS FINE 


      SqlCommand picCount = new SqlCommand(photoCountQuery, con);// THIS WORKS FINE 
      picCount.Parameters.AddWithValue("@albumID", albumId);// THIS WORKS FINE 
      int photoCount = Convert.ToInt16(picCount.ExecuteScalar());// THIS WORKS FINE 

      int start = (int)ViewState["start"];// THIS WORKS FINE 
      ViewState["pagesize"] = photoCount;// THIS WORKS FINE 

      //string query = "select * from gallery_photos where AlbumId='" + albumId + "'"; //// THIS WORKS FINE 
      string query = "select * from gallery_photos where [email protected]";// THIS is the problem. It does not work when i use command paramters 
      SqlCommand cmd = new SqlCommand(query, con); 
      cmd.Parameters.AddWithValue("@IdAlbum", albumId); 
      SqlDataAdapter da = new SqlDataAdapter(query, con); 

      da.Fill(ds, start, (int)(ViewState["pagesize"]), "gallery_photos"); 


     } 
     catch (Exception ex) 
     { 
      ExceptionUtility.LogException(ex, "Exception in " + Path.GetFileName(Page.AppRelativeVirtualPath).ToString()); 
      Context.ApplicationInstance.CompleteRequest(); 
     } 
     finally 
     { 
      con.Close(); 
     } 
     return ds; 
    } 

回答