1

i want to insert records from datagridview to mysql database table , but i dont know how to insert datagridview to database table. How to do it? :(

4 Answers 4

2

kumar, there is a problem with your code

name = Me.DataGridView1.Rows(i).Cells(0).ToString()

  age= Me.DataGridView1.Rows(i).Cells(1).ToString()

SHOULD BE

name = Me.DataGridView1.Rows(i).Cells(0).Value.ToString()

  age= Me.DataGridView1.Rows(i).Cells(1).Value.ToString()
Sign up to request clarification or add additional context in comments.

Comments

1

This is jus my suggestion if you have you code then can work out more better

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ''create connection 
        Dim conn As SqlConnection = New SqlConnection()
        conn.Open()

        Dim comm As SqlCommand = New SqlCommand()
        comm.Connection = conn

        ''insert data to sql database row by row
        Dim name, ageAs String
        For i As Integer = 0 To Me.DataGridView1.Rows.Count
            name = Me.DataGridView1.Rows(i).Cells(0).ToString()
            age= Me.DataGridView1.Rows(i).Cells(1).ToString()

            comm.CommandText = "insert into mytable(name,age) values('" & name & "','" & age& "')"
            comm.ExecuteNonQuery()
        Next

        conn.Close()

    End Sub

This is just a way to insert it is best it you use stored procedure to insert the value because if you use SQL it might happen SQL Injection. So stored procedure is the best way and save to use.

4 Comments

how about if i want to insert with multiple row from datagridview ? or may be you have any idea to my problem ? anyway, thank for your help
My suggestion is for your to use Stored Procedure. You jus need to create once can be used multiple times within the query.
Use the newer code. this will help you actually since using the for each loop.
thank you so much kumar, its very helpfull i can insert to mysql from datagridview now,
0

Change the line of your code:

 For i As Integer = 0 To Me.AddPurchases.Rows.Count

with this line:

 For i As Integer = 0 To Me.AddPurchases.Rows.Count - 1 

The above is the solution for the exception of verohwm's message. *

Comments

0

instead of inserting data I was trying to update the record and for me

Me.AddPurchases.Rows(i).Cells(0).Value.ToString()

this statement did not solve the purpose this statement will return the row count and column count like { row=0,col=1} next iteration { row=0,col=1} and so on but

Dim roid As String

    For i As Integer = 0 To Me.edit_role_grid.RowCount - 1
        roid = Me.edit_role_grid.Rows(i).Cells(0).FormattedValue
        MsgBox(roid) 

this worked fine for me, this is returning the text of the cell and now this value can be used anywhere

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.