Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

populate gridview, stored proc, codebehind, asp.net c#

The good solution from Fernando at: https://www.experts-exchange.com/questions/28505181/gridview-not-populating-asp-net-c.html

Works when I am populating a gridview using a table. But here with stored procedure I am getting an error at line 11.

Question: How can I correct this?

Thank you.
       SqlCommand cmd = new SqlCommand();
        cmd.Parameters.Clear();
        SqlDataAdapter da = new SqlDataAdapter("spAuunuaCountMedCtr", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RegionID", Session["RegionID"]);
        cmd.Parameters.AddWithValue("@YYYY", Session["YYYY"]);
        cmd.Parameters.AddWithValue("@StatType_ID", Session["StatType_ID"]);
        var ds = new DataSet();
        da.Fill(ds);
        // Assign the Table to the grids datasource before data binding it.
       // grdHighLight.DataSource = ds.Tables[0];
        grdHighLight.DataBind();

Open in new window

Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

What error are you getting? If you uncomment line 11, it looks like it should work at first glance. Even using a stored procedure, you still have to assign the gridview a datasource before you try to populate it.
Avatar of Mike Eghtebas

ASKER

The error is: Cannot find table 0.
It doesn't look like you're executing the stored procedure.  Add a cmd.execute before your fill command.
There is new error at cmd.execute;:

var ds = new DataSet();
cmd.execute;
da.Fill(ds);

(local variable)sqlCommand cmd

Error:
Only assignment, all, increment, and new object expressions can be used as a statement.
Sorry, ExecuteReader is for SQLDataReader, not adapters.

Try this:

SqlCommand cmd = new SqlCommand();
        cmd.Parameters.Clear();
        SqlDataAdapter da = new SqlDataAdapter("spAuunuaCountMedCtr", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@RegionID", Session["RegionID"]);
        cmd.Parameters.AddWithValue("@YYYY", Session["YYYY"]);
        cmd.Parameters.AddWithValue("@StatType_ID", Session["StatType_ID"]);
        var ds = new DataSet();
        da.Fill(ds);
        // Assign the Table to the grids datasource before data binding it.
        if (ds.Tables[0].Rows.Count > 0)
          {
        grdHighLight.DataSource = ds;
        grdHighLight.DataBind();
          }
          else
        {
        grdHighLight.DataSource = null;
        grdHighLight.DataBind();
          }
ASKER CERTIFIED SOLUTION
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you.