lorena Posted December 14, 2004 Posted December 14, 2004 (edited) I am working with a datagrid that the user (hopefully) will be able to edit/update The columns are template columns. The grid loads fine but when I change the cell data and click "Update" - I get "Object reference not set to an instance of an object" Here is the update code: Sub dgRecs_UpdateRow(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Const strConnString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\intranet\databases\toolroom_sched.mdb" Dim textDwg As TextBox = e.Item.Cells(3).FindControl("txtDwg") Dim ddlT As DropDownList = e.Item.Cells(2).FindControl("ddlType") Dim irecordID As Integer irecordID = dgRecs.DataKeys(e.Item.ItemIndex) Dim strSQL As String strSQL = "UPDATE toolroom SET Type = @typeParam, @dwgParam WHERE RecID = @recordIDParam " Dim objConn As New OleDb.OleDbConnection(strConnString) Dim objCommand = New OleDb.OleDbCommand(strSQL, objConn) objConn.Open() objCommand.CommandType = CommandType.Text Dim recordIDParam As New OleDb.OleDbParameter("@RecordIDParam", OleDb.OleDbType.Single) recordIDParam.Value = irecordID objCommand.Parameters.Add(recordIDParam) Dim typeParam As New OleDb.OleDbParameter("@typeParam", OleDb.OleDbType.VarChar, 25) typeParam.Value = ddlT.SelectedItem.Text objCommand.Parameters.Add(typeParam) Dim dwgParam As New OleDb.OleDbParameter("@dwgParam", OleDb.OleDbType.VarChar, 25) dwgParam.Value = textDwg.Text objCommand.Parameters.Add(dwgParam) objCommand.ExecuteNonQuery() objConn.Close() dgRecs.EditItemIndex = -1 BindData() End Sub And part of the datagrid code: <asp:datagrid id="dgRecs" runat="server" AutoGenerateColumns="False" DataKeyField="Rec_ID" OnEditCommand="dgRecs_EditRow" OnUpdateCommand="dgRecs_UpdateRow" OnCancelCommand="dgRecs_CancelRow"> <Columns> <asp:EditCommandColumn ButtonType="LinkButton" HeaderText="EDIT" EditText="Edit" UpdateText="Update" CancelText="Cancel"></asp:EditCommandColumn> <asp:BoundColumn DataField="Rec_ID" HeaderText="Rec ID" ReadOnly="True"></asp:BoundColumn> <asp:TemplateColumn HeaderText="Type"> <ItemTemplate> <asp:Label Runat="server" id="lblType" Width="60px" text='<%# DataBinder.Eval(Container.DataItem, "Type") %>'/> </ItemTemplate> <EditItemTemplate> <asp: DropDownList Runat="server" ID="ddlType" Font-Name="Verdana" Font-Size="xx-small" DataTextField="Type" DataValueField="Type" DataSource="<%# GetTypes() %>"> </asp: DropDownList></EditItemTemplate></asp:TemplateColumn> <asp:TemplateColumn HeaderText="Dwg No" HeaderStyle-CssClass="tdBasic"> <ItemTemplate> <asp:Label Runat="server" id="lblDwgNo" Width="60px" Text=' <%# DataBinder.Eval(Container.DataItem, "Dwg_No") %>' /> </ItemTemplate> <EditItemTemplate> <asp:TextBox Runat="server" ID="txtDwgNo" Width="100px" Font-Name="Verdana" Font-Size="xx-small" Text='<%# DataBinder.Eval(Container.DataItem, "Dwg_No") %>'> </asp:TextBox></EditItemTemplate></asp:TemplateColumn> <asp:TemplateColumn HeaderText="Description"> I am just not sure what I am doing wrong. :( Edited December 14, 2004 by PlausiblyDamp Quote
Administrators PlausiblyDamp Posted December 14, 2004 Administrators Posted December 14, 2004 Which line actually generates the error? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
lorena Posted December 14, 2004 Author Posted December 14, 2004 Which line actually generates the error? Everything is fine until I click "Update" and this line: dwgParam.Value = textDwg.Text appears to be causing the problem Quote
Administrators PlausiblyDamp Posted December 14, 2004 Administrators Posted December 14, 2004 Check the line Dim textDwg As TextBox = e.Item.Cells(3).FindControl("txtDwg") and see if it is returning anything, Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
lorena Posted December 15, 2004 Author Posted December 15, 2004 The problem seems to be that even though the code says "Parameter.Add" - the SQL string doesn't contain the parameter values. If I output the query string at this point it is: "UPDATE toolroom SET Type = @typeParam, Dwg_No = @dwgParam WHERE RecID = @recordIDParam " Quote
lorena Posted December 15, 2004 Author Posted December 15, 2004 Never mind - I found the problem - the recordIDParam was out of sequence. Thanks for the help Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.