将行重复绑定到数据库中

时间:2016-09-19 07:28:18

标签: c# asp.net

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

1 个答案:

答案 0 :(得分: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 Name=@Name and RoleID=@RoleID and IsActive=@IsActive)
begin      
  insert into Roles values(@Name, @RoleID, @IsActive) 
end 


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

如果条件不同,则更改Roles表的列名称。