Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii,

I would like to know how to save the datas from a datagridview(contains both unbound and bound columns) into the database. i am using Visual Studio 2010 (vb.net)..

Please Help..!!!
Thanks in advance
Posted
Comments
RDBurmon 21-Jan-12 4:18am    
Can you give us the gridview structure as well the table structure ? other wise wait a while I am preparing code for you.

Hello Cluelessssssss ,

Since I didn't have you grid and table structure. I will be giving solutions through my considered example.

Here it is

Situation :
We need to Add / Update employee information and key is employee id.
Database Stucture :

Table :
SQL
/****** Object:  Table [dbo].[Employee]    Script Date: 01/21/2012 16:38:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Employee](
	[Id] [nvarchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[Name] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
	[Position] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
	[Location] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]


Store Procedure :
SQL
CREATE PROCEDURE sp_SaveEmployeeDetail
 ( @Id	NVARCHAR(10),
   @Name	NVARCHAR(255),
   @Position	NVARCHAR(255),
   @Location	NVARCHAR(255)
 )
 AS 
 BEGIN
  
  If EXISTS (SELECT * FROM Employee WHERE Id=@Id)
   Begin
       Update Employee
        SET 
			Name=@Name ,
			Position=@Position ,
			Location=@Location
       WHERE Id=@Id
   End
  Else
   Begin
		Insert Into Employee SELECT @Id,@Name,@Position,@Location
   End		
END


And here is vb.net to call this procedure which would accept parameters .

VB.NET
Dim connection As SqlConnection = New SqlConnection("Data Source=MDT765\MDT765;Initial Catalog=TST;User Id=user;Password=user@123;")
connection.Open()
Dim Cou As Integer
Dim command As SqlCommand = _
    New SqlCommand("sp_SaveEmployeeDetail", connection)
command.CommandType = CommandType.StoredProcedure
command.Parameters.Add("@Id", SqlDbType.NVarChar,10)
command.Parameters.Add("@Name", SqlDbType.NVarChar, 255)
command.Parameters.Add("@Position", SqlDbType.NVarChar, 255)
command.Parameters.Add("@Location", SqlDbType.NVarChar, 255)

For Cou = 0 To DataGridView1.Rows.Count - 2
    command.Parameters("@Id").Value = DataGridView1.Item(0, Cou).Value.ToString()
    command.Parameters("@Name").Value = DataGridView1.Item(1, Cou).Value.ToString()
    command.Parameters("@Position").Value = DataGridView1.Item(2, Cou).Value.ToString()
    command.Parameters("@Location").Value = DataGridView1.Item(3, Cou).Value.ToString()
    command.ExecuteNonQuery()
Next

MsgBox("Done")
 
Share this answer
 
You can update database after inserting data(rows) or edit data in datagridview.
see this tip
DataBase Updation With DataGridView
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900