2011-12-22 1 views
0

내 페이지에 Gridview가 있습니다 어느 행을 선택할 수 있습니까? Gridview에서 행을 선택하고 `단순 테이블에서 삭제하지만이 오류가 있습니다 "스케이프 변수"@Senduserid "를 선언해야합니다"

public bool Delete(int Id) 
{ 
    using (SqlCommand cmd = new SqlCommand()) 
    { 
     string text = string.Format("Delete From {0} where {1} = @{1} or {2} [email protected]{2}" 
             , Common.Data.MessageInfo.TableName 
             , Common.Data.MessageInfo.SenduseridField 
             ,Common.Data.MessageInfo.RecuseridField); 
     cmd.CommandText = text; 
     SqlParameter param = new SqlParameter("@" + Common.Data.MessageInfo.SenduseridField, Id); 
     param.SqlDbType = SqlDbType.Int; 
     param = new SqlParameter("@" + Common.Data.MessageInfo.RecuseridField, Id); 
     param.SqlDbType = SqlDbType.Int; 
     cmd.Parameters.Add(param); 
     cmd.Connection = this.GetConnection(); 
     cmd.Connection.Open(); 
     cmd.ExecuteNonQuery(); 
     return true; 
    } 
} 
: 여기에 나는이 오류가있어 장소의 삭제 버튼 코드

protected void btnMultipleRowDelete_Click(object sender, EventArgs e) 
{ 
    // Looping through all the rows in the GridView 

    foreach (GridViewRow row in GridView1.Rows) 
    { 
     CheckBox checkbox = (CheckBox)row.FindControl("chkRows"); 

     //Check if the checkbox is checked. 
     //value in the CheckBox's Value property is set as the //value of the delete command's parameter. 

     while (checkbox.Checked) 
     { 
      // Retreive the ID 
      int ida = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value); 

      // Pass the value of the selected Employye ID to the Delete 
      //These numbers indicate in which order tables shoulde be deleted 
      /*1*/ 
      new BLL.LoginBLL().Delete(ida); 
      /*2*/ 
      new BLL.MessageBLL().Delete(ida); 
      /*3*/ 
      new BLL.JointBLL().Delete(ida);      
      /*4*/ 
      new BLL.Tempprice().Delete(ida);      
      /*5*/ 
      new BLL.LotsBLL().Delete(ida); 
      /*6*/ 
      new BLL.AuctionBLL().Delete(ida); 
      /*7*/ 
      new BLL.ProfileBLL().DeleteProfile(ida); 

      checkbox.Checked = false; 
     } 
     //refresh the Gridview rows 
     ShowUsers(); 
    } 
} 

그리고 코드의이 오류 가 @Senduserid "" "스칼라 변수를 선언해야"삭제 버튼 igot 클릭

데이터베이스 관계

DataBase Relationship

답변

2

당신은 두 개의 매개 변수를 초기화하지만, 명령에 첫 번째 매개 변수를 추가하지 마십시오. recuseridfield 매개 변수를 인스턴스화하기 전에 senduseridfield 매개 변수를 명령의 매개 변수 목록에 추가하십시오.

+0

나는 바보입니다. –

1
cmd.CommandText = text;    
SqlParameter param = new SqlParameter("@" + Common.Data.MessageInfo.SenduseridField, Id);    
param.SqlDbType = SqlDbType.Int;   
cmd.Parameters.Add(param);    // you over looked this 

param = new SqlParameter("@" + Common.Data.MessageInfo.RecuseridField, Id);    
param.SqlDbType = SqlDbType.Int;    
cmd.Parameters.Add(param);    

cmd.Connection = this.GetConnection();    
cmd.Connection.Open(); 
+0

감사합니다. –

0

Gridview 컨트롤의 DataKeyNames 속성을 지정해야합니다.

DataKeyNames="Senduserid"