My GridView is not displaying data from datatable.
My code is -
ASPX Page -
----------
ASPX.CS Page -
Page_Load
{
LoadUsers();
}
private void LoadUsers()
{
try
{
BLLAviation2 obj = new BLLAviation2();
GridView1.DataSource = obj.LoadUsers();
GridView1.DataBind();
}
catch (Exception err) { err.Message.ToString(); }
}
--------
My requirement-
Get data from sql server in datatable (say table)
and make this table as a DataSource of GridView1.
I do not want to use any
Please help.
----
I am getting the data in the datatable, but it is not displayed while viewing the data.
Pradeep ShetPosted Dec 9, 2014, 6:40 AM
The reason for not showing columns is, you set AutoGenerateColumns property to false neither you have created column templates in gridview, then how it will bind the columns to gridview.
Set the column template like
Hoe this answers your question
Riddhi ValechaPosted Dec 9, 2014, 6:27 AM
Did all things...
Finally... made up a new project all together...
Now working...
Thanks a ton to all guys.....
Manish Kumar ChoudharyPosted Dec 6, 2014, 4:24 AM
Riddhi ValechaPosted Dec 6, 2014, 4:21 AM
obj.LoadUsers(); is returning the datatable with data.
The return type is datatype.
Method -
#region LoadUsers
public DataTable LoadUsers()
{
try
{
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "SP_LOADUSERS";
table = new DataTable();
adp = new SqlDataAdapter();
cmd.ExecuteNonQuery();
adp.SelectCommand = cmd;
adp.Fill(table);
if (table.Rows.Count > 0) { return table; }
else return null;
}
catch (Exception err) { err.Message.ToString(); return null; }
}
#endregion
GridView1.DataSource = obj.LoadUsers();
GridView1.DataBind();
Property - AutoGenerateColumns="True" is set to True.
Manish Kumar ChoudharyPosted Dec 6, 2014, 3:42 AM
Is ur obj.LoadUsers(); returning dataTable ?
if not make that function return type dataTable and then use
GridView1.DataSource = obj.LoadUsers();
GridView1.DataBind();
mark the AutoGenerateColumns="True" for ur grid view
Sanjay KumarPosted Dec 6, 2014, 2:57 AM
http://www.c-sharpcorner.com/UploadFile/47548d/how-to-bind-data-from-mysql-database-to-gridview-in-Asp-Net/