Click here to Skip to main content
15,911,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Design Page
ASP.NET
<asp:Button ID="btnReport1" runat="server" Text="Report 1" Width="83px" OnClick ="btnReport1_Click"/>


<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns ="true" ViewStateMode="Enabled">
                       
                    </asp:GridView>

Coding Page
VB
Protected Sub btnReport1_Click(sender As Object, e As EventArgs) Handles btnReport1.Click
        Using conn1 As New SqlConnection(ConfigurationManager.ConnectionStrings("TERAMSConnectionString").ConnectionString)
            conn1.Open()
            Dim cmd As SqlCommand = New SqlCommand("Select * from Excel_table", conn1)
            Dim da As DataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataSet = New DataSet
            da.Fill(dt)
            GridView1.DataSource = dt
            GridView1.DataBind()
            conn1.Close()
        End Using
    End Sub
Posted
Updated 28-Oct-13 4:58am
v2
Comments
Richard C Bishop 28-Oct-13 12:04pm    
Any errors? Where does it fail when debugging?
Ankit Mishra 28-Oct-13 23:12pm    
No errors page just refresh without displaying the gridview.
Please share the Page Load code.

You are populating the records to a dataset. So while binding the datasource you have to specify the index of the table to be used from the dataset. eg: dt.Tables(0)
 
Share this answer
 
Comments
Ankit Mishra 29-Oct-13 5:16am    
Hi Madhu i have tried this but still its not displaying the data - GridView1.DataSource = Tables(0)
Madhu Nair 29-Oct-13 5:18am    
it should be like -

GridView1.DataSource = dt.Tables(0)

Also Debug & check if dataset is getting populated with any data or not
Ankit Mishra 29-Oct-13 5:31am    
Hi Madhu DataSet is getting populated with data, is there any problem with aspx page.
Madhu Nair 29-Oct-13 5:42am    
have you change the data source code to like given below - ?

GridView1.DataSource = dt.Tables(0)
Ankit Mishra 29-Oct-13 6:24am    
Yes, Madhu could you please give the fresh code for design and backend page.
Madhu Nair is absolutely Right.

get the datatable out of the returning dataset and than bind your gridview with that datatable.

incase you still got nothing, than put the debugger on the line where you are filling the datataable. and check either query is returning some data or not.

Hope it will help. :)
 
Share this answer
 
Comments
Ankit Mishra 29-Oct-13 5:18am    
After debugging the data is there in datatable but still no display of data :)
VICK 29-Oct-13 8:44am    
is the following line throwing some exception.

GridView1.DataBind()

??
Ankit Mishra 29-Oct-13 8:56am    
No Vick
Please specify the index of table which dataset returns as like dt.Table(0) and another thing is that when you returning the value or data from database and it's return null or zero record then you could not be see the gridview because gridview only display when dataset have any data. so, displaying gridview at run time without containing data you should to create dynamic table at run time......I hope you understand what i am trying to say.

Thanx....:-)
 
Share this answer
 
Comments
Ankit Mishra 29-Oct-13 5:19am    
Thanks for help 'create dynamic table at run time' i didn't get this i am very new to .NET :)
Chintan Desai1988 29-Oct-13 5:34am    
It's really easy to create dynamic table at run time if you want code then tell me will give you.
Ankit Mishra 29-Oct-13 7:49am    
Yes Please for sure.
You can create this kind of function if your dataset not return any value from database.
C#
public void DynamicTable()
   {
       string Id = "id";
       string Name = "name";

       DataTable dt = new DataTable();

       DataColumn dc1 = new DataColumn(Id,typeof(System.String));
       dt.Columns.Add(dc1);

       DataColumn dc2 = new DataColumn(Name, typeof(System.String));
       dt.Columns.Add(dc2);

       DataRow dr = dt.NewRow();
       dr["id"] = "No Record";
       dr["name"] = "No Record";
       dt.Rows.Add(dr);

       GridView1.DataSource = dt;
       GridView1.DataBind();
   }
 
Share this answer
 
v2

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