|
-
Mar 28th, 2003, 12:51 AM
#1
Thread Starter
Junior Member
simple error
i am trying to loop into the dataset .Here is the vb.net code and error could anybody solve this simple problem
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnConnection As OleDbConnection
Dim adAdapter As OleDbDataAdapter
Dim dataset1 As New DataSet()
Dim strConnection As String
Dim strSQL As String
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CAFE\Database\UserDatail.mdb;Persist Security Info=False"
strSQL = "select * from mydata"
cnConnection = New OleDbConnection(strConnection)
adAdapter = New OleDbDataAdapter(strSQL, cnConnection)
adAdapter.Fill(dataset1)
Dim rowfirstName As System.Data.DataRow
'here is the error
For Each rowfirstName In dataset1.Tables("mydata").Rows
Console.WriteLine(rowfirstName("Firstname"))
Next
'error
txtName.Text = dataset1.Tables(0).Rows(0).Item(0)
End Sub
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe
Additional information: Object reference not set to an instance of an object.
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
-
Mar 28th, 2003, 03:00 AM
#2
Fanatic Member
well
use Try...
Catch
end try for correct and clear error message.... and use to string
Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cnConnection As OleDbConnection
Dim adAdapter As OleDbDataAdapter
Dim dataset1 As New DataSet()
Dim strConnection As String
Dim strSQL As String
Try
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\CAFE\Database\UserDatail.mdb;Persist Security Info=False"
strSQL = "select * from mydata"
cnConnection = New OleDbConnection(strConnection)
adAdapter = New OleDbDataAdapter(strSQL, cnConnection)
adAdapter.Fill(dataset1)
Dim rowfirstName As System.Data.DataRow
'here is the error
For Each rowfirstName In dataset1.Tables("mydata").Rows
Console.WriteLine(rowfirstName("Firstname").ToString)
Next
'error
txtName.Text = dataset1.Tables(0).Rows(0).Item(0)
catch ex as Exception
msgbox ex.message
end try
End Sub
-
Mar 28th, 2003, 03:08 AM
#3
Frenzied Member
Change the line
as follows:
VB Code:
adAdapter.Fill(dataset1, "mydata")
'Heading for the automatic overload'
Marillion, Brave, The Great Escape, 1994
'How will WE stand the FIRE TOMORROW?'
Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|