Problem With "DELETE FROM"

Jnox

Active member
Joined
Apr 19, 2006
Messages
25
Programming Experience
Beginner
I have this following code. It looks okay, but it doesnt work. Could someone be able to help me solve the problem?

VB.NET:
                    Dim dbComm As SqlCommand
                    Dim DbCommand As SqlDataAdapter
                    Dim DbFill As New DataSet
                    Dim DbConn As New SqlConnection("uid=xxxx;pwd=xxxx;data source=LAPTOP\SQLEXPRESS;initial catalog=CheckWeigher")

                    Dim strOleDb As String

                    Dim strUserID As String
                    Dim strFirstName As String
                    Dim strLastName As String
                    Dim strTitle As String
                    Dim strPassword As String

                    strUserID = TextBox2.Text
                    strFirstName = TextBox3.Text
                    strLastName = TextBox4.Text
                    strTitle = TextBox10.Text
                    strPassword = TextBox5.Text

                    strOleDb = "DELETE FROM Users WHERE UserID=@UserID, " & "FirstName=@FirstName, LastName=@LastName, " & "Title=@Title, Password=@Password"

                    dbComm = New SqlCommand(strOleDb, DbConn)


                    dbComm.Parameters.Add("@UserID", strUserID)
                    dbComm.Parameters.Add("@FirstName", strFirstName)
                    dbComm.Parameters.Add("@LastName", strLastName)
                    dbComm.Parameters.Add("@Title", strTitle)
                    dbComm.Parameters.Add("@Password", strPassword)

                    DbConn.Open()
                    dbComm.ExecuteNonQuery()
                    DbConn.Close()
 
I am not sure but this
strOleDb = "DELETE FROM Users WHERE UserID=@UserID, " & "FirstName=@FirstName, LastName=@LastName, " & "Title=@Title, Password=@Password"

looks to me like an inconventional SQL statement (maybe i am wrong, ... i'll expect someone to correct me if i am)
However in my opinion it should look as it follows (standard SQL statement):
VB.NET:
strOleDb = "DELETE FROM Users WHERE UserID=@UserID AND FirstName=@FirstName AND LastName=@LastName AND Title=@Title AND Password=@Password"

btw, don't you think it is a lot conditions? Allowing duplicate UserID's or Password is not the best idea however which means that if you follow my advice then your DELETE statement would have only one or two conditions to check (PWD and ID).

Regards :)
 
I agree 100% with kulrom; to specify multiple conditions in ANY sql where clause, you use AND to connect the clauses

... WHERE a=b AND c=d AND ...

Additionally, your table should have a primary key which in this case I would guess is UserID; it is enough to run the delete statement with the primary key (the definition of a primary key is a column or subset of columns whose values are unique within the table, such that any row can be uniquely identified by just those column(s)):

VB.NET:
strUserID = TextBox2.Text
                    strFirstName = TextBox3.Text

                    strOleDb = "DELETE FROM Users WHERE UserID=@UserID"
                    dbComm = New SqlCommand(strOleDb, DbConn)

                    dbComm.Parameters.Add("@UserID", strUserID)

I am curious to know, if when you said "the code looks okay" - had you tried the DELETE command in this format in some kind of query analyzer? Did it run there ok?
 
Back
Top