Reputation: 127
I want to delete a row in my FormClosing event of mainFrm form the value of where condition that deletes the row is coming from another form which is contained by a textBox
the codes of first form which has to transform the value to the next form are
id = Convert.ToInt32(userNametxt.Text);
MainFrm mainfrm = new MainFrm(id);
mainfrm.Show();
this.Hide();
The codes for the constructor of the second form are:
public MainFrm(int id)
{
InitializeComponent();
deleteById = id;
}
The codes of FormClosing event in the second form are:
SqlCommand cmd = new SqlCommand("delete empId,empPerm from empLogin where empId="+deleteById+"", cn);
cmd.ExecuteNonQuery();
cn.Close();
The id variable is declared above the constructor ... the exception is "incorrect syntax near',' "any help !!
Upvotes: 1
Views: 53
Reputation: 164
You can't delete a row by using this code.
SqlCommand cmd = new SqlCommand("delete empId,empPerm from empLogin where empId="+deleteById+"", cn);
Instead of using this code, you can use this
SqlCommand cmd = new SqlCommand("delete from empLogin where empId="+deleteById+"", cn);
If you want to delete a data from that column,you should use update query like this:
SqlCommand cmd = new SqlCommand("update empLogin set empPerm='' where empId="+deleteById+"", cn);
Upvotes: 1
Reputation: 2978
The exception clearly said that Incorrect syntax near ','
Your delete statement is incorrect. Correct delete statement is:
DELETE FROM empLogin WHERE empId=" + deleteById
Upvotes: 0