Results 1 to 7 of 7

Thread: inserting into a access database

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2002
    Location
    BC,Canada
    Posts
    15

    inserting into a access database

    Could somebody help me out.
    I'm trying to add some values from a textbox into my database
    and so far i couldn't get it to work.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Posting a sample app for you but I need to trim down the database size so it's under 100k, just a minute.

    Actually that's a crappy example, let me put together a better one. Just a bit
    Last edited by Slow_Learner; Oct 1st, 2002 at 01:32 AM.

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    can't get my example to work either :/ Looks really simple, doesn't error, but records never get written to the file.

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim objRow As DataRow = DataSet11.Table1.NewRow()
            objRow("Text") = TextBox1.Text
            DataSet11.AcceptChanges()
    
            OleDbConnection1.Open()
    
            Try
                OleDbDataAdapter1.Update(DataSet11.Table1)
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                OleDbConnection1.Close()
            End Try
    
        End Sub
    Maybe some brighter soul will enlighten us both.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    AcceptChanges doesn't do what you think. You call it after the changes have been made (kind of like setting the IsDirty flag to False) or you don't have to do it at all. But if you call it before it thinks the changes have already been made and doesn't update.

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Duh! Forgot to .Rows.Add!!

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim objRow As DataRow = DataSet11.Table1.NewRow()
            objRow("Text") = TextBox1.Text
            DataSet11.Table1.Rows.Add(objRow)
            OleDbDataAdapter1.Update(DataSet11)
        End Sub
    *bangs forehead against the wall* Although you're right Edneeis, AcceptChanges does keep the update from happening if you do it first. I guess the "correct" way would be:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim objRow As DataRow = DataSet11.Table1.NewRow()
            objRow("Text") = TextBox1.Text
            DataSet11.Table1.Rows.Add(objRow)
            Try
                OleDbDataAdapter1.Update(DataSet11)
                DataSet11.AcceptChanges()
            Catch ex As Exception
                msgbox(ex.Message)
            End Try
        End Sub

  6. #6
    Fanatic Member rudvs2's Avatar
    Join Date
    Mar 2001
    Location
    NZ
    Posts
    935
    actually the code above wont work unless the data objects you are referencing are design time bound to your database

    If you are trying to create the connection through code you should use OLEDB and either Use the OLEDBCommands specifiying each sql statement in code, or you can create a dataset via code and create an instance of an OLEDBDataadapter bind to the dataadapter an Instance of the OLEDBCommandBuilder, you still have to mainly specify the sql Select statement for the dataadapter but basically the builder will dynamically take care of the rest for you.

  7. #7
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Originally posted by rudvs2
    actually the code above wont work unless the data objects you are referencing are design time bound to your database
    Yep that's the way I did it.

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