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


I am trying to save the data from data grid view to database but it is saving one data at one time i want to save multiple data please can any one tell me how to do this




Thanks in advance
Posted
Updated 4-Apr-12 0:21am
v2

You will get some help from these links:
Here[^]
Here[^] &
Here[^]
 
Share this answer
 
STEP 1:
StringBuilder strSample = new StringBuilder();
DataTable dt = new Datatable();

STEP 2:
Add your datagridview value in to above declared datatable;

STEP 3:
strSample.Append("<Root>");
if (dt.Rows.Count > 0)
{
foreach (DataRow dtDetailsRow in dt.Rows)
{
strSample.Append(" <TESTDETAILS TEST_ID1 = '" + 1 + "'");
strSample.Append(" TEST_VALUE1 = '" + "SAMPLE TEST" + "'/>");
}
}
strSample.Append("</Root>");

Step 4:
send this "strSample" stringbuilder to SQL Server Stored Procedure Like below

Step 5:
CREATE PROCEDURE [dbo].[SP_Sample_Test]
(
@strSample NText
)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @OC INT
--TEMP TABLE DECLARATION
DECLARE @TBLSAMPLEDTLS TABLE
(
TEST_ID1 INT,
TEST_VALUE1 VARCHAR(25)
)
SET @ERRORCODE=0

--Temp table Insert
Exec SP_XML_PrepareDocument @OC Output, @strSample
INSERT INTO @TBLSAMPLEDTLS
(
TEST_ID1,TEST_VALUE1
)
SELECT TEST_ID1,TEST_VALUE1
FROM OPENXML(@OC,'Root/TESTDETAILS',1)
WITH (TEST_ID1 INT,TEST_VALUE1 VARCHAR(25))

--Main table Insert
INSERT INTO Sample_Test
(
TEST_ID1,TEST_VALUE1
)
SELECT TEST_ID1,TEST_VALUE1 FROM @TBLSAMPLEDTLS

SET @ERRORCODE=0


END


Finally at one shot your data's get uploaded.
 
Share this answer
 
pls give datagridview in loop condition
for(i=0;i<datagridview1;i++)>
{
insert into table name value();
}
 
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