Results 1 to 8 of 8

Thread: need help saving to database

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    need help saving to database

    Hey guys,
    I have an "Import" button that successfully reads in an excel spreadsheet to a bound datagridview with this code:

    vb Code:
    1. Private Sub importDBButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles importDBButton.Click
    2.         OpenFileDialog1.InitialDirectory = ""
    3.         OpenFileDialog1.Title = "Select the Excel File to import from"
    4.         OpenFileDialog1.Filter = "Excel(*.xls)|*.xls"
    5.         OpenFileDialog1.ShowDialog()
    6.  
    7.         If OpenFileDialog1.FileName <> "" Then
    8.             Dim _filename As String = OpenFileDialog1.FileName
    9.             Dim _conn As String
    10.             _conn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & _filename & ";" & "Extended Properties=Excel 8.0;"
    11.             Dim _connection As OleDbConnection = New OleDbConnection(_conn)
    12.             Dim da As OleDbDataAdapter = New OleDbDataAdapter()
    13.             Dim _command As OleDbCommand = New OleDbCommand()
    14.             _command.Connection = _connection
    15.             _command.CommandText = "SELECT * FROM [Sheet1$]"
    16.             da.SelectCommand = _command
    17.             Try
    18.                 da.Fill(ds1, "sheet1")
    19.                 MessageBox.Show("Imported Successfully.")
    20.                 frmTest.device1DataScan.DataSource = ds1
    21.                 frmTest.device1DataScan.DataMember = "sheet1"
    22.                 frmTest.DVRSTableAdapter.Update(Me.ds1) 'I thought this would do the trick, but relaunching the program doesn't load the saved DB
    23.             Catch e1 As Exception
    24.                 MessageBox.Show("Import Failed, incorrect Column names in the sheet!")
    25.             End Try
    26.         End If
    27.     End Sub


    This works great! What I would like to happen though, is for the new contents of the DGV to save to that table in the database, so that the next time the program loads, the user won't have to import again.

    I tried Update().. Fill(), but to be honest, I don't know what needs to be "updated" or "filled" or how I should be saving the new contents to the database

    Can someone straighten me out?

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

    Re: need help saving to database

    When you Fill a DataTable using a DataAdapter, all the DataRows in the table have their RowState set to Unchanged by default. What you need to do is set the AcceptChangesDuringFill property of your DataAdapter to False so that all the DataRows are flagged as Added, which means that they are new rows ready to be inserted into the database. You can then Update that DataTable using your TableAdapter.

    Just note though, that you will need to Fill the same DataTable as your TableAdapter is supposed to save. That means that you can't Fill a table named "sheet1" in your DataSet. You have Fill ds1.DVRS, or whatever the table is called.
    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
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: need help saving to database

    ahh ok. guess that's where I'm kinda confused. that code posted I snagged online and it creates a completely different DA and DS. I was hoping I could leave it intact, and once the new ds is filled and I have bound it to the DGV, then I could somehow say.. "take whatever is now in the DGV and store it in my original table"
    (DeviceDBDataSet.DVRS)

    EDIT: I dont show that property on my TableAdapter or on my DataSet. Intellisense will show it for the da in the code above, but tells me "Property access must assign to the property or use its value"

    frmTest.DVRSTableAdapter is the one i need to allow changes on i suppose but the property isn't listed for it

  4. #4

  5. #5

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: need help saving to database

    Really can't figure this out.. been toying with it for the past 2 days and can't get it..

    I have:
    DVRSTableAdapter
    DeviceDBDataSet
    and a table called DVRS

    the code above seems to create its own dataset and data adapter. I just need to be able to save what gets imported from the above code to my Table called DVRS and have it save to the database so they won't have to import on the next program launch.

    Is there another way I could be doing this? I have a blank table, DVRS, in an access file. When the program first launches, I want them to import their stuff to my DataGridView. This works so far. Now I want them to be able to save the contents of the DataGridView to he DVRS table in the database. Since the DataGridView's binding source is the DVRS table, on the next launch, it should populate by itself the previous imported devices.

    Can someone help me out here?

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

    Re: need help saving to database

    Quote Originally Posted by stateofidleness View Post
    Really can't figure this out.. been toying with it for the past 2 days and can't get it..

    I have:
    DVRSTableAdapter
    DeviceDBDataSet
    and a table called DVRS

    the code above seems to create its own dataset and data adapter. I just need to be able to save what gets imported from the above code to my Table called DVRS and have it save to the database so they won't have to import on the next program launch.

    Is there another way I could be doing this? I have a blank table, DVRS, in an access file. When the program first launches, I want them to import their stuff to my DataGridView. This works so far. Now I want them to be able to save the contents of the DataGridView to he DVRS table in the database. Since the DataGridView's binding source is the DVRS table, on the next launch, it should populate by itself the previous imported devices.

    Can someone help me out here?
    I've already answered your question in post #2:
    Just note though, that you will need to Fill the same DataTable as your TableAdapter is supposed to save. That means that you can't Fill a table named "sheet1" in your DataSet. You have Fill ds1.DVRS, or whatever the table is called.
    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

  7. #7

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: need help saving to database

    what is ds1.DVRS?

    ds1 is the dataset created during the import. "DVRS" is an existing table in my database.
    I've tried:
    da.Fill(frmTest.DeviceDBDataSet.DVRS, "sheet1")

    This works:
    da.Fill(ds1, "sheet1")

    but it is filling the wrong data adapter. I need it to go to DVRSTableAdapter, but the .Fill on DVRSTableAdapter won't work for me...

    what should my fill statement be?

  8. #8

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: need help saving to database

    vb Code:
    1. Try
    2.                 da.Fill(ds1, "sheet1")
    3.                 MessageBox.Show("Imported Successfully.")
    4.                 frmTest.device1DataScan.DataSource = ds1
    5.                 frmTest.device1DataScan.DataMember = "sheet1"
    6.             Catch e1 As Exception
    7.                 MessageBox.Show("Import Failed, incorrect Column names in the sheet!")
    8.             End Try
    9.  
    10.             Try
    11.                 frmTest.DVRSTableAdapter.ClearBeforeFill = True
    12.                 frmTest.DVRSTableAdapter.Fill(ds1.Tables(0))
    13.                 frmTest.DVRSTableAdapter.Update(ds1)
    14.                 MsgBox("success!")
    15.             Catch ex As Exception
    16.                 MsgBox(ex.ToString)
    17.             End Try

    This first Try block works fine. The second one fails with the exception "System.InvalidCastException: Unable to cast object of type 'System.Data.DataTable' to type 'DVRSDataTable'.

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