Kyle Bartolo

Kyle Bartolo

  • NA
  • 15
  • 16.3k

SqlDataSource

Aug 26 2013 4:14 AM
Hi.

I am using Telerik HTML5 charts.  Basically i have flat tables in the database and frankly displaying the x/y axis from the columns i select.  Initially i had done everything without a WHERE clause whereby i will simply get the data between specified dates.  Though until i developed them, i just had a simple select statement.

Im using the <asp:SqlDataSource> element to call the stored procedure.  I am having trouble in how i can pass the parameters using this way.  Can someone provide me an example please?

Currently I have this.
<asp:SqlDataSource ID="ds1" runat="server" ConnectionString="<%$ ConnectionStrings:sqlConnRep %>"
 SelectCommandType="StoredProcedure" SelectCommand="MySpName" />

So this simply calls the sp and returns the column values.  Though i need to pass parameters into this sp.  it can be done from the code behind (which i am assuming that it must be from there) or from the aspx page.  I dont mind.

Thanks.

UPDATE:
I think im close but still in need of help.  I have added the parameters within the <asp:SqlDataSource> element

<SelectParameters>
            <asp:Parameter Name="fromDate" Direction="Output" Type="DateTime" DefaultValue="01-01-1900" />
            <asp:Parameter Name="toDate" Direction="Output" Type="DateTime" DefaultValue="01-01-1900" />
</SelectParameters>

In the code behind im then doing
protected void dsDtnTimMedByMon_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
            System.Data.Common.DbCommand command = e.Command;
            command.Parameters["@fromDate"].Value = _startDate;
            command.Parameters["@toDate"].Value = _endDate;
}

OR
ds1.SelectParameters.Add("fromDate", System.Data.DbType.Guid, _startDate.ToShortDateString());

But unfortunately, when debugging, as soon as the Selecting event ends, it just stops the execution and fails showing this error on the browser:

Runtime Error

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated


Any help please?


Answers (3)