An unhandled exception of type 'System.NullReferenceException' occurred in system.dat

supro

New member
Joined
May 17, 2007
Messages
1
Programming Experience
Beginner
Dim DataTable As DataTable

Dim currRec, totalRec As Integer

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles btnSave.Click

Dim objConnection As SqlConnection = New SqlConnection("user id=sa;Password=jimwest;data source=SUPRO;persist security info=False;initial catalog=pubs")

Dim objCommand As New SqlCommand

Dim ds As New DataSet
objCommand.Connection = objConnection

objCommand.CommandText = "select au_id,au_lname,au_fname from authors"


Dim dataAdaptor As New SqlDataAdapter
dataAdaptor.SelectCommand = objCommand

dataAdaptor.SelectCommand.Connection = objConnection

dataAdaptor.Fill(ds, "auth")

DataTable = ds.Tables("prog")

currRec = 0

totalRec = DataTable.Rows.Count

txtId.Text = DataTable.Rows(currRec)("au_id").ToString()

txtLast.Text = DataTable.Rows(currRec)("au_lname").ToString()

txtFirst.Text = DataTable.Rows(currRec)("au_fname").ToString()


End SubDim DataTable As DataTable

Dim currRec, totalRec As Integer

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs ) Handles btnSave.Click

Dim objConnection As SqlConnection = New SqlConnection("user id=sa;Password=jimwest;data source=SUPRO;persist security info=False;initial catalog=pubs")

Dim objCommand As New SqlCommand

Dim ds As New DataSet
objCommand.Connection = objConnection

objCommand.CommandText = "select au_id,au_lname,au_fname from authors"


Dim dataAdaptor As New SqlDataAdapter
dataAdaptor.SelectCommand = objCommand

dataAdaptor.SelectCommand.Connection = objConnection

dataAdaptor.Fill(ds, "auth")

DataTable = ds.Tables("auth")

currRec = 0

totalRec = DataTable.Rows.Count

txtId.Text = DataTable.Rows(currRec)("au_id").ToString()

txtLast.Text = DataTable.Rows(currRec)("au_lname").ToString()

txtFirst.Text = DataTable.Rows(currRec)("au_fname").ToString()


End Sub

I have already checked with the SQl server logins and right..Everything is okay..A similar code works on my friends machine..what could be the reason
 
Last edited:
Public Function CheckNull(ByVal objVal As Object, Optional ByVal objDefault As Object = "") As Object

If (objVal Is System.DBNull.Value) Then
Return objDefault
Else
Return objVal
End If
End Function


'Call using this
txtId.Text = CheckNull(DataTable.Rows(currRec)("au_id").ToString())
 
Back
Top