简体   繁体   中英

How to show GridView inside Login with databind?

If outside the loginView, the gridview can show correctly!

Put inside Login View and use below code

<LoggedInTemplate> 
  <asp:GridView ID="GridView1" runat="server">
  </asp:GridView>          
</LoggedInTemplate>

((GridView)LoginView1.FindControl("GridView1")).DataSource = query;
((GridView)LoginView1.FindControl("GridView1")).DataBind(); 

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 22: ((GridView)LoginView1.FindControl("GridView1")).DataSource = query;

How to show GridView inside Login with databind in c#?

If login is a button in your application, then at the end of that code file, you have to write and you have to make one query to select that table.

Something like this:

SELECT * FROM [table_name];   // here, you can take table name which you want to bind

After that, you have to fill dataAdapter by dataset, and that dataset is bound via following code:

ds = databind();

The <LoggedInTemplate /> is only available after the user logs in. The NullReferenceException will occur if you try to access the grid before the user logs in. I would suggest you add a check like this

if(Request.IsAuthenticated)
{
    GridView gv = ((GridView)LoginView1.FindControl("GridView1"));
    if(gv != null)
    {
        gv.DataSource = query;
        gv.DataBind();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM