Solved

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

Posted on 2009-05-07
10
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

756 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