Mouad Raizada
Mouad Raizada

Reputation: 127

how to delete a row in a table acording to a value coming from another form in C#

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

Answers (2)

Subash B
Subash B

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

Balagurunathan Marimuthu
Balagurunathan Marimuthu

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

Related Questions