Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2014-08-26
7
Medium Priority
?
505 Views
Last Modified: 2014-08-26
The good solution from Fernando at: http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28505181.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

0
Comment
Question by:Mike Eghtebas
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 40286297
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.
0
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40286349
The error is: Cannot find table 0.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 40286365
It doesn't look like you're executing the stored procedure.  Add a cmd.execute before your fill command.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40286389
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.
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 40286597
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();
          }
0
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 2000 total points
ID: 40286603
Also you should generally wrap datasource access in a try...catch statement like on this page:

http://www.webcodeexpert.com/2013/02/how-to-bind-gridview-using_9775.html#.U_zoz8LD_Gg

That way you are always closing out your datasources when you are finished with them.
0
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40287111
Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question