Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
kindly help me when i tried to delete message show of success but in reality it not perform action of deletion

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.Connection = Con
Cmd = New SqlCommand
Cmd.CommandText = "DELETE FROM fuel Where Fuel_Id='" & txtfillingid.Text & "', GenID='" & txtGeneratorId.Text & "', F_date='" & txtDate.Text & "', S_F_reading='" & txtBeforefilling.Text & "', E_F_reading='" & txtAfterfilling.Text & "', Total_Feul='" & TxtTottal.Text & "'"
lblmessage.Text = "Your data has succesfully deleted"

'For Each p As SqlParameter In Cmd.Parameters
' Cmd.Parameters.Remove(p)
'Next
Catch ex As Exception
lblmessage.Text = ex.ToString

Finally
Con.Close()
End Try

btnInsert.Visible = False
End Sub
Posted
Updated 26-May-16 1:09am
Comments
Mahar Tanvir 26-May-16 7:53am    
is it correct form to use to build sql Command with connection string?
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)

1 solution

Firstly, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

And second, comma is not a WHERE condition conjunction: you need AND or OR:
SQL
DELETE FROM MyTable WHERE columnName1=value AND columnName2=otherValue
Or
SQL
DELETE FROM MyTable WHERE columnName1=value ORcolumnName2=otherValue

Finally, you need to execute the command:
VB
Cmd.ExecuteNonQuery()
 
Share this answer
 
Comments
Mahar Tanvir 26-May-16 7:44am    
Sir as You above says that"Do not concatenate strings to build a SQL command"
kindly told me about connection and command how i should do it

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