0
当我插入@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
请更好地解释您的要求 – Steve
当我插入数据到数据库。我希望这些(@ RoleID,@ IsActive,@ PermissionRoleID)第一次只有3列应该插入到数据库中,下次不应该插入这3列到数据库中。我怎么能做到这一点。 – Vinoth
在这种情况下,存储过程非常没用。删除它并直接在代码中执行查询。用这种方法,你可以用一个简单的变量区分第一个循环和下一个循环。 – Steve