-1

I have a sql procedure which is outputting the following @startdate as a datetime output. I am trying to get these values into my C# aspx.cs code.

C# Output parameters

 cmd5.Parameters.Add(new SqlParameter("@startdate", SqlDbType.DateTime, 100, ParameterDirection.Output, false, 0, 10, "startdate", DataRowVersion.Default, null));

I have a public variable called startdate and trying to assign the above output parameter to the variable as seen below:

startdate = DateTime.Parse(cmd5.Parameters["@startdate"].Value.ToString()); //output is {01/12/2017 00:00:00} (correct date coming from sql, wrong format)

I need to parse this variable into another procedure from the front end. This procedure expects nvarchar(35) in sql format: yyyy-MM-dd hh:MM:ss

How do i parse the above startdate global variable to the sql format in nvarchar(35) from here?

RA19
  • 709
  • 7
  • 28
  • `DateTime startdate = Convert.ToDateTime(cmd5.Parameters["@startdate"].Value);` – Dmitry Bychenko Feb 15 '19 at 09:46
  • @DmitryBychenko - output is still {01/12/2017 00:00:00} – RA19 Feb 15 '19 at 09:48
  • `DateTime.ToString`? See https://learn.microsoft.com/dotnet/standard/base-types/custom-date-and-time-format-strings . Your `startdate` is a `DateTime` (which is correct) and it does not have a format associated with it (which is also correct). If your sproc foolishly expects a string instead of using a `DATETIME` parameter, you'll need to convert it explicitly. – Jeroen Mostert Feb 15 '19 at 09:48
  • 4
    this smells like a problem that should be solved by making the stored procedures use `datetime` throughout, rather than using `nvarchar(35)`; is it even remotely possible to get them to change that? because that's just bad design – Marc Gravell Feb 15 '19 at 09:51
  • I have changed it to use datetime. Regardless the format of the datetime on C# is not the same as how sql expects it – RA19 Feb 15 '19 at 09:55
  • 01/12/2017 00:00:00 (this is how the variable outputs) – RA19 Feb 15 '19 at 09:56
  • 2
    "the format of the datetime on C# is not the same as how sql expects it" @RA19 `datetime` (SQL) and `DateTime` (.NET) **do not have a format** - they are numbers, not strings; if you talk in `datetime` and `DateTime`: it will all work correctly. If you talk in strings: it won't – Marc Gravell Feb 15 '19 at 09:57
  • It would be awesome if you could provide a [mcve]. Please include the C# source code, the stored proc source code, and the `CREATE TABLE` for the relevant tables. – mjwills Feb 15 '19 at 09:59
  • Possible duplicate of [Format DateTime.Now to yyyy-mm-dd](https://stackoverflow.com/questions/38823758/format-datetime-now-to-yyyy-mm-dd) – mjwills Feb 15 '19 at 09:59
  • I have changed the procedure to expect a datetime in all. However doesnt make sense i get the following error : The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. – RA19 Feb 15 '19 at 10:31
  • cmd5.Parameters.Add(new SqlParameter("@startdate", SqlDbType.DateTime, 100, ParameterDirection.Output, false, 0, 10, "startdate", DataRowVersion.Default, null)); – RA19 Feb 15 '19 at 10:31
  • Managed to figure it out using below answer – RA19 Feb 16 '19 at 10:53

1 Answers1

1

Try with
DateTime.TryParseExact(


In TryParseExact method you need to pass a string and expected format.

you can find more samples and description here https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?view=netframework-4.7.2

  • I am not wanting to parse it as a string anymore as seen in above comments. I want to send it as a datetime to the procedure but i get the following The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. – RA19 Feb 15 '19 at 10:32