2016-09-19 56 views
0

screenshot綁定行多次到數據庫

當我插入@RoleID@IsActive@PermissionRoleID這也不斷插入。我想要這3列第一次只有下一個不會。我怎樣才能做到這一點?

protected void Add_Click(object sender, EventArgs e) 
{ 
    foreach (GridViewRow row in addGridView.Rows) 
    { 
     if (row.RowType == DataControlRowType.DataRow) 
     { 
      CheckBox chkRow = (row.Cells[1].FindControl("chkboxaddView") as CheckBox); 
      CheckBox chkRow1 = (row.Cells[2].FindControl("chkboxaddEdit") as CheckBox); 
      string aaa = row.Cells[0].Text; 

      if (chkRow.Checked) 
      { 
       con.Open(); 

       var View = chkRow.Checked ? 1 : 0; 
       var Edit = chkRow1.Checked ? 1 : 0; 
       var addchkboxisactive = chkaddisactive.Checked ? 1 : 0; 
       var editaddchkboxisactive = chkboxisactive.Checked ? 1 : 0; 

       string sqlcmd = "select AccesspageID From AccessPages where pages='" + row.Cells[0].Text + "'"; 

       SqlCommand cmdd = new SqlCommand(sqlcmd, con); 
       cmdd.CommandType = CommandType.Text; 

       var result = cmdd.ExecuteScalar(); 

       string query = "add3tables";   // Stored procedure name 

       SqlCommand com = new SqlCommand(query, con); //creating SqlCommand object 
       com.CommandType = CommandType.StoredProcedure; //here we declaring command type as stored Procedure 

       // adding paramerters to SqlCommand below 
       com.Parameters.AddWithValue("@Name", txtaddrolename.Text.ToString()); 
       string unique_string = Guid.NewGuid().ToString(); 
       com.Parameters.AddWithValue("@RoleID ", unique_string); 
       com.Parameters.AddWithValue("@IsActive ", addchkboxisactive); 
       com.Parameters.AddWithValue("@PermissionRoleID ", unique_string); 
       com.Parameters.AddWithValue("@View ", View); 
       com.Parameters.AddWithValue("@Edit ", Edit); 
       com.Parameters.AddWithValue("@PermissionAccessoageID ", result); 

       com.ExecuteNonQuery(); 
       con.Close(); 
      } 

      if (chkRow1.Checked == false) 
      { 
      } 
     } 
    } 

    this.ModalPopupExtender2.Hide(); 
    BindGridview(); 
} 

下面是存儲過程:

ALTER PROCEDURE add3tables 
    @Name nvarchar(50), 
    @RoleID nvarchar(50), 
    @PermissionRoleID nvarchar(50), 
    @IsActive bit, 
    @View bit, 
    @Edit bit, 
    @PermissionAccessoageID nvarchar(100) 
AS 
BEGIN 
    insert into Roles values(@Name, @RoleID, @IsActive) 

    insert into AllowPermission 
    values(@PermissionRoleID, @View, @Edit, @PermissionAccessoageID) 
END 
+0

請更好地解釋您的要求 – Steve

+0

當我插入數據到數據庫。我希望這些(@ RoleID,@ IsActive,@ PermissionRoleID)第一次只有3列應該插入到數據庫中,下次不應該插入這3列到數據庫中。我怎麼能做到這一點。 – Vinoth

+0

在這種情況下,存儲過程非常沒用。刪除它並直接在代碼中執行查詢。用這種方法,你可以用一個簡單的變量區分第一個循環和下一個循環。 – Steve

回答

0

更換步驟對

ALTER PROCEDURE add3tables 
    @Name nvarchar(50), 
    @RoleID nvarchar(50), 
    @PermissionRoleID nvarchar(50), 
    @IsActive bit, 
    @View bit, 
    @Edit bit, 
    @PermissionAccessoageID nvarchar(100) 
AS 
BEGIN 

    IF Not EXISTS (SELECT * FROM Roles 
        WHERE [email protected] and [email protected] and [email protected]) 
begin  
    insert into Roles values(@Name, @RoleID, @IsActive) 
end 


    insert into AllowPermission 
    values(@PermissionRoleID, @View, @Edit, @PermissionAccessoageID) 
END  

在如果條件角色表中的變化的列名,如果不同。