0
votes

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#?

2
Did you tried just GridView1.DataSource = query;? and than GridView1.DataBind(); Because ((GridView)LoginView1.FindControl("GridView1")) is null. - Andrew Orsich
try finding the control after checking Request.IsAuthenticated possibly in the page load. - Aravind

2 Answers

0
votes

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();
    }

}
0
votes

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();