שתף באמצעות


how to insert data using datagridview in vb.net windows form?

Question

Monday, January 9, 2012 7:28 AM

hi friends

how can i insert data to database using datagridview in vb.net? i want to use the datagird as input filed. i dont want to bind the gridview.

please give any idea or sample code

thanks in advance

By Faisal

 

faisal

All replies (5)

Monday, January 9, 2012 7:32 AM

Yes you can do it, but a bunch of work, be aware it is so unconventional what you ask, that there is no professional sample code.

It is asking in an analogy: can somebody give me an example how to drive the best 100 backwards on the highway.

If you use databases you use in anyway something which holds your data. Which can be entities, DataSets, context or any other object to which you bind a user interface (for instance a datagridview) to let the user handle the data.

That data can be updated.

 

Success
Cor


Monday, January 9, 2012 11:50 AM

Hi Faisal Abdul

Welcomes you to MSDN forums.

If you insert the new record from Datagrid view to SQL table
Use the following code with the help of DataTable
Also you can use datatable for Collect all the data from your DataGridview using DataGridview.DataSource method.

COde Snipt:

 Private Sub cmdInsert_Click(sender As System.Object, e As System.EventArgs) Handles cmdInsert.Click

        Dim dt As New DataTable
        dt = MyDataGridView.DataSource

        Dim objSQLCon As New SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;")
        Dim TableName As String = "Your Sql table Name"
        Dim strErrorMessage As String = ""

        Try
            If objSQLCon.State = ConnectionState.Open Then
                objSQLCon.Close()
            End If

            objSQLCon.Open()

            Try
                'To Perform Bulk Copy for copy data from DataTable to SQL Table
                If dt.Rows.Count > 0 Then
                    Using Sqlbcp As SqlBulkCopy = New SqlBulkCopy(objSQLCon)
                        Sqlbcp.DestinationTableName = "[dbo].[" & TableName & "]"
                        Sqlbcp.WriteToServer(dt)
                    End Using
                End If

            Catch sqlEx As SqlException
                strErrorMessage = "SqlException: " & sqlEx.Message & Environment.NewLine & "Error Code: " & sqlEx.ErrorCode
            Catch ex As Exception
                strErrorMessage = ex.Message
            End Try

        Catch ex As Exception
            strErrorMessage = "Open SQL connection failed" & ex.Message
        End Try
    End Sub

 

I hope It will helps you to solve your problems..
By
A Pathfinder.
Joswa

 

If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"


Monday, January 9, 2012 12:32 PM

Hi Faisal Abdul

Welcomes you to MSDN forums.

If you insert the new record from Datagrid view to SQL table
Use the following code with the help of DataTable
Also you can use datatable for Collect all the data from your DataGridview using DataGridview.DataSource method.

COde Snipt:

 Private Sub cmdInsert_Click(sender As System.Object, e As System.EventArgs) Handles cmdInsert.Click

        Dim dt As New DataTable
        dt = MyDataGridView.DataSource

        Dim objSQLCon As New SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;User ID=myDomain\myUsername;")
        Dim TableName As String = "Your Sql table Name"
        Dim strErrorMessage As String = ""

        Try
            If objSQLCon.State = ConnectionState.Open Then
                objSQLCon.Close()
            End If

            objSQLCon.Open()

            Try
                'To Perform Bulk Copy for copy data from DataTable to SQL Table
                If dt.Rows.Count > 0 Then
                    Using Sqlbcp As SqlBulkCopy = New SqlBulkCopy(objSQLCon)
                        Sqlbcp.DestinationTableName = "[dbo].[" & TableName & "]"
                        Sqlbcp.WriteToServer(dt)
                    End Using
                End If

Jo,

I become always curious if I see something from which I thought it would not go. Be aware I'm often wrong and than I've learned again something.

I tried your code, with option strict on it gives direct an impossible casting error. Despite that I thought to know the result I tried it with Option Strict Off

This was the result, (i've put a breakpoint to avoid an error)

Please reply next time to the Original Poster and not to me so it does not confuse me if you were intending it like that.

 

 

Success
Cor


Monday, January 9, 2012 1:49 PM

Dim dt As New DataTable
dt = MyDataGridView.DataSource
From the above two statements, I'd like to tell you. Maybe you misuse the New keyword. I would assume you previously has populated the MyDataGridView.DataSource property. So it would not be a null reference(Nothing in VB.NET).

My blog: http://soho-hsh.blogspot.com


Monday, January 9, 2012 3:52 PM

hi friends

how can i insert data to database using datagridview in vb.net? i want to use the datagird as input filed. i dont want to bind the gridview.

please give any idea or sample code

thanks in advance

By Faisal

 

faisal

Here are some examples, with Access2007, and SqlServerExpress SqlCompact, but besides the examples I suggest a good book and some other article.

http://code.msdn.microsoft.com/Esempio-applicazione-dati-70387089 SqlCompact

http://code.msdn.microsoft.com/Esempio-applicazione-dati-494c129a SqlSrverExpress

http://code.msdn.microsoft.com/Esempio-di-utilizzo-file-4d9b07a8 Access 2007.

 

Regards.