Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Please help me how to do it
i tried to delete record from by textbox but it failed and result is "0Your data has successfully deleted"

What I have tried:

Protected Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click

Con = New SqlConnection("Data Source=VU-IT-INTERN1;Initial Catalog=fuelsystem;User ID=sa;Password=123")

Try
Con.Open()
Cmd = New SqlCommand("DELETE FROM fuel Where Fuel_Id='" & txtfillingid.Text & "' AND GenID='" & txtGeneratorId.Text & "' AND F_date='" & txtDate.Text & "' AND S_F_reading='" & txtBeforefilling.Text & "' AND E_F_reading='" & txtAfterfilling.Text & "' AND Total_Feul='" & TxtTottal.Text & "'", Con)
'Cmd.CommandText = "Delete from Fuel where Fuel_id='FL0001'"
lblmessage.Text = Cmd.ExecuteNonQuery().ToString & "Your data has succesfully deleted"
Catch ex As Exception
lblmessage.Text = ex.ToString

Finally
Con.Close()
End Try

btnInsert.Visible = False
End Sub
Posted
Updated 26-May-16 4:09am
Comments
pt1401 26-May-16 8:10am    
Does the WHERE clause return any records?
Does it return the record(s) that you were intending to delete?
Try changing the DELETE to SELECT * and running in in SQL Management Studio to confirm.

A side issue is PLEASE don't construct SQL like this, it leaves your code wide open to SQL injection attacks. It may just a noddy program for your personal use, but it's a very bad and dangerous habit to get into. Use parameterised queries instead.
Karthik_Mahalingam 26-May-16 8:20am    
you shall post this as Solution.
Richard MacCutchan 26-May-16 10:38am    
Why are you posting the message "Your data has succesfully deleted", without checking that the delete command has actually succeeded correctly? This is the third time today I have seen similar coding. I can only assume you all go to the same school, and the teacher knows nothing about software development. The alternative reason is too terrifying to contemplate.

When you posted this last time: Help me to do action at delete[^]
I gave you three reasons not to do that: and you have implemented two of them.
You've left your code wide open to SQL Injection still.

And the response says it has done exactly what you asked it to: the "0" in front of "Your data has successfully deleted" indicates that zero rows have been affected - which means that no rows exactly matched your criteria.

Either your data isn't what you think it is, or your criteria is wrong. Simple as that.
 
Share this answer
 
Reposted as solution by OP request:-

Does the WHERE clause return any records?
Does it return the record(s) that you were intending to delete?
Try changing the DELETE to SELECT * and running it in SQL Management Studio to confirm.

A side issue is PLEASE don't construct SQL like this, it leaves your code wide open to SQL injection attacks. It may just a noddy program for your personal use, but it's a very bad and dangerous habit to get into. Use parameterised queries instead
 
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