Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB SQL-Server SQL-Server-2008 , +
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 10-Dec-12 11:19am
Joy1979316
Edited 10-Dec-12 11:58am
v8
Comments
richcb at 10-Dec-12 16:26pm
   
I would suggest putting ' ' around your question mark.
 
"Delete From Design_Parameters Where DesignID = '?' "
Joy1979 at 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
richcb at 10-Dec-12 16:30pm
   
So is the "?" not the value you are looking for to delete?
Joy1979 at 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
richcb at 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?
Joy1979 at 10-Dec-12 17:05pm
   
Yes Sir, Correct. Thanks!
richcb at 10-Dec-12 17:15pm
   
Ok, looks like DinoRondelly summed it up good with the post below. The "@DesignID" is what you are looking for.
Joy1979 at 11-Dec-12 9:53am
   
Thx for your help.
 
cmd.CommandText = "Delete From Design_Parameters where DesignID = @DesignID "
cmd.Parameters.Add(New SqlParameter("@DesignID", TextBox4.Text))
 
...I can delete records now. :)
richcb at 11-Dec-12 10:24am
   
Good, glad I could help.
DinoRondelly at 10-Dec-12 16:48pm
   
When you run
cmd.ExecuteNonQuery()
 
you are executing the query so to have you message box come up after this makes no sense because you are asking them if they are sure they want to delete.
 
What is ? supposed to be? I would imagine you want to have you id value there.
Joy1979 at 10-Dec-12 17:04pm
   
Hi, Thanks for your reply. Apologies If my question is not cleared. there is not value in table associated with "?". I have updated my question.
DinoRondelly at 10-Dec-12 17:07pm
   
read the link below
http://msdn.microsoft.com/en-us/library/yy6y35y8.aspx
 
Also
cmd.Parameters.Add(New SqlParameter("@DesignID", " Am I missing any value here?"))
 
Datatype????
 
Also Spacing
"Delete From Design_Parameters where DesignID = @DesignID "
DinoRondelly at 10-Dec-12 16:51pm
   
Also your quotes are messed up in your statement which is another reason this would fail
Joy1979 at 11-Dec-12 9:53am
   
Thx for your help.
 
cmd.CommandText = "Delete From Design_Parameters where DesignID = @DesignID "
cmd.Parameters.Add(New SqlParameter("@DesignID", TextBox4.Text))
 
...I can delete records now. :)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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?
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 260
1 Mathew Soji 244
2 Afzaal Ahmad Zeeshan 232
3 OriginalGriff 231
4 Maciej Los 230
0 OriginalGriff 6,419
1 Sergey Alexandrovich Kryukov 6,008
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,070


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 10 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100