Solved

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

Posted on 2009-05-07
10
351 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 32

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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
LVL 32

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 32

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 32

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

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Serialization error 8 42
What are MicroServices? 4 65
Data Analysis 7 57
SQL query joining 6 tables in asp.net 4 18
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

738 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