J M
J M

Reputation: 1

return data as SqlDataReader

I am working on an ASP.NET project. I have a stored procedure in SQL Server which returns a list of data to show in a chart. This stored procedure works fine. I need the return data to be a SqlDataReader object to pass into the Chart.DataBindTable.

I call the stored procedure like this:

string spName = "spFetchDetails";
DbCommand dbCommand = this.ObjDatabase.GetStoredProcCommand(spName);
this.ObjDatabase.AddInParameter(dbCommand, "@Company", DbType.Int32, Company);
this.ObjDatabase.AddInParameter(dbCommand, "@Title", DbType.String, Title);
this.ObjDatabase.AddInParameter(dbCommand, "@DateRange", DbType.String, DateRange);

DataTable dt = new DataTable();
dt = this.ObjDatabase.ExecuteDataSet(dbCommand).Tables[0];

Here the code is returning the stored procedure result as a DataTable, I want to call ExecuteReader() method, but I am not sure how to do that.

Chart.DataBindTable(dt, "Example");

This is the code to render the chart.

Below is how I do in another chart and it is working.

SqlCommand cmd = new SqlCommand(
  "Select ai.ItemDescription, ai.ItemRate, los.sellingprice, los.mrp from aitem ai inner join losShopItemShipmentDetail los on los.itemid = ai.itemid", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();

Chart.DataBindTable(rdr, "ItemDescription");

How do I call a stored procedure this way? or is there a way to pass the DataTable into DataBindTable function?

public void DataBindTable (System.Collections.IEnumerable dataSource, string xField);

Upvotes: 0

Views: 155

Answers (2)

Michał Turczyn
Michał Turczyn

Reputation: 37525

You could try this (sample code to visualise how to use it):

Chart c = new Chart();
DataTable dt = new DataTable();
c.DataBindTable(dt.AsEnumerable());

Upvotes: 1

Raka
Raka

Reputation: 427

you can call the stored procedure as below.

SqlCommand cmd = new SqlCommand("<SqlProcedureName>", con)
cmd.CommandType = CommandType.StoredProcedure;

But in place of assigning datareader to chart control. Find another way to set the data only

Upvotes: 0

Related Questions