Click here to Skip to main content
15,908,173 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi All,

I want to delete a sql table record using C#. I already tried the below mentioned code snippet but it didn't work. However the message is showing 'Data Record Deleted!' as it should it be. I can't find an issue in the statement also.

What I have tried:

C#
private void btnDeleteData_Click(object sender, EventArgs e)
       {
           try
           {

               SqlComm = new SqlCommand("DELETE FROM MyDataTable WHERE DataID='@DataID'", SqlConn);
               SqlComm.Parameters.AddWithValue("@DataID", txtDataID.Text);
               SqlComm.ExecuteNonQuery();
               MessageBox.Show("Data record deleted!", "DB Connection With App.Config", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

               Clear();
               DisableButtons();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


Could someone please try to solve this matter?

Thanks and regards,
Chiranthaka
Posted
Updated 2-Feb-20 0:25am

The code looks OK, so the most likely thing is that the WHERE clause matches no rows.
Start by checking how many rows the command is affecting: the ExecuteNonQuery returns that:
C#
SqlComm.Parameters.AddWithValue("@DataID", txtDataID.Text);
int deleted = SqlComm.ExecuteNonQuery();
MessageBox.Show(string.Format("{0} rows deleted.", deleted), "DB Connection With App.Config", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
If that indicates zero, then use the debugger to check exactly what is in txtDataId and manually check the DB for exact matches using SSMS.
 
Share this answer
 
Comments
Chiranthaka Sampath 20-Aug-17 2:41am    
Hi OriginalGriff,

Thanks for the snippet. You were correct my statement was not affecting the Table. So that I have looked into the SQL statement again & found that the issue is with it. Below is the updated SQL statement.

SqlComm = new SqlCommand("DELETE FROM MyDataTable WHERE DataID LIKE @DataID", SqlConn);

Thanks for your help!
OriginalGriff 20-Aug-17 3:28am    
You're welcome!
add this: SqlComm.CommandType = CommandType.Text after SqlComm.Parameters.AddWithValue("@DataID", txtDataID.Text). this should solve your probelm.
 
Share this answer
 
Comments
CHill60 26-Nov-19 3:55am    
That is the default anyway so this would not have solved the OPs problem - that was solved by adjusting the WHERE clause - see the comments to Solution 1 from August 2017
Hi. I had the same issue. My solution: String DelQuery = "DELETE FROM Tutor where Username=@UserName";

So i removed the '' from '@UserName'.

Works Perfectly now!
 
Share this answer
 
Comments
CHill60 3-Feb-20 4:05am    
Firstly there is no "from '@UserName'" in your query. If you meant that you removed "where Username=@UserName" then you will be deleting the entire contents of your Tutor table. Your problem was probably the same as the OPs - there was nothing matching in @UserName therefore there was nothing to delete.
All you have done here is demonstrate that you don't know how "WHERE" clauses work.
Member 14022627 30-Mar-22 15:42pm    
@Chill60 I'd ask you to please read the etiquette for the forum at the bottom of the page. We are not supposed to insult people in the forums.

Number 2, you were also incorrect about the issue with Solution 3. While the person who posted the solution did not give an answer to the OP's question, their answer was not about an issue with the WHERE clause. The member 14732900 stated they removed the single quotes ('') from '@UserName'. It is obvious if you review the post they meant they had an original string such that: String DelQuery = "DELETE FROM Tutor where Username='@UserName'"; and modified it to: String DelQuery = "DELETE FROM Tutor where Username=@UserName";. While the etiquette does not state one should not insult AND one should do their best to make correct statements when commenting, I believe it is implied.

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