Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My Code:
C#
try
 {
    for (int i = 0; i < gradePolicyGridView.Rows.Count - 1; i++)
        {
           db.command.Parameters.Add("@ClassInterval", SqlDbType.VarChar).Value = gradePolicyGridView.Rows[i].Cells[1].Value.ToString();
           db.command.Parameters.Add("@LetterGrade", SqlDbType.VarChar).Value = gradePolicyGridView.Rows[i].Cells[2].Value.ToString();
           db.command.Parameters.Add("@GradePoint", SqlDbType.Decimal).Value = Convert.ToDecimal(gradePolicyGridView.Rows[i].Cells[3].Value.ToString());
           db.Adapter("insertGradePolicy", true);
           MessageBox.Show("Data saved successfully...");
        }
  }
  catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }

In my GridView controls I am putting 3 rows but, when I press the save button the GridView clean its all values and inserting only 1 -row in my sql server table. How, can I insert multiple rows from my GriedView.

My Stored procedure :
SQL
USE [SMS2]
GO
/****** Object:  StoredProcedure [dbo].[insertGradePolicy]    Script Date: 06/20/2012 09:58:09 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[insertGradePolicy]
	(
	 @ClassInterval 	[varchar](50),
	 @LetterGrade 	[varchar](50),
	 @GradePoint 	[decimal])

AS INSERT INTO [SMS2].[dbo].[GradePolicy] 
	 ( 
	 [ClassInterval],
	 [LetterGrade],
	 [GradePoint])
 
VALUES 
	( 
	 @ClassInterval,
	 @LetterGrade,
	 @GradePoint 
	)

My Table:
SQL
USE [SMS2]
GO
/****** Object:  Table [dbo].[GradePolicy]    Script Date: 06/20/2012 10:52:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[GradePolicy](
	[SL] [int] IDENTITY(1,1) NOT NULL,
	[ClassInterval] [varchar](50) NULL,
	[LetterGrade] [varchar](50) NULL,
	[GradePoint] [decimal](18, 0) NULL,
 CONSTRAINT [PK_GradePolicy] PRIMARY KEY CLUSTERED 
(
	[SL] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Please tell me where is the problem in my code or how can I update my c# code to insert multiple rows without cleaning GridView fields.

Thank you..
Posted
Updated 19-Jun-12 19:38pm
v3

1 solution

Hi ,
Try this one
C#
foreach (DataGridViewRow row in dataGridView1.Rows)
            {
            // here your Storedprocdure  
           //MessageBox.Show(row.Cells[0].Value.ToString());
            }

Best Regards
M.Mitwalli
 
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