Click here to Skip to main content
16,017,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Friends,

I am trying to delete records from my table but unable to do so. I have window form with Insert, Update, Delete and Save button. I can insert data but I can't delete specific or any records from that table. Below is my code.

Whenever I click 'delete' button I get ("Error while deleting record on table..." & ex.Message, "Delete Records") msgbox only.

Am I missing something? Please advise. Any help would be great. Thank you!

Private Sub Deletebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Deletebtn.Click

        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Try
            con.ConnectionString = "Server=USRAG-L-0067215\SQLEXPRESS;Database=Alamo Products_Design Data;Trusted_Connection=True"
            con.Open()
            cmd.Connection = con
           cmd.CommandText = "Delete From Design_ParameterswhereDesignID=@DesignID "
            cmd.Parameters.Add(New SqlParameter("@DesignID", " Am I missing any value here?"))
            cmd.ExecuteNonQuery()
            If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then

                MsgBox("Operation Cancelled")

                Exit Sub

            End If
        Catch ex As Exception
            MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")

        Finally

            con.Close()
        End Try
    End Sub
End Class
Posted
Updated 10-Dec-12 10:58am
v8
Comments
Richard C Bishop 10-Dec-12 16:26pm    
I would suggest putting ' ' around your question mark.

"Delete From Design_Parameters Where DesignID = '?' "
Joy1979 10-Dec-12 16:29pm    
Yes, I agree but it will only return value with '? ' correct? where I need to find records by 'ID'. Thanks
Richard C Bishop 10-Dec-12 16:30pm    
So is the "?" not the value you are looking for to delete?
Joy1979 10-Dec-12 16:51pm    
No, I need to delete records by 'DesignID'. user put their order number to form and that order number inserted to 'DesignID' column. If I pick any designID number then I should be able to delete it. Thanks
Richard C Bishop 10-Dec-12 16:59pm    
Ok, so just use a variable with the value of the DesignID instead of the "?". Is your problem that you don't know how to get the ID you want as the variable?

1 solution

What the '?' stands for in your CommandText. If it is an unnamed parameter, please see "How to set unnamed SQL parameters in SqlCommand"[^]. Moreover where such parameter is set in your code?
 
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