I am having a problem trying to delete rows from an editable Data Grid. I have declared the grid as follows:-
<asp:datagrid id="DgSTDPE" runat="server" CssClass="datagrid" ShowHeader="False" SelectedItemStyle-BackColor="LightGreen"
AutoGenerateColumns="False" DataKeyField="t_dsn" BorderColor="#000000">
<EditItemStyle HorizontalAlign="Center" BackColor="LightGreen"></EditItemStyle>
<Columns>
<asp:BoundColumn DataField="t_dsn" ReadOnly="True" HeaderText="Demand Date DSN">
<ItemStyle HorizontalAlign="Right" Width="100px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="spc" ReadOnly="True" HeaderText="SPC">
<ItemStyle HorizontalAlign="Right" Width="100px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="dmc" ReadOnly="True" HeaderText="DMC">
<ItemStyle HorizontalAlign="Right" Width="100px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="nc" ReadOnly="True" HeaderText="NC">
<ItemStyle HorizontalAlign="Left" Width="50px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="iin" ReadOnly="True" HeaderText="IIN">
<ItemStyle HorizontalAlign="Right" Width="180px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="QTY">
<ItemTemplate>
<asp:TextBox Runat=server ID="demand_qty" Text='<%# Container.DataItem("demand_qty") %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="locat" ReadOnly="True" HeaderText="DEPT">
<ItemStyle HorizontalAlign="Left" Width="100px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="task_no" ReadOnly="True" HeaderText="TASK">
<ItemStyle HorizontalAlign="Left" Width="100px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="rdd" ReadOnly="True" HeaderText="RDD" DataFormatString="{0:dd-MM-yy}">
<ItemStyle HorizontalAlign="Left" Width="200px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="dmd_type" ReadOnly="True" HeaderText="TYPE">
<ItemStyle HorizontalAlign="Left" Width="60px"></ItemStyle>
<FooterStyle HorizontalAlign="Center" VerticalAlign="Middle"></FooterStyle>
</asp:BoundColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete" />
</Columns>
</asp:datagrid></div>
I have got the
VB program to update the database when I wish to write it back, but although I’ve added code to delete rows, I cannot get this to happen. My delete code is:-
Private Sub DgSTDPE_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DgSTDPE.DeleteCommand
Dim IntRowIndex As Integer
Dim objTable As DataTable
Dim objDelRow As DataRow ' the data row to be deleted
IntRowIndex = e.Item.ItemIndex 'set IntRowIndex to the index value of the selected row.
objTable = DataSet1.Tables("due_in")
objTable.Rows(IntRowIndex).Delete()
DgSTDPE.DataBind()
End Sub
There error I get when a I click a delete button is:-
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 348: objTable = DataSet1.Tables("due_in")
Line 349:
Line 350: objTable.Rows(IntRowIndex).Delete() ‘this line is where the failure occurs
Line 351:
Line 352:
Source File: c:\inetpub\wwwroot\WebOASIS\STDPM Print Demands Menu\STDPE.aspx.
vb Line: 350
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
WebOASIS.STDPE.DgSTDPE_DeleteCommand(Object source, DataGridCommandEventArgs e) in c:\inetpub\wwwroot\WebOASIS\STDPM Print Demands Menu\STDPE.aspx.
vb:350
System.Web.UI.WebControls.DataGrid.OnDeleteCommand (DataGridCommandEventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(O bject source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEve nt(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(Com mandEventArgs e)
System.Web.UI.WebControls.LinkButton.System.Web.UI .IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
Any ideas anyone?