Results 1 to 10 of 10

Thread: VS 2008 and Access 2003

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    23

    Angry VS 2008 and Access 2003

    dim Code:
    1. Imports System.Data.OleDb
    2.  
    3. Public Class highScore
    4.     Dim dbConnection As OleDb.OleDbConnection
    5.     Dim dbCommand As OleDb.OleDbCommand
    6.     Dim dbAdapter As OleDb.OleDbDataAdapter
    7.     Dim ConnectionString As String = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
    8.     "Data Source = SSADatabase.mdb"
    9.     Dim dtHighScore As DataTable
    10.     Dim SelectedData As String = "highScore"
    11.  
    12.     Private Sub getData()
    13.         dbConnection = New OleDb.OleDbConnection(ConnectionString)
    14.         dbAdapter = New OleDb.OleDbDataAdapter
    15.         dtHighScore = New DataTable
    16.         dbCommand = New OleDb.OleDbCommand(SelectedData)
    17.         dbCommand.CommandType = CommandType.TableDirect
    18.         dbAdapter.SelectCommand = dbCommand
    19.         dbAdapter.SelectCommand.Connection = dbConnection
    20.         dbConnection.Open()
    21.         dbAdapter.Fill(dtHighScore)
    22.         DataGridView.DataSource = dtHighScore
    23.     End Sub
    24.  
    25.     Private Sub highScore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    26.         getData()
    27.     End Sub
    28.  
    29.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    30.         Dim objRow As DataRow
    31.         Dim tableName As String = "highScore"
    32.         Dim objDataSet As New DataSet
    33.         If txtName.Text = "" Then
    34.             MsgBox("You need to fill out your name")
    35.             Exit Sub
    36.         End If
    37.         Dim newAdapter As New OleDb.OleDbDataAdapter("SELECT * FROM " & tableName & "", ConnectionString)
    38.         objDataSet.Clear()
    39.         newAdapter.FillSchema(objDataSet, SchemaType.Source, tableName)
    40.         newAdapter.Fill(objDataSet, tableName)
    41.         objRow = objDataSet.Tables(tableName).Rows.Find(objDataSet.Tables(tableName).Rows.Count + 1)
    42.         objRow.Item("Name") = txtName.Text   '' <----- This line here
    43.         objRow.Item("Score") = myTotalScore
    44.  
    45.     End Sub

    Note the last 3rd line i get this error when i try to save data
    Object reference not set to an instance of an object.
    It never happens to me before and it works on a different application that i made.

    My DB looks like this
    ID = auto number & primary no
    Name = Text
    Score = number

    What is going on???

    Please help me!

    T Chaiyaphan

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: VS 2008 and Access 2003

    It's the Find method that failed - if nothing is returned then your objRow object won't be set:

    objRow = objDataSet.Tables(tableName).Rows.Find(objDataSet.Tables(tableName).Rows.Count + 1)


    Run it in debug to see what's value you're searching for:

    objDataSet.Tables(tableName).Rows.Count + 1 <<< this value might not be there.


    Regardless you need Try - Catch block in your procedure to capture (and handle) exceptions.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    23

    Re: VS 2008 and Access 2003

    Yes you are right it returns nothing on:
    vb Code:
    1. objRow = objDataSet.Tables(tableName).Rows.Find(objDataSet.Tables(tableName).Rows.Count + 1)

    objRow = "Nothing"
    How can i fix this?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    23

    Re: VS 2008 and Access 2003

    vb Code:
    1. Dim objRow As DataRow
    2.         Dim tableName As String = "highScore"
    3.         Dim objDataSet As New DataSet
    4.         If txtName.Text = "" Then
    5.             MsgBox("You need to fill out your name")
    6.             Exit Sub
    7.         End If
    8.         Try
    9.             dbAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & tableName & "", ConnectionString)
    10.             objDataSet.Clear()
    11.             dbAdapter.FillSchema(objDataSet, SchemaType.Source, tableName)
    12.             dbAdapter.Fill(objDataSet, tableName)
    13.             objRow = objDataSet.Tables(tableName).Rows.Find(objDataSet.Tables(tableName).Rows.Count + 1)
    14.             objRow.Item("Name") = txtName.Text
    15.             objRow.Item("Score") = myTotalScore
    16.             getData()
    17.         Catch ex As Exception
    18.             MsgBox(ex.Message)
    19.         End Try

    i get an error
    Object reference not set to an instance of an object

    While not? it is right isnt it

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    23

    Re: VS 2008 and Access 2003

    vb Code:
    1. Dim objRow As DataRow
    2.         Dim tableName As String = "highScore"
    3.         Dim objDataSet As New DataSet
    4.         If txtName.Text = "" Then
    5.             MsgBox("You need to fill out your name")
    6.             Exit Sub
    7.         End If
    8.         Try
    9.             dbAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & tableName & "", ConnectionString)
    10.             objDataSet.Clear()
    11.             dbAdapter.FillSchema(objDataSet, SchemaType.Source, tableName)
    12.             dbAdapter.Fill(objDataSet, tableName)
    13.             objRow = objDataSet.Tables(tableName).Rows.Add
    14.             Dim rowNum = objDataSet.Tables(tableName).Rows.Count
    15.             objRow = objDataSet.Tables(tableName).Rows.Find(rowNum)
    16.             objRow.Item(0) = rowNum
    17.             objRow.Item(1) = txtName.Text
    18.             objRow.Item(2) = myTotalScore
    19.             getData()
    20.         Catch ex As Exception
    21.             MsgBox(ex.Message)
    22.         End Try

    I dont get errors from doing this but when it doesnt save to database and objRow isnt null anymore

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: VS 2008 and Access 2003

    Well, I only pointed why you're getting that error and how you should be handling it but you'll have to determine why record you expected to be in the table isn't there on your own - you are the one who should know your database best.

  7. #7
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    Re: VS 2008 and Access 2003

    I tend not to use the DataAdapter myself, but it looks to me like you need to:

    A. Specify an INSERT command
    B. Call the dbAdapter.Update method after adding your new row.

    Unless you have controls bound to your dataset, you might want to examine whether the DataSet/DataAdapter approach is the most efficient.

    jmchilhinney has a number of posts in his code bank related to data access. Here is one:

    http://www.vbforums.com/showthread.php?t=469872

  8. #8
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: VS 2008 and Access 2003

    You don't have to go to code bank - this forum has great ado tutorials with samples.

    btw, I never use any bindings what so ever - they are nothing but pain. Direct select/insert/update/delete imho is much better approach.

  9. #9
    Fanatic Member
    Join Date
    Jun 2008
    Location
    Portland, OR, USA
    Posts
    659

    Re: VS 2008 and Access 2003

    Quote Originally Posted by RhinoBull View Post
    You don't have to go to code bank - this forum has great ado tutorials with samples.

    btw, I never use any bindings what so ever - they are nothing but pain. Direct select/insert/update/delete imho is much better approach.
    Yup, What Rhinobull said, on both counts.

    That said, the tutorial of jmc's I provided as a link is one of the better ones, even though tere are great tutorials here in the FAQ as well.

    Bound controls ARE a pain!!

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    23

    Re: VS 2008 and Access 2003

    vb Code:
    1. If txtName.Text = "" Then
    2.             MsgBox("You need to fill out your name")
    3.             Exit Sub
    4.         End If
    5.         Try
    6.             ''This code is doing my head in''
    7.             Dim objRow As DataRow
    8.             Dim tableName As String = "highScore"
    9.             Dim objDataSet As New DataSet
    10.             dbConnection = New OleDb.OleDbConnection(ConnectionString)
    11.  
    12.             dbAdapter = New OleDb.OleDbDataAdapter("SELECT * FROM " & tableName & "", ConnectionString)
    13.             Dim NewCommand = New OleDb.OleDbCommandBuilder(dbAdapter)
    14.             dbAdapter.FillSchema(objDataSet, SchemaType.Source, tableName)
    15.             dbAdapter.Fill(objDataSet, tableName)
    16.             objRow = objDataSet.Tables(tableName).Rows.Add
    17.             Dim rowNum = objDataSet.Tables(tableName).Rows.Count
    18.             objRow = objDataSet.Tables(tableName).Rows.Find(rowNum)
    19.  
    20.             objRow.Item(0) = rowNum
    21.             objRow.Item(1) = txtName.Text
    22.             objRow.Item(2) = myTotalScore
    23.             dbAdapter.Update(objDataSet, tableName)
    24.         Catch ex As Exception
    25.             MsgBox(ex.Message)
    26.         End Try

    Ok this works and anyway thanks for the helps folks

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