Link to home
Start Free TrialLog in
Avatar of javierpdx
javierpdxFlag for United States of America

asked on

Update sql Statement based on a GridView field

Hello,
I am trying run an update statement based on a current GridView.  The reason is that if the sql table data is updated while in the current GridView, I only want to set an update for each record of the currently selected data.
Insert Statement:
using (SqlCommand command = new SqlCommand())
                        {command.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString);
command.CommandText = @"insert into [Comm](CommType, StudentID)
                                                    values(@CommType, @StudentID)";
command.Parameters.Add("@CommType", SqlDbType.VarChar, 16).Value = "Emailed";
command.Parameters.Add("@StudentID", SqlDbType.VarChar, 9).Value = **How do I add a GridView Column Name here for each record in my GridView?***;                           
                            command.Connection.Open();
                            command.ExecuteNonQuery();
                            command.Parameters.Clear();
                            command.Connection.Close();

Open in new window

Comm Table:
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[CommType] [varchar](16) NULL,
	[StudentID] [varchar](9) NULL,

Open in new window

The field I want to grab from the GridView is:
asp:BoundField DataField="StudentID"

Is is possible to do this, or is there a better way to achieve this?

Thank you for any assistance.
SOLUTION
Avatar of k_murli_krishna
k_murli_krishna
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of javierpdx

ASKER

Sorry for the delay in responding to your suggestions.  I have been away due to the holiday break.  I will test this over the weekend and respond as soon as I am able.  Thanks.
Thanks for the expert advise.  I appreciate it.

This ended up working:

command.CommandText = @"insert into [Communication](CommType, ID)
values(@CommType, @ID)"
command.Parameters.Add("@CommType", SqlDbType.VarChar, 16).Value = Row.Cells[12].Text;
command.Parameters.Add("@ID", SqlDbType.VarChar, 9).Value = "0" + Row.Cells[2].Text;