Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagridview not showing up in webpage

I'm using MS Visual Studio 2010 (ASP.NET - C#) and MS SQL Server 2005, and I'm trying to retrieve all the records in my table tblEmployee from my database EMPLOYEES. There's no error when I debug my program in Visual Studio but when the localhost webpage opens, the datagridview dgvEmployee is not there. There are no retrieved records as well. Here's my code:

SqlConnection sConn;
SqlDataAdapter daEmp;
DataSet dsEmp;

const string sStr = "Server = Server-PC\\SQLEXPRESS; Database = EMPLOYEES; Integrated Security = SSPI";

protected void Page_Load(object sender, EventArgs e)
{
    sConn = new SqlConnection(sStr);
    daEmp = new SqlDataAdapter("SELECT * FROM tblEmployee", sConn);
    dsEmp = new DataSet();

    daEmp.Fill(dsEmp, "tblEmployee");

    dsEmp.Tables["tblEmployee"].PrimaryKey = new DataColumn[] { dsEmp.Tables["tblEmployee"].Columns["EmployeeID"] };

    dgvEmployee.DataSource = dsEmp.Tables["tblEmployee"];
}

This is the code in my Defaultpage.aspx:

<asp:GridView ID="dgvEmployee" runat="server">
</asp:GridView>

The database and table name is correct, the table have records, and I'm running both programs as Admin. I just can't figure out what's wrong... Is this regarding with the permissions?

like image 501
user2971155 Avatar asked Nov 13 '13 02:11

user2971155


2 Answers

You need to bind the grid, like this:

dgvEmployee.DataBind();

Note: Unlike Windows development (WinForms, WPF, etc.), the ASP.NET data controls require an explicit call to actually bind the data. On the upside, it is much easier to re-bind to data sources in ASP.NET than Windows development.


Another problem you are going to have is that, based upon the markup you posted for your GridView, you will not get any data once you actually call .DataBind(), because you have no Columns defined in your GridView. You can either explicitly define the columns in your markup or set the AutoGenerateColumns property to True, like this:

<asp:GridView ID="dgvEmployee" runat="server" AutoGenerateColumns="True">
</asp:GridView>

Note: Using the AutoGenerateColumns="True" route, will take the exact column names from your data source and use them as headers in your grid; so if you have columns with underscores or abbreviations (i.e. AVG_COST or CUSTOMER_ID) then they will display exactly like that, with underscores, in your output. This method is useful if you just want to dump the data out into a grid, as is.

To have control over the names of the columns, then you must explicitly define the columns of the grid, like this:

<asp:GridView ID="dgvEmployee" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField datafield="CustomerID" headertext="Customer ID"/>
        <asp:BoundField datafield="CompanyName" headertext="Company Name"/>
        <asp:Boundfield datafield="Address" headertext="Address"/>
    </Columns>
</asp:GridView>

Note: Set AutoGenerateColumns to False or you will end up with duplicate columns, as it will render your defined columns and then all of the columns in the data source.

like image 181
Karl Anderson Avatar answered Sep 21 '22 09:09

Karl Anderson


Use this method after you have given the data source to your grid.

dgvEmployee.DataBind()
like image 22
Saad Surya Avatar answered Sep 19 '22 09:09

Saad Surya