Results 1 to 20 of 20

Thread: [2005] Can't update, change or add new row in a Access database

  1. #1

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    [2005] Can't update, change or add new row in a Access database

    Hi All,

    I'm trying to create a first Database and use a AccessDatabase to do so.

    The problem I have is that:

    - Can't create a new Row
    - Can't update my database
    - Can't commit my database

    Every time I've got an erros saying that:
    Object reference not set to an instance of an object.

    Here's my code to update my database:

    Code:
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            da.Fill(ds, "AddressBook")
    
            ds.Tables("AddressBook").Rows(inc).Item(1) = IDTextBox.Text
            ds.Tables("AddressBook").Rows(inc).Item(1) = FirstNameTextBox.Text
            ds.Tables("AddressBook").Rows(inc).Item(2) = SurnameTextBox.Text
    
            da.Update(ds, "AddressBook")
    
            MsgBox("Data updated")
        End Sub
    Thanks in advance,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Can't update, change or add new row in a Access database

    Which line throws the exception? Which reference on that line is Nothing?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Can't update, change or add new row in a Access database

    I think that da in line
    vb Code:
    1. Dim cb As New OleDb.OleDbCommandBuilder(da)
    hasn't been declared before being used

  4. #4

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Quote Originally Posted by jmcilhinney
    Which line throws the exception? Which reference on that line is Nothing?
    Hi,

    I was in a little hurry and forgot to tell you wich line gave me the error;

    Code:
    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            da.Fill(ds, "AddressBook")  ' <- here's the error
    
            ds.Tables("AddressBook").Rows(inc).Item(1) = IDTextBox.Text
            ds.Tables("AddressBook").Rows(inc).Item(1) = FirstNameTextBox.Text
            ds.Tables("AddressBook").Rows(inc).Item(2) = SurnameTextBox.Text
    
            da.Update(ds, "AddressBook")
    
            MsgBox("Data updated")
        End Sub
    You can also see in attachment what error it gives me.

    Wkr,

    sparrow1
    Last edited by sparrow1; Oct 11th, 2007 at 10:07 AM.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  5. #5
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Can't update, change or add new row in a Access database

    Where did you declare the variable da?

  6. #6

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Quote Originally Posted by talkro
    Where did you declare the variable da?
    Hi,

    I declared the variable da before the form_load.
    This is my variable:

    Code:
    Dim da As OleDb.OleDbDataAdapter
    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] Can't update, change or add new row in a Access database

    try
    vb Code:
    1. private da as oledb.oledbDataAdapter

    private has a scope for the all class while dim as a limited scope (generally for a specific sub or function)
    Last edited by talkro; May 21st, 2007 at 09:52 AM.

  8. #8
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Can't update, change or add new row in a Access database

    I think you forgot the New keyword... either with the dataset or the data adapter
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  9. #9
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Can't update, change or add new row in a Access database

    You need the New keyword.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Can't update, change or add new row in a Access database

    If you take a look at the error message, it's telling you exactly the same thing as stimbo and onlyGirl.
    Quote Originally Posted by talkro
    try
    vb Code:
    1. private da as oledb.oledbDataAdapter

    private has a scope for the all class while dim as a limited scope (generally for a specific sub or function)
    "Dim" and "Private" are completely different things. "Dim" has nothing whatsoever to do with scope and everything to do with declaring a variable, no matter where it's declared. Try entering the code you suggested in the IDE and then mouse over the variable name. You'll see that the tool tip says "Private Dim da As OleDb.OleDbDataAdapter". "Dim" is ALWAYS included when you declare a variable, whether explicitly or implicitly. If you omit the "Dim" key word on a member variable declaration then it is implied. If you omit the access modifier on a member variable declaration and then the default is implied. The default access level for classes is Private while for structures it's Public. Access modifiers like Private are used on all members and types too, while the "Dim" key word is used for one thing only: declaring variables.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Hi,

    Thanks for your time and explanation about the declaration of variables.

    Where shout I use the New Ketword like Stimbo and OnlyGirl suggested.


    It is my first database to create and I'm still reading about it.
    So, Where should I put the New keyword.

    Thanks,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Can't update, change or add new row in a Access database

    It doesn't matter whether you've used databases before or whatever. Classes are classes and objects are objects. If you want to create an object, i.e. and instance of a class, then you have to use the New key word to invoke its constructor. All classes are the same. Look at this from your own code:
    vb.net Code:
    1. Dim cb As New OleDb.OleDbCommandBuilder(da)
    You wanted a new instance of the OleDbCommandBuilder class so you used the New key word to invoke the class constructor and create an instance. The OleDbDataAdapter class is exactly the same, as is any class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Hi All,

    I've rebuild my database and now she's working.

    But in the mean time I've got some difficulties like, if the database isn't loaded and the user clicks on the Next Record button or another one then I've got the Null reference error again.

    I thought in the first place to Enable all the buttons except the Load button.
    The easy way, but I want it the other way.

    Is it possible to add some code like:

    If the database isn't loaded then
    a messagebox with Yes/No and
    a text like "would you like to load it" then
    yes and load the database.

    This to avoid the Error message and crashes the databaseapplication.

    If there's another solution, please tell me about it.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  14. #14
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: [2005] Can't update, change or add new row in a Access database

    Assuming you don't want to load the database at startup, I'd disable all the buttons except Load-then enable them at the (successful) end of the Load procedure.

    It's possible to use the approach you described, but you'd need to add the 'load database' question to each of the buttons that manipulates the database (either explicitly or by each of the buttons calling a common routine).

  15. #15

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Hi,

    Thanks for your reply!
    That was my first thinking about it also.

    I've found already something and It does it, like this;

    Code:
    If inc <> -1 Then
                MsgBox("Load Database First")
                Exit Sub  ' instead of exit sub how can I load my database here
            End If
    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  16. #16
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: [2005] Can't update, change or add new row in a Access database

    When using a shared database you'll probably want to open a connection, use it (e.g. fill the dataset) & then close it. Since this is Access it might not be shared so you could open it when the program starts, in which case all you'd need to do here is fill the dataset.

  17. #17
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Can't update, change or add new row in a Access database

    Quote Originally Posted by calvin-c
    When using a shared database you'll probably want to open a connection, use it (e.g. fill the dataset) & then close it. Since this is Access it might not be shared so you could open it when the program starts, in which case all you'd need to do here is fill the dataset.

    A DataAdapter opens and closes the connection on its own. There is no need to manaully code the opening and closing actions.

    Sparrow, are you using any datasource controls like the BindingNavigator?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  18. #18

    Thread Starter
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] Can't update, change or add new row in a Access database

    Quote Originally Posted by circuits2
    A DataAdapter opens and closes the connection on its own. There is no need to manaully code the opening and closing actions.

    Sparrow, are you using any datasource controls like the BindingNavigator?
    Hi,

    No I don't use a BindingNavigator.
    It's just a Access database.
    With a :OleDb.OleDbConnection
    OleDb.OleDbDataAdapter
    OleDb.OleDbCommandBuilder

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  19. #19
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Can't update, change or add new row in a Access database

    Hi sparrow1,

    so, if a user selects a button to look at something but you haven't loaded it yet you get an error because the dataset hasn't been filled yet, correct? There's probably a better way of doing this but you could count if there is a table in the dataset.


    2 Code:
    1. If Not ds.Tables.Count = 0 Then
    2.         'Execute code as normal here
    3.  
    4.     Else
    5.  
    6.          Select Case MessageBox.Show("No data exists. Load now?", "Load Data?", MessageBoxButtons.OKCancel)
    7.  
    8.             Case Windows.Forms.DialogResult.OK
    9.  
    10.                  'Fill data here
    11.         End Select
    12.  
    13. End If
    Last edited by stimbo; May 23rd, 2007 at 05:19 PM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  20. #20
    Hyperactive Member
    Join Date
    May 2005
    Posts
    334

    Re: [2005] Can't update, change or add new row in a Access database

    Oops. Forgot that-we use a common routine that both creates & opens the connection, plus another that closes & destroys it. (It was created for other objects, but it's so convenient that we use it for data adapters, too.)

    Sparrow, assuming you've already created your connection what I'd do is replace Exit sub with your da.fill command. You'll need to be sure that you've created da as New & assigned it a select command, at least.

    Good luck.

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