You are here:

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

Advertisement


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()



       Try

         ' Open Connection

         thisConnection.Open()

         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

         nonqueryCommand.Prepare()



         ' 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

         nonqueryCommand.ExecuteNonQuery()

 



       End If

         Next



         

      Catch ex As SqlException

         ' Display error

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

       Finally

         ' Close Connection

         thisConnection.Close()

         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  


VB.NET

All Answers


Answers by Expert:


Ask Experts

Volunteer


Anup Mistry

Expertise

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

Experience

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.

Publications
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”

Education/Credentials
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 About.com. All rights reserved.