Data Access - Data Entry Form
Hi everyone!
I am just starting to work with VB.NET and I need your help!
I am trying to do a Data Entry form that has the following controls: 1 data time picker, 1 autonumber, 3 texboxes, 1 button
This form will capture the data on these fields and will put it in an SQL database. I am using Sql Server 2000.
The code, which doesn't work because I get a System.Null.Reference Exception is as follows:
Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
'Setup the database objects
Dim objConnection As New SqlClient.SqlConnection()
objConnection.ConnectionString = "server=MyServer;" & _
"database=IncomingMail;user ID=sa;password=open;"
Dim objCommand As New SqlClient.SqlCommand("", objConnection)
Dim objTransaction As SqlClient.SqlTransaction
Dim strSQL As String
Try
'Open connection and begin transaction
objConnection.Open()
objTransaction = objConnection.BeginTransaction
'Make the Command object aware it is in this transaction
objCommand.Transaction = objTransaction
'Generate SQL statement and execute to
'add a new mail transaction to the IncomingMail table
strSQL = "INSERT INTO IncomingMail(Recipient, Sender, Contents, _ DateReceived)" & "VALUES('" & txtRecipient.Text & "', '" & _
txtSender.Text & "' , '" & txtSubject.Text & "', '" & _
dtpDateTimePicker1.Value & "')"
objCommand.CommandText = strSQL
objCommand.ExecuteNonQuery()
'Retrieve the RecipientID for our new recipient, the automatically
'generated identity value
Dim intAutoNumber As Integer
strSQL = "SELET @@Identity"
objCommand.CommandText = strSQL
intAutoNumber = objCommand.ExecuteScalar
'Commit successful transaction, close connection
objTransaction.Commit()
objConnection.Close()
Catch
btnEnter.Text = "Cannot Add Mail Transaction!"
' objTransaction.Rollback()
objConnection.Close()
End Try
End Sub
I have hardcoded the id and password because I don't know how to do a login window.
Thank you for your help - please be gentle - I am just starting to program!!
Thanks again,
Tere