|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataGrid Update IssueI hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my database has been made in MSDE. I have a DataGrid, which was working fine. The user could edit,update, delete without a problem. However, I changed the DataGrid to make certain columns 'uneditable' (such as the primary key, TimeSheetID) ..The problem now is that one of my Update Stored Procedure is not working, as it references the Primary Key unique identifier column in the DataGrid. To explain further, the code reads as follows: private void Update_DataGrid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { System.Web.UI.WebControls.TextBox tId = new System.Web.UI.WebControls.TextBox(); System.Web.UI.WebControls.TextBox tHrs = new System.Web.UI.WebControls.TextBox(); //This is the problem line! tId = (System.Web.UI.WebControls.TextBox) e.Item.Cells[4].Controls[0]; tHrs = (System.Web.UI.WebControls.TextBox) e.Item.Cells[3].Controls[0]; SqlCommand myCommand = new SqlCommand("NewUpdateTimeSheet",sqlConnection1); myCommand.CommandType = CommandType.StoredProcedure; myCommand.Parameters.Add(new SqlParameter("@TimeSheetID",SqlDbType.Int,4)); myCommand.Parameters["@TimeSheetID"].Value = tId.Text; myCommand.Parameters.Add(new SqlParameter("@Hrs",SqlDbType.Float,8)); myCommand.Parameters["@Hrs"].Value = Convert.ToSingle (tHrs.Text); This code worked fine before I made TimeSheetID 'read only'. The reason I want it read only is because I do not want the user to be able to update it. However it needs to be included as it is the unique identifier that is used in the Stored Procedure 'WHERE' clause (WHERE TimesheetID = @TimesheetID). The reason it is not working is that whereas before the cell was editable, you could assign the value in the editable cell to the 'tId.Text', now this is not possible. What code would I use to tell the Update command to take the value from a normal DataGrid cell - not in edit mode basically?.. Sorry if that seems unclear, I am basically looking for a way of referencing the TimeSheetID in the DataGrid when performing my update stored procedure. The TimeSheetID is not editable. I tried myCommand.Parameters["@TimeSheetID"].Value = e.Item.Cells[4].Controls[0]; however this returned the "System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index" error message. For info my DataGrid is bound to DataView4 as Data Source, with a RowFilter Any suggestions on how to do this? Thanks Al |
|||||||||||||||||||||||