Click here to Skip to main content
16,010,416 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Error near WHERE clause is coming.
This is my code. please check it.
C#
protected void Button3_Click(object sender, EventArgs e) 
{     
    foreach(GridViewRow ro in GridView2.Rows)
    {
        foreach (GridViewRow row in GridView1.Rows)
        { 
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label Role = (ro.Cells[1].FindControl("Role_Name") as Label);
                CheckBox chkAdd = (row.Cells[2].FindControl("CheckBox1") as CheckBox);
                CheckBox chkEdit = (row.Cells[2].FindControl("CheckBox2") as CheckBox); 
                CheckBox chkView = (row.Cells[2].FindControl("CheckBox3") as CheckBox); 
                CheckBox chkDelete = (row.Cells[2].FindControl("CheckBox4") as CheckBox); 
                string strID = row.Cells[0].Text; 
                string Pages = row.Cells[1].Text;
                string Role_Name = ro.Cells[2].Text;
                con.Open();
                string query = "Insert Into  Role1  ([Role_Name],[Add],[View],[Edit],[Delete]) values('"+Role_Name+"','"+(chkAdd.Checked== true?'Y':'N')+"','"+(chkView.Checked==true?'y':'N')+"' ,'"+(chkEdit.Checked==true?'y':'N')+"' ,'"+(chkDelete.Checked==true?'y':'N')+"') WHERE ID='" +strID + "' and Pages ='"+Pages+"'" ; 
                SqlCommand cmd = new SqlCommand(query, con); 
                cmd.ExecuteNonQuery(); 
                BindGrid();
                
                con.Close();
              
            } 
        }
    }
}
Posted
Updated 28-Mar-14 0:09am
v2

INSERT statements do not have a WHERE clause - they do not affect existing rows, they always create a new rows instead.

You either need to remove the WHERE clause completely, or use an UPDATE query instead, which will change all rows that match the WHERE clause, and do not create any new rows.
 
Share this answer
 
I think you want to update record and not insert a new use update query not insert
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900