Mike Eghtebas
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.
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();
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.
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.
ASKER
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.
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("spAuunuaCo untMedCtr" , con);
cmd.CommandType = CommandType.StoredProcedur e;
cmd.Parameters.AddWithValu e("@Region ID", Session["RegionID"]);
cmd.Parameters.AddWithValu e("@YYYY", Session["YYYY"]);
cmd.Parameters.AddWithValu e("@StatTy pe_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();
}
Try this:
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Clear();
SqlDataAdapter da = new SqlDataAdapter("spAuunuaCo
cmd.CommandType = CommandType.StoredProcedur
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
cmd.Parameters.AddWithValu
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.