Results 1 to 8 of 8

Thread: how to insert multiple rows from datagrid to database in vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    how to insert multiple rows from datagrid to database in vb.net

    i have a datagrid contol with multiple rows. my database structure and datagridview stucture is same.

    now how to insert multiple rows from datagrid to database in vb.net


    please give a sample code for this

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

    Re: how to insert multiple rows from datagrid to database in vb.net

    Is this question related to this thread:

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

    You know, the one where I asked:
    So, the data is NOT going to be saved to a database?
    and you said:
    yes you are correct.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: how to insert multiple rows from datagrid to database in vb.net

    yes. of course.

    at first i need to collect more than one record in my datagridview. after collecting the record. i need to insert the datgridview values into my database.......

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

    Re: how to insert multiple rows from datagrid to database in vb.net

    So, why did you say that it wasn't going to be saved when I asked if it was? Did you not think that maybe I was asking that question for a reason? My answer to your question was going to be different depending on whether the data was to be saved to a database or not. If you're going to ask for our help then the least you can do is provide accurate information when we ask for it because otherwise you're wasting our time.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: how to insert multiple rows from datagrid to database in vb.net

    yes Mr.jmcilhinney that was my mistake.



    is there any way to insert multiple rows from datagrid to database ?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    103

    Re: how to insert multiple rows from datagrid to database in vb.net

    is there any way to insert multiple rows from datagrid to database ?

  7. #7
    Frenzied Member
    Join Date
    Jan 2010
    Location
    Connecticut
    Posts
    1,687

    Re: how to insert multiple rows from datagrid to database in vb.net

    There may be a better way, but you can always build an INSERT statement in a loop.
    VB6 Library

    If I helped you then please help me and rate my post!
    If you solved your problem, then please mark the post resolved

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: how to insert multiple rows from datagrid to database in vb.net

    Follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. It includes an example of using a DataAdapter to retrieve and save data, with the ability to edit in between. That's basically what you're doing, with a bit of variation.

    In your case, you can call FillSchema instead of Fill, because you don't actually want to retrieve any data. You also don't need the DELETE and UPDATE statements, because you're only inserting data.

    Once you've got your DataTable, bind it to your grid via a BindingSource, e.g.
    vb.net Code:
    1. myBindingSource.DataSource = myDataTable
    2. myDataGridView.DataSource = myBindingSource
    When you want to add a new row, you use the BindingSource, e.g.
    vb.net Code:
    1. Dim newRow As DataRowView = myBindingSource.AddNew()
    2.  
    3. newRow("Column1") = Me.TextBox1.Text
    4. newRow("Column2") = Me.TextBox2.Text
    5. myBindingSource.EndEdit()
    That's it, that's all.

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