How to pass in datetime parameters to your stored proc using asp.net

82 Views Asked by At

so i have a stored proc that i am passing in datetime params too return the data... The problem is i want to pass in default dates and times but not sure how to do this as i can only see datetime.now... here is my code...

public void RefreshLabeldata(int selectedProduct, DateTime shiftStart, DateTime shiftEnd)
{
    BizManager biz = new BizManager();

    DataSet dt = new DataSet();
    dt = biz.GetTotalPacked(
        shiftStart 
      , shiftEnd
      , selectedProduct).DataSet;
    labeltotal.Text = dt.Tables[0].Rows[0]["TotalPacked"].ToString();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Refreshdata(214, DateTime.Today, DateTime.Today.AddDays(1).AddMinutes(-1));
        BindDropDownList();
        RefreshLabeldata(214, DateTime.Today , DateTime.Today);
        ...
    }
...
}

I want to pass in the date that my sql stored proc accepts i.e shiftStart 2016-06-06 06:00 shiftEnd 2016-06-06 14:00.

I know my datetimes are all wrong :) i am still trying to figure this part out.

3

There are 3 best solutions below

0
On

I Think this help for you ...

String.Format("{0:yyyy-MM-dd hh mm}", YourDate)

Refer this link :http://www.csharp-examples.net/string-format-datetime/

3
On

If you are learning how to pass DateTime as a parameter then please go through DateTime documentation. Here

DateTime.Today will contain value something like 6/6/2018 12:00:00 AM. If you know exact date and time, then instead of using DateTime.Today you can use DateTime.ParseExact(dateString, format, provider).

where dateString will be your date in string format.

format will contain date time format e.g. "yyyy-MM-dd".

provider will be cultureInfo. For more details MSDN

To summarize, try with

RefreshLabeldata(214, DateTime.ParseExact("2018-06-06", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture) , DateTime.ParseExact("2018-06-07", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture))
0
On

To use current date with your requested format, try this:

 Refreshdata(214, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddDays(1).ToString("yyyy-MM-dd HH:mm"));