You are here:

VB.NET/save data from unbound datagridview into sql server database


farhan wrote at 2012-12-16 21:42:11
the abov code must be corrected this way

Dim thisConnection As New SqlConnection("server=.\SQLEXPRESS;" & _

         "integrated security=sspi;database=Northwind")

       'Create Command object

       Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()


         ' Open Connection


         Console.WriteLine("Connection Opened")

         ' Create INSERT statement with named parameters

         nonqueryCommand.CommandText = _

         "INSERT  INTO myTable (Col1, Col2) VALUES (@Col1, @Col2)"

         ' Add Parameters to Command Parameters collection

         nonqueryCommand.Parameters.Add("@Col1", SqlDbType.VarChar, 50)

         nonqueryCommand.Parameters.Add("@Col2", SqlDbType.VarChar, 50)

         ' Prepare command for repeated execution


         ' Data to be inserted

         For Each row As DataGridViewRow In DataGridView1.Rows

         If Not row.IsNewRow Then

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

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



       End If



      Catch ex As SqlException

         ' Display error

         Console.WriteLine("Error: " & ex.ToString())


         ' Close Connection


         Console.WriteLine("Connection Closed")

       End Try  

ihtisham khan wrote at 2013-05-01 04:08:07
hi and good morning who ever has done that coding thanx a lot it help me properly to insert datagridview item to sql but one thing i want to make it clear that is this code will be usefull in case if we have combobox as datagridcolumn  


All Answers

Answers by Expert:

Ask Experts


Anup Mistry


WILL ANSWER: .NET Framework, WinForms, Windows Mobile, XML, SQL Server, T-SQL, Database related, Databinding, collections, Grid, DevExpress Framework, Web based, HTTP, IIS, ASP.NET anything web-related


Been software developer for 10+ years. Worked on various large business applications (Warehousing + Distribution, Financial, Healthcare). Worked on Mobile development (Proof of Delivery, Consumer based Infotainment application, Web based apps). Never stopped programming all these years. Worked on embedded, small, medium and large scale databases. Even in leisure time am reading computer related magazines. Have been a visiting professor at educational institutes teaching corporate and college students alike. Held Systems Analyst, Technical R&D member positions as well.

Speaker at Planet PDA on topic “Developing Apps for the Pocket PC” Seminar at Fairleigh Dickinson University, NJ on topic “Wireless LAN and Wireless Software applications”

Bachelors Degree in Engineering Electronics and Telecommunications Mobile Cellular Systems Satellite Communications

Awards and Honors
Brainbench certifications: (Microsoft.NET, Java 1, Oracle, Network Technical National Center for Software Technology: D-Level (software engineering exam)

©2016 All rights reserved.