Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
Into my application there is a panel inside a panel inside a formview its look like below, what i am facing is the error message : " Object reference not set to an instance of an object." next to line: " if (ShwUpSocids.Tables[0].Rows.Count > 0) ". as i want the updatesocialpanel to be shown in case if the UsrType = 'Business';. Now if i use DataBound for the listview then i will not be able to bind when page is load as i think if i am mistaken please could explain how i can do it?

ASP.NET
<asp:Panel ID="EditUsrPan" runat="server">

                          <asp:FormView ID="EditUsrInfoFVw" runat="server">
                          <ItemTemplate>

 <asp:Panel ID="updatesocialpanel" runat="server">

</asp:Panel>

</ItemTemplate>
                          </asp:FormView>

                      </asp:Panel>


Below code is to show the updatesocialpanel in case if user are business

C#
protected void Page_Load(object sender, EventArgs e)
        {

if (Session["UsrNme"] != null)
                {
                    using (SqlConnection ShwUpSociCon = new SqlConnection(sc))
                    {
                        ShwUpSociCon.Open();

                        Panel updatesocial = (Panel)EditUsrInfoFVw.FindControl("updatesocialpanel");
                        DataSet ShwUpSocids = new DataSet();
                        SqlDataAdapter ShwUpSociADP = new SqlDataAdapter(@"SELECT * from UserInfo WHERE [UID] = @USER and UsrType = 'Business'", sc);
                        var use = Session["UsrNme"];
                        ShwUpSociADP.SelectCommand.Parameters.AddWithValue("@USER", use);
                        
                        ShwUpSociADP.Fill(ShwUpSocids);
                        
                        if (ShwUpSocids.Tables[0].Rows.Count > 0)
                        {
                            updatesocial.Visible = true;
                        }
                        else
                        {
                            updatesocial.Visible = false;
                        }
                    }
                }
                else
                {
                    viwapplipanel.Visible = false;
                }
}


Below code is to bind the listview and it locate into page_load:

C#
if (Session["UsrNme"] != null)
            {

                using (SqlConnection HPPersInfoCon = new SqlConnection(sc))
                {
                    HPPersInfoCon.Open();

                    SqlDataAdapter HPPersInfoSQLAD = new SqlDataAdapter(@"SELECT * From UserInfo Where UID=@UID", sc);

                    var usrinfoHP = Session["UsrNme"];

                    HPPersInfoSQLAD.SelectCommand.Parameters.AddWithValue("@UID", usrinfoHP);

                    DataSet HPPersInfoDS = new DataSet();
                    HPPersInfoSQLAD.Fill(HPPersInfoDS);

                    HPPersInfoLV.DataSource = HPPersInfoDS.Tables[0];
                    HPPersInfoLV.DataBind();

                }

            }
Posted
Comments
Richard Deeming 4-Aug-15 13:47pm    
Are you sure the error is on that line? I suspect it's more likely that it's on the following line:
updatesocial.Visible = true;

A FormView's child controls don't exist until it has been bound to some data. You haven't shown any code which would bind the EditUsrInfoFVw control to a data source, so the updatesocial variable will be null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900