Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to delete a record in sql with c# , it's my code but it has problem :
C#
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection s = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\mm\Documents\Visual Studio 2008\Projects\LAS\LAS\mm.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string delstring = textBox1.Text;
            SqlCommand com = new SqlCommand("DELETE bookInfo WHERE bookName=" + delstring, s);
            s.Open();
            com.ExecuteNonQuery();
            s.Close();
        }

please tell me how can I solve it's problem.
Posted
Updated 26-Feb-11 1:15am
v2

If it gives you an error, that is normally a clue.
If it doesn't, then you need to look at your data.

The chances are it is your data that is the problem. Does your book name contain any spaces or punctuation? If so, then it will muck up the SQL command. To avoid that (and accidental or deliberate SQL Injection attacks) use parametrized queries instead:
C#
private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection s = new SqlConnection(@"DataSource=.\SQLEXPRESS;AttachDbFilename=C:\Users\mm\Documents\Visual Studio 2008\Projects\LAS\LAS\mm.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
            string delstring = textBox1.Text;
            SqlCommand com = new SqlCommand("DELETE bookInfo WHERE bookName=@BN", s);
            com.AddWithValue("@BN", delstring);
            s.Open();
            com.ExecuteNonQuery();
            s.Close();
        }
 
Share this answer
 
Comments
Sandeep Mewara 26-Feb-11 5:58am    
His query is wrong.. just posted it. :)
Espen Harlinn 26-Feb-11 9:34am    
My 5 for using: com.AddWithValue("@BN", delstring);
I guess that's what you actually wanted to show :)
Don't you think you missed something here:
("DELETE bookInfo WHERE bookName=" + delstring, s);

Which table you are referring to?
Try:
("DELETE FROM bookInfo WHERE bookName=" + delstring, s);
Assuming, 'bookInfo' is the table name.


UPDATE:
I also notice that bookname value is not surrounded by a quote here. It looks like a string, varchar in DB it should be and thus quotes would be needed.

Thus, try:
("DELETE FROM bookInfo WHERE bookName='" + delstring+"'", s);
 
Share this answer
 
v2
Comments
OriginalGriff 26-Feb-11 6:01am    
Good point! I missed that - I suspect it may be a combination of the two...:laugh:
Sandeep Mewara 26-Feb-11 6:03am    
:)
Sandeep Mewara 26-Feb-11 6:07am    
BTW, I just noticed you too crossed 100K barrier. Congrats! :)
OriginalGriff 26-Feb-11 7:10am    
Thanks! I hadn't noticed myself...:blush:
mehdi_k 26-Feb-11 6:14am    
Even with this code the problem message is "Invalid column name".
please help!

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