Groups | Blog | Home
all groups > vb.net data > june 2005 >

vb.net data : USING SQL COMMAND TO DELETE A RECORD IN VB.NET APPLICATION


FLORENCE EWURUJE
6/16/2005 8:35:39 AM
Sir,

I am new to programming in vb.net. I am trying to develop a simple application to contain Add, Find, Update, Delete Record command button on the GUI. I have been able to add new record and also use the Find Command sucessfully but I have been having a problem with the Update and Delete command. I would appreciate your kind assistance please. The following error is what I see if I type 1 as the ID no and press delete, hoping that it will delete the entire row content:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.

Also find below my codes


Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Try
'make sure user has already found record to update
If txtID.Text <> "" Then

'set SQL query to update all fields in table where id number matches id in idTextBox

Me.SqlDataAdapter1.UpdateCommand.CommandText = _
"UPDATE address SET firstName=" & txtFirst.Text & " , " & _
"Lastname=" & txtLast.Text & " , " & _
"Address=" & txtAddress.Text & " , " & _
"State=" & cboState.Text & " , " & _
"Country=" & cboCountry.Text & " , " & _
"Married=" & radMarried.Text & " , " & _
"Basic=" & txtBasic.Text & " , " & _
"WHERE id=" & txtID.Text & "; "

'notify user that query is being sent
txtMessage.Text &= vbCrLf & "Sending query: " & _
Me.SqlDataAdapter1.UpdateCommand.CommandText & vbCrLf

'execute query
' SqlDataAdapter1.UpdateCommand.ExecuteNonQuery()
txtMessage.Text &= vbCrLf & "Query Successful" & vbCrLf

'prompt user to input existing record
Else
txtMessage.Text &= vbCrLf & _
"You may only update an existing record. " & _
"Use Find to locate the record. then " & _
"modify the information and press Update." & vbCrLf

End If

' display verbose information when database exception
Catch oleDbExceptionParameter As _
System.Data.OleDb.OleDbException

Console.WriteLine(oleDbExceptionParameter.StackTrace)
txtMessage.Text &= oleDbExceptionParameter.ToString
End Try

End Sub 'cmdUpdate_Click



Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
If txtID.Text = 0 Then
MessageBox.Show("Record Does Not Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
ClearTextBoxes()
Return
End If

Me.SqlDeleteCommand1.CommandText = "DELETE FROM address WHERE IDNo = @Original_IDNo"
‘Me.SqlDeleteCommand1.CommandText = "DELETE FROM address WHERE (IDNo = Me.txtID.Text)"
Me.SqlDeleteCommand1.Parameters("@Original_IDNo").Value = Me.txtID.Text
Me.SqlConnection1.Open()
Me.SqlDataAdapter1.DeleteCommand.ExecuteNonQuery()
Me.txtMessage.Text = "Delete was successful"
Me.SqlConnection1.Close()
End Sub
End Class ' FrmAddressBook

Thanks and God bless you.



From http://www.developmentnow.com/g/40_2004_8_0_0_0/dotnet-languages-vb-data.htm

Posted via DevelopmentNow.com Groups
William (Bill) Vaughn
6/16/2005 12:19:50 PM
Ah, some of the people that help here are women...
Put a Try/Catch block in the routine that executes the query. This will help
determine the real cause of the exception.
I would also do some reading about how to use a DataAdapter... your approach
is a bit strange. Most of the books (mine included) can save you a lot of
time--they are well worth the investment.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
www.sqlreportingservices.net
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________


[quoted text, click to view]

AddThis Social Bookmark Button