Results 1 to 4 of 4

Thread: help! insert into mdb database using datatable

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    45

    help! insert into mdb database using datatable

    hi,

    i'm having a problem with this set of codes.
    i'm trying to insert data into datatable and update it into a database but everytime i click the save button

    an OledbException will occur

    VB Code:
    1. Private myConnect As OleDbConnection
    2.     Private myDataadapter As OleDbDataAdapter
    3.     Private myDataTable As DataTable
    4.     Private cmbInsert As OleDbCommand    
    5.  
    6.     Private Sub FormItemType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim dbConnect As String = "Provider=Microsoft.JET.OLEDB.4.0;data source=D:\prodType.mdb"
    8.  
    9.         myConnect = New OleDbConnection(dbConnect)
    10.         Dim mySqlQuery As String = "Select *from ProductTypeInfo"
    11.         myDataTable = New DataTable
    12.         myDataadapter = New OleDbDataAdapter(mySqlQuery, dbConnect)
    13.         cmbInsert = New OleDbCommand
    14.         cmbInsert.CommandText = "Insert into ProductTypeInfo (ProductTypeID, ProductTypeName) values (@ProductTypeID, @ProductTypeName)"
    15.         cmbInsert.Connection = myConnect
    16.         cmbInsert.Parameters.Add(New OleDbParameter("@ProductTypeID", OleDbType.BSTR))
    17.         cmbInsert.Parameters.Add(New OleDbParameter("@ProductTypeName", OleDbType.BSTR))
    18.         myDataadapter.InsertCommand = cmbInsert
    19.  
    20.         myConnect.Open()
    21.         myDataadapter.Fill(myDataTable)
    22.         myConnect.Close()
    23.         DataGrid1.DataSource = myDataTable
    24.  
    25.     End Sub
    26.  
    27.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    28.         myConnect.Open()
    29.         myDataadapter.Update(myDataTable)
    30.  
    31.         myConnect.Close()
    32.     End Sub

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

    Re: help! insert into mdb database using datatable

    You should always provide the error message when posting code that throws an exception, otherwise we're guessing to a degree. I'm guessing the issue is the types you've chosen for your parameters. What sort of data do your columns contain? BSTR is an odd choice for most data. If your columns contain text then you should probably be using VarChar as the type. If ProductTypeID contains numbers then the type for that parameter should be Integer.
    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

    Thread Starter
    Member
    Join Date
    Apr 2006
    Posts
    45

    Re: help! insert into mdb database using datatable

    ok. thank you

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

    Re: help! insert into mdb database using datatable

    So... does that mean that the problem is solved? If so you then you should mark your thread as resolved from the Thread Tools menu. If not then feedback's a good idea, like "I'll try that and get back to you". Otherwise people might just unsubscribe from your thread and thus be less likely to help you if you add another post.
    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

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