Solved

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

Posted on 2009-05-07
10
345 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 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

22 Experts available now in Live!

Get 1:1 Help Now