在 gridView 中显示数据时出现 'Object reference not set to an instance of an object.' 错误
Getting error of 'Object reference not set to an instance of an object.' when displaying data in a gridView
我想在用户单击 'Save' 按钮时将详细信息(作为输入从用户处获取)添加到网格视图中。但是当我点击 'save' 按钮时出现 'Object reference not set to an instance of an object' 错误。
这是后面的代码,
protected void btnSave_Click(object sender, EventArgs e)
{
string rootCaseID = txtRootCaseID.Text;
string root = txtRoot.Text;
string description = txtDes.Text;
DataTable rottCasetTbl = null;
DataRow dr = null; ;
dr["id"] = txtRootCaseID.Text;
dr["root"] = txtRoot.Text;
dr["description"] = txtDes.Text;
rottCasetTbl.Rows.Add(dr);
grdrootCase.DataSource = rottCasetTbl;
grdrootCase.DataBind();
}
前端代码,
<tr>
<td align="center">
<asp:GridView ID="grdrootCase" runat="server" ShowHeaderWhenEmpty="true"
EmptyDataText="No Records Found" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="None" Font-Size="Small"
Width="95%">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Root Case ID" DataField="id" />
<asp:BoundField HeaderText="Root" DataField="root" />
<asp:BoundField HeaderText="Description" DataField="description" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
谁能帮我解决这个问题。
您的 DataTable 设置为 null,您的 DataRow 也是如此
改变
DataTable rottCasetTbl = null;
DataRow dr = null;
至
DataTable rottCasetTbl = new DataTable();
//add the columns
rottCasetTbl.Columns.Add("id", typeof(String));
rottCasetTbl.Columns.Add("root", typeof(String));
rottCasetTbl.Columns.Add("description", typeof(String));
DataRow dr = rottCasetTbl.NewRow();
我想在用户单击 'Save' 按钮时将详细信息(作为输入从用户处获取)添加到网格视图中。但是当我点击 'save' 按钮时出现 'Object reference not set to an instance of an object' 错误。
这是后面的代码,
protected void btnSave_Click(object sender, EventArgs e)
{
string rootCaseID = txtRootCaseID.Text;
string root = txtRoot.Text;
string description = txtDes.Text;
DataTable rottCasetTbl = null;
DataRow dr = null; ;
dr["id"] = txtRootCaseID.Text;
dr["root"] = txtRoot.Text;
dr["description"] = txtDes.Text;
rottCasetTbl.Rows.Add(dr);
grdrootCase.DataSource = rottCasetTbl;
grdrootCase.DataBind();
}
前端代码,
<tr>
<td align="center">
<asp:GridView ID="grdrootCase" runat="server" ShowHeaderWhenEmpty="true"
EmptyDataText="No Records Found" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333"
GridLines="None" Font-Size="Small"
Width="95%">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField HeaderText="Root Case ID" DataField="id" />
<asp:BoundField HeaderText="Root" DataField="root" />
<asp:BoundField HeaderText="Description" DataField="description" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</td>
</tr>
谁能帮我解决这个问题。
您的 DataTable 设置为 null,您的 DataRow 也是如此
改变
DataTable rottCasetTbl = null;
DataRow dr = null;
至
DataTable rottCasetTbl = new DataTable();
//add the columns
rottCasetTbl.Columns.Add("id", typeof(String));
rottCasetTbl.Columns.Add("root", typeof(String));
rottCasetTbl.Columns.Add("description", typeof(String));
DataRow dr = rottCasetTbl.NewRow();