Solved

How do I put the result of my stored procedure into a dataset in C#?

Posted on 2009-05-07
10
342 Views
Last Modified: 2013-12-17
I have written the attached code but I'm not too sure how I can put the result of my stored procedure into my dataset which will then allow me to bind my ReportViewer control to the filled dataset...
ReportViewer1.Visible = true;

        SqlConnection thisConnection = new SqlConnection(thisConnectionString);

        System.Data.DataSet thisDataSet = new System.Data.DataSet();

        SearchValue[0] = new SqlParameter("@DueDate", DropDownList1.SelectedValue);
 

        SqlCommand cmd = new SqlCommand("proc_ShowCrucialByDate", thisConnection);

        cmd.CommandType = CommandType.StoredProcedure;
 

        thisDataSet = cmd......?
 

        ReportDataSource datasource = new ReportDataSource("CrucialDatesDataSet_proc_ShowCrucialByDate", thisDataSet.Tables[0]);
 

        ReportViewer1.LocalReport.DataSources.Clear();

        ReportViewer1.LocalReport.DataSources.Add(datasource);

        if (thisDataSet.Tables[0].Rows.Count == 0)

        {

            lblMessage.Text = "Sorry, no products under this category!";

        }
 

        ReportViewer1.LocalReport.Refresh();

Open in new window

0
Comment
Question by:Shepwedd
  • 5
  • 4
10 Comments
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 24327569
You need to call the Fill() method of a SqlDataAdapter to fill a dataset;

Check out the code here;

http://msdn.microsoft.com/en-us/library/system.data.dataset(VS.71).aspx

Excerpt attached..
Dim adpProducts As SqlDataAdapter = new SqlDataAdapter()

      adpProducts.TableMappings.Add("Table", "Products")

      Dim cmdProducts As SqlCommand = _

      new SqlCommand("SELECT * FROM Products", cnNorthwind)

      adpProducts.SelectCommand = cmdProducts

      adpProducts.Fill(ds)

      cnNorthwind.Close()

Open in new window

0
 
LVL 9

Expert Comment

by:Deathrace
ID: 24327624
try the below code.
 

 cmd.CommandType = CommandType.StoredProcedure;

 // cmd.parameters stuff

 //create our DataAdapter and DataSet objects

SqlDataAdapter SqlDA = new SqlDataAdapter(cmd);

DataSet objDS = new DataSet("DataSetName");

		

//fill the dataset

SqlDA.Fill(objDS);
 

// thats it... you have objDs filled with storedpro information

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24334076
I have now changed my code to the attached but I get the following error:

"System.Data.SqlClient.SqlException: Procedure or function 'proc_ShowCrucialByDate' expects parameter '@CrucialID', which was not supplied"

I have a dropdownlist control which is bound to a field value (CrucialID) of one of my database tables. What I'm trying to do is pass the selected CrucialID to the dataset and then fill the dataset on that ID value. From the error it doesn't look like this ID is getting passed to my dataset?

Thanks.
ReportViewer1.Visible = true;

        SqlConnection thisConnection = new SqlConnection(thisConnectionString);

        SqlCommand cmd = new SqlCommand("CrucialDates.proc_ShowCrucialByDate", thisConnection);

        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        System.Data.DataSet thisDataSet = new System.Data.DataSet();

        SearchValue[0] = new SqlParameter("@CrucialID", DropDownList1.SelectedValue);

        da.Fill(thisDataSet);

        

        ReportDataSource datasource = new ReportDataSource("CrucialDatesDataSet_proc_ShowCrucialByDate", thisDataSet.Tables[0]);
 

        ReportViewer1.LocalReport.DataSources.Clear();

        ReportViewer1.LocalReport.DataSources.Add(datasource);

        if (thisDataSet.Tables[0].Rows.Count == 0)

        {

            lblMessage.Text = "No Crucial Dates are due!";

        }
 

        ReportViewer1.LocalReport.Refresh();

Open in new window

0
 
LVL 31

Accepted Solution

by:
Jamie McAllister MVP earned 500 total points
ID: 24334125
This is how I put together SQL Parameters. The value is set differently to yours;
SqlParameter startDateParameter = new SqlParameter("@startDate", SqlDbType.DateTime);

            startDateParameter.Direction = ParameterDirection.Input;

            startDateParameter.Value = startDate;//start date

            command.Parameters.Add(startDateParameter);

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24334146
I have taken my code from the below link...

http://www.codeproject.com/KB/webforms/ReportViewer.aspx

I am mearly trying to mirror this.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 24334195
I'm saying my tried and tested syntax is different to yours. Your choice what you use.

I don't see anywhere in your code where you assign the parameter array to the query (SearchValue).

In the example code there is a line which I've attached as a snippet, but I can't see an equivalent in your code?
thisDataSet = SqlHelper.ExecuteDataset(thisConnection, 

                      "ShowProductByCategory", SearchValue);

Open in new window

0
 

Author Comment

by:Shepwedd
ID: 24335886
Yes, if you read at the very top of the link that I sent you he is using microsoft application block which I am not using so I have to call the stored procedure via the SQL Command object without using the SQL Helper class, that's what I was attempting to do with my code. Do you know of a better way?

Thanks.
0
 
LVL 31

Expert Comment

by:Jamie McAllister MVP
ID: 24335931
Didn't you sort this out by raising yet another thread on the forum?

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24391737.html


0
 

Author Comment

by:Shepwedd
ID: 24336179
That thread used a different approach which created another problem if you read the thread. Your solution did work in the end (attached) so I will close this question and award the points.
ReportViewer1.Visible = true;

        SqlConnection thisConnection = new SqlConnection(thisConnectionString);

        SqlCommand cmd = new SqlCommand("CrucialDates.proc_ShowCrucialByDate", thisConnection);

        cmd.CommandType = CommandType.StoredProcedure;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        System.Data.DataSet thisDataSet = new System.Data.DataSet();

        SqlParameter crucialDueDateFromParameter = new SqlParameter("@DueDateFrom", SqlDbType.DateTime);

        SqlParameter crucialDueDateToParameter = new SqlParameter("@DueDateTo", SqlDbType.DateTime);

        crucialDueDateFromParameter.Direction = ParameterDirection.Input;

        crucialDueDateFromParameter.Value = tbCrucialSearchDateFrom.Text;

        cmd.Parameters.Add(crucialDueDateFromParameter);

        crucialDueDateToParameter.Direction = ParameterDirection.Input;

        crucialDueDateToParameter.Value = tbCrucialSearchDateTo.Text;

        cmd.Parameters.Add(crucialDueDateToParameter);

        da.Fill(thisDataSet);
 

        ReportDataSource datasource = new ReportDataSource("CrucialDatesDataSet_proc_ShowCrucialByDate", thisDataSet.Tables[0]);
 

        ReportViewer1.LocalReport.DataSources.Clear();

        ReportViewer1.LocalReport.DataSources.Add(datasource);

        if (thisDataSet.Tables[0].Rows.Count == 0)

        {

            lblMessage.Text = "No Crucial Dates are due!";

        }
 

        ReportViewer1.LocalReport.Refresh();

Open in new window

0
 

Author Closing Comment

by:Shepwedd
ID: 31579055
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now