Results 1 to 3 of 3

Thread: simple error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2002
    Location
    Mumbai
    Posts
    31

    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.

  2. #2
    Fanatic Member khalik_ash's Avatar
    Join Date
    Aug 2002
    Location
    Singapore
    Posts
    724
    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

  3. #3
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Change the line
    VB Code:
    1. adAdapter.Fill(dataset1)
    as follows:
    VB Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width