0
votes

I have finally gotten past the errors but now I ham having issue with my db not being update with my datgridview data. Sorry I am a newbie here and really don't know why it is not updating.

protected void gvKeyPersonnel_RowUpdating(object sender, EventArgs e) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = conn; conn.Open(); // SqlDataReader myReader = null; // myReader = cmd.ExecuteReader();

            foreach (GridViewRow row in gvKeyPersonnel.Rows)
            {

                                cmd.Parameters.Clear();
               cmd.CommandText = @"UPDATE SP2010_EDCStaffing_AppDB.dbo.CMS_Key_Personnel  SET  Name = @Name, VDCIDIQ = @VDCIDIQ, VDCFFS = @VDCFFS, VDCHIM = @VDCHIM, VDCWEBHOSTING = @VDCWEBHOSTING, VDCCWF = @VDCCWF WHERE ID = @id";

                cmd.Parameters.AddWithValue("@id", Convert.ToInt32(gvKeyPersonnel.DataKeys[row.RowIndex].Values[0]));

                cmd.Parameters.AddWithValue("@Name", row.Cells[1].Text);
                cmd.Parameters.AddWithValue("@VDCIDIQ", row.Cells[2].Text);
                cmd.Parameters.AddWithValue("@VDCFFS", row.Cells[3].Text);
                cmd.Parameters.AddWithValue("@VDCHIM", row.Cells[4].Text);
                cmd.Parameters.AddWithValue("@VDCWEBHOSTING", row.Cells[5].Text);
                cmd.Parameters.AddWithValue("@VDCCWF", row.Cells[6].Text);
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
    }


}

}

1
Why are you doing int @id = Convert.ToInt32(gvKeyPersonnel.DataKeys[row.RowIndex].Values[0]); ? this will not add the parameter to your command object instead do cmd.Paramaters.AddWithValue("@id", Convert.ToInt32(gvKeyPersonnel.DataKeys[row.RowIndex].Values[0])); do the similar for @name parameter as well.Habib
you need to google msdn command.Parameters.Add method use parameterized query's also wrap all your executing code around a try{}catch you do not need to call conn.Close(); the using handles that for you when doing auto disposalMethodMan
It doesn't like "Parameters". Do I need to set something first?Norris Chappell
Besides the concerns that the previous commenters have raised, are you absolutely certain that Values[1] exists?Bart van Nierop
Got past the Parameters error. But still getting the OOR range.Norris Chappell

1 Answers

0
votes

you

cmd.CommandText = @"UPDATE 

no need @ just

cmd.CommandText = "UPDATE 

and ensure your connection to database is open by

conn.Open();