编辑:这不是一个重复的问题,因为我的问题比简单的NullReferenceException更复杂。
这是给我一些问题的代码:
private void BindData()
{
SqlConnection con = new SqlConnection(Connection.constr);
con.Open();
string[] Querys = new string[4];
Querys[0] = "PopulateConstraintTable";
Querys[1] = "PopulateModuleTable";
Querys[2] = "PopulateFeatureTable";
Querys[3] = "PopulateInterfaceTable";
for (int n = 0; n < 4; n++)
{
SqlCommand cmd = new SqlCommand(Querys[n], con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@License", SqlDbType.Int).Value = 20000 + MyGlobals.CurrentOrgID;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable Table = new DataTable();
da.Fill(Table);
((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();
cmd.ExecuteNonQuery();
}
con.Close();
}
此代码应该使用这些存储过程中的信息填充4个不同的Gridview,但是我在行中出现错误
((GridView)Page.FindControl("GridView" + (n + 1))).DataSource = Table;
((GridView)Page.FindControl("GridView" + (n + 1))).DataBind();
说:
An exception of type 'System.NullReferenceException' occurred in App_Web_30v1z0on.dll but was not handled in user code
Additional information: Object reference not set to an instance of an object.
请问有人带我走错路吗?谢谢!
编辑:标记 -<asp:GridView
ID="GridView1"
runat="server"
CellPadding="6"
GridLines="Horizontal"
Font-Names="Verdana"
Font-Size="10pt"
DataKeyNames="ConstraintID"
AutoGenerateColumns="false"
allowpaging="false"
Width="730px">
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" ForeColor="White" HorizontalAlign="Left" Height="25" Font-Bold="True" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>...</Columns>
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="false" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:GridView
ID="GridView2"
cellpadding="6"
runat="server"
GridLines="Horizontal"
Font-Names="Verdana"
Font-Size="10pt"
DataKeyNames="ModuleID"
AutoGenerateColumns="False"
onrowcancelingedit="GridView2_RowCancelingEdit"
onrowediting="GridView2_RowEditing"
onrowupdating="GridView2_RowUpdating"
Width="730px">
<HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />
<Columns>...</Columns>
</asp:GridView>
<asp:GridView
ID="GridView3"
runat="server"
CellPadding="6"
GridLines="Horizontal"
Font-Names="Verdana"
Font-Size="10pt"
DataKeyNames="FeatureID"
AutoGenerateColumns="false"
allowpaging="false"
onrowcancelingedit="GridView3_RowCancelingEdit"
onrowediting="GridView3_RowEditing"
onrowupdating="GridView3_RowUpdating"
OnRowDataBound="GridView3_RowDataBound"
Width="730px">
<EditRowStyle Font-Bold="True" />
<HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />
<Columns>...</Columns>
</asp:GridView>
<asp:GridView
ID="GridView4"
runat="server"
CellPadding="6"
GridLines="Horizontal"
Font-Names="Verdana"
Font-Size="10pt"
DataKeyNames="InterfaceID"
AutoGenerateColumns="false"
allowpaging="false"
onrowcancelingedit="GridView4_RowCancelingEdit"
onrowediting="GridView4_RowEditing"
onrowupdating="GridView4_RowUpdating"
OnRowDataBound="GridView4_RowDataBound"
Width="730px">
<EditRowStyle Font-Bold="True" />
<HeaderStyle BackColor="#336699" ForeColor="White" HorizontalAlign="Left" Height="25" />
<Columns>...</Columns>
</asp:GridView>
答案: 看到这篇文章如何被人们轰炸声称这是一个副本,因此我无法发表答案,我将把答案放在编辑中(所有的荣誉和赞美都归@ConnorsFan所有)
解决方案是用
替换两个问题代码行 ((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataSource = Table;
((GridView)Master.FindControl("MainContent").FindControl("GridView" + (n + 1))).DataBind();
答案 0 :(得分:2)
这是空的,
Page.FindControl("GridView" + (n + 1))
您可以粘贴标记,以便我们检查它。