Results 1 to 11 of 11

Thread: How to insert multiple data using dgv?

  1. #1
    New Member
    Join Date
    Jan 12
    Posts
    5

    How to insert multiple data using dgv?

    is there anyone there can help how to insert a multiple data using dgv in my database...i can insert a data but the only it is only rather two i want?
    here is my code any can help i do appreciate tnx in advance.....student :-)

    Code:
     ' Create INSERT statement with named parameters
                nonqueryCommand.CommandText = _
                   "INSERT  INTO ordetail (Account_code,description) VALUES (@Account_code,@description)"
    
                ' Add Parameters to Command Parameters collection
                nonqueryCommand.Parameters.Add("@Account_code", MySqlDbType.String, 0)
                nonqueryCommand.Parameters.Add("@description", MySqlDbType.String, 0)
                '  nonqueryCommand.Parameters.Add("@Col2", SqlDbType.VarChar, 50)
    
                ' Prepare command for repeated execution
                nonqueryCommand.Prepare()
    
                ' Data to be inserted
                For Each row As DataGridViewRow In DataGridView1.Rows
                    If Not IsNothing(row.Cells(0).Value) Then
                        '  For i As Integer = 0 To DataGridView1.Rows.Count - 1
    
                        With nonqueryCommand
    
                            ' .Parameters[0].Value = datagridview1.Rows[i].Cells[0].Value
    
                            .Parameters("@Account_code").Value = row.Cells(0).Value.ToString
    
                            .Parameters("@description").Value = row.Cells(1).Value.ToString
    
                        End With
                        ' Next
    
                        'nonqueryCommand.Parameters("@Col2").Value = row.Cells(1).Value.ToString
                    End If
                Next row
    
                nonqueryCommand.ExecuteNonQuery()
                ' nonqueryCommand.Parameters.Clear()
    
            Catch ex As MySqlException
                ' Display error
                Console.WriteLine("Error: " & ex.ToString())
            Finally
                ' Close Connection
                tryCon.Close()
                MsgBox("Details hav been successfully added", MsgBoxStyle.Information, "ORdetail information")
    
            End Try
    Last edited by koolsid; Sep 17th, 2012 at 12:06 AM. Reason: Added Code Tags

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,781

    Re: How to insert multiple data using dgv?

    Create a DataTable with the appropriate schema, either by building it manually or calling FillSchema on a data adapter. Bind that DataTable to your grid. Adding data to the grid will push it into the DataTable. You can then use a data adapter, perhaps the same one you called FillSchema on, to save the data to the database with a call to Update. To learn how to use the data adapter, follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data.

  3. #3
    New Member
    Join Date
    Jan 12
    Posts
    5

    Re: How to insert multiple data using dgv?

    ok tnx...can you give me an example?

  4. #4
    Addicted Member
    Join Date
    Nov 11
    Posts
    174

    Re: How to insert multiple data using dgv?

    hi jmc, i dunno if this is allowed. but can i ask a follow up question since im also doing this.
    can i just loop through the dgv and insert each row? thank you.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,781

    Re: How to insert multiple data using dgv?

    Quote Originally Posted by junskie View Post
    ok tnx...can you give me an example?
    If you have read my post and followed the instructions provided then you already have an example. Why should I bother posting if you're not going to read what I post and do what it says?

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,781

    Re: How to insert multiple data using dgv?

    Quote Originally Posted by m.davide View Post
    hi jmc, i dunno if this is allowed. but can i ask a follow up question since im also doing this.
    can i just loop through the dgv and insert each row? thank you.
    That is absolutely allowed. If your question is on the same topic then it is encouraged to post to an existing thread.

    Yes you can loop through the grid an insert each row but you shouldn't. Using a DataTable and data adapter is a better option. Unless you have a compelling reason to do otherwise, you should use the better option. If you do have a compelling reason then you would create a command and add parameters to it first. You would then loop through the grid and, for each row, set the parameter values of the command and execute it. That's exactly what junskie is doing in post #1 and I advised against it then.

  7. #7
    New Member
    Join Date
    Jan 12
    Posts
    5

    Re: How to insert multiple data using dgv?

    sori by the way...i did not follow your instruction...i read it anyway it was great tnx for your help...hope i can ask again if i have further question...god bless :-)

  8. #8
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    80,781

    Re: How to insert multiple data using dgv?

    Quote Originally Posted by junskie View Post
    sori by the way...i did not follow your instruction...i read it anyway it was great tnx for your help...hope i can ask again if i have further question...god bless :-)
    Sure you can ask more questions. That's what we're here for. Just please, if and when an answer is provided, make sure you read it and follow any instructions provided. Also, pick out any keywords and, if you need more information, search for it first before asking. Do that and we'll get on famously.

  9. #9
    Junior Member
    Join Date
    Sep 12
    Posts
    17

    Re: How to insert multiple data using dgv?

    Thank you

  10. #10
    Addicted Member
    Join Date
    Nov 11
    Posts
    174

    Re: How to insert multiple data using dgv?

    Quote Originally Posted by jmcilhinney View Post
    That is absolutely allowed. If your question is on the same topic then it is encouraged to post to an existing thread.

    Yes you can loop through the grid an insert each row but you shouldn't. Using a DataTable and data adapter is a better option. Unless you have a compelling reason to do otherwise, you should use the better option. If you do have a compelling reason then you would create a command and add parameters to it first. You would then loop through the grid and, for each row, set the parameter values of the command and execute it. That's exactly what junskie is doing in post #1 and I advised against it then.
    ok, thanks!

  11. #11
    New Member
    Join Date
    Jan 12
    Posts
    5

    Re: How to insert multiple data using dgv?

    this code statement is totaly correct im just miss look up my table name supposed to be or_detail in i wrote it ----ordetail---- thats why my select cannot find any table and my catch exeption supposed to be a msgbox.show (ex.message) and not console.writeline it is not running in vb.net mode only in C# try to look at my code....hehehe here the code try to look at this


    ' Create INSERT statement with named parameters
    nonqueryCommand.CommandText = _
    "INSERT INTO ordetail (Account_code,description) VALUES (@Account_code,@description)"

    ' Add Parameters to Command Parameters collection
    nonqueryCommand.Parameters.Add("@Account_code", MySqlDbType.String, 0)
    nonqueryCommand.Parameters.Add("@description", MySqlDbType.String, 0)
    ' nonqueryCommand.Parameters.Add("@Col2", SqlDbType.VarChar, 50)

    ' Prepare command for repeated execution
    nonqueryCommand.Prepare()

    ' Data to be inserted
    For Each row As DataGridViewRow In DataGridView1.Rows
    If Not IsNothing(row.Cells(0).Value) Then
    ' For i As Integer = 0 To DataGridView1.Rows.Count - 1

    With nonqueryCommand

    ' .Parameters[0].Value = datagridview1.Rows[i].Cells[0].Value

    .Parameters("@Account_code").Value = row.Cells(0).Value.ToString

    .Parameters("@description").Value = row.Cells(1).Value.ToString

    End With
    ' Next

    'nonqueryCommand.Parameters("@Col2").Value = row.Cells(1).Value.ToString
    End If
    Next row

    nonqueryCommand.ExecuteNonQuery()
    ' nonqueryCommand.Parameters.Clear()

    Catch ex As MySqlException
    ' Display error
    Console.WriteLine("Error: " & ex.ToString())
    Finally
    ' Close Connection
    tryCon.Close()
    MsgBox("Details hav been successfully added", MsgBoxStyle.Information, "ORdetail information")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •