hi,

is someone can give me a code example of insert data from datagrid to sql table?

thank's,

shay.

Dim conn As SqlConnection
        Dim strCnn As String = "your connection string;"
        conn = New SqlConnection(strCnn)
        Dim icnt As Int64 = 0
        conn.Open()

        Try


            If DataGridView1.Rows.Count > 0 Then

                Dim cmdInsert As New SqlCommand
                cmdInsert.Connection = conn
                Dim strCommandText As String
                Dim values As String = ""

                strCommandText = "INSERT INTO your insert statement here)VALUES("
                For i As Integer = 0 To DataGridView1.Rows.Count - 1


                    values = strCommandText & "Prepare your Values to pass from Grid i mean get the Cell values you want to insert into db "
                    cmdInsert.CommandText = values
                    icnt = icnt + 1
                    cmdInsert.ExecuteNonQuery()

                    values = ""
                Next
                cmdInsert = Nothing
            End If



        Catch ex As Exception
            MsgBox(icnt.ToString)
            'MsgBox(ex.ToString)
        Finally
            If conn.State = ConnectionState.Open Then
                conn.Close()
            End If
        End Try

hi,
thank's alot for your reply.
Can you give me an exact example for connection string, for an INSERT command and for what you wrote in line 21.
Because those the things that is Difficult for me.
you can come up with any names you want for sql name and values.
thank you!

Please give us ur table details for insert... And also the columns in grid..
or you can get several example on net.. try goggling...

table details : userName , userID , Password
columns in grid : userName , PhoneNo , Duration

I don't think you got the table details right. How you can insert phone number and duration into a table that holds user ID and password?

yes, you right...
table details : userName , userID , Password
columns in grid : userName , userID , Password
thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.