[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-05-07
10
Medium Priority
?
357 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 32

Accepted Solution

by:
Jamie McAllister MVP earned 2000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

656 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