-1

I want to parse a string value that is displayed in a label and then insert that value along with other values to a table in a database .. so I have the stored procedure that contains the insert statement and a method that performs performs the insert statement and finally I call the method in my button click event to perform the insert method but it gives me an exception message "Input string was not in correct format" and a tip which is "Parse the string to take the date before putting each variable into datetime object"

the call of this method in the button click event is:

DateTime billDateTime = DateTime.Parse(pBillDateLbl.Text);
purchaseCashBill.InsertGeneralCashBillInfo(billDateTime, pBillUserNameTxt.Text, Convert.ToInt32(pBillCompCmbo.SelectedValue), pBillCompCmbo.SelectedText, Convert.ToDouble(pBillItmTtlPrice.Text), true);  

I have also tried to pare the string using this way also but the same exception appears:

DateTime billDateTime = DateTime.ParseExact(
   pBillDateLbl.Text, 
  "d/M/yyyy hh:mm:ss", 
   System.Globalization.CultureInfo.CurrentCulture);

and the value of the label is in the form of:

pBillDateLbl.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss");  

appears like this in my application :05:45:22 05/04/2017

Mohammed Shfq
  • 65
  • 2
  • 11
  • what's the value of `pBillDateLbl.Text` – fubo May 04 '17 at 12:31
  • what is your input value (the string)? – rory.ap May 04 '17 at 12:32
  • 4
    It sounds like the problem has nothing to do with the SQL, and is purely about the parsing - so please provide a [mcve] showing that, including hard-coded input. It's important to perform diagnostic work to reduce the problem to a minimal example before posting: if you never get as far as the `InsertGeneralCashBillInfo` method, then that's not relevant IMO. – Jon Skeet May 04 '17 at 12:32
  • The only code you should have in your question is `DateTime billDateTime = DateTime.ParseExact(pBillDateLbl.Text, "d/M/yyyy hh:mm:ss", System.Globalization.CultureInfo.CurrentCulture);`. All other code and details are irrelevant. – rory.ap May 04 '17 at 12:33
  • @fubo the input to the label is in a form of day-month-year hour:muinets:seconds pBillDateLbl.Text = DateTime.Now.ToString("d/M/yyyy hh:mm:ss"); – Mohammed Shfq May 04 '17 at 12:34
  • @MohammedShfq -- No, what is the *actual value* at the time the exception is being thrown? Copy and paste it here. – rory.ap May 04 '17 at 12:34
  • @rory.ap this is the value 05:43:34 04/05/2017 – Mohammed Shfq May 04 '17 at 12:44
  • Possible duplicate of [Parse string to DateTime in C#](http://stackoverflow.com/questions/5366285/parse-string-to-datetime-in-c-sharp) – Thibault Cam May 04 '17 at 13:07

1 Answers1

0

Based on the value you provided in your comments -- 05:43:34 04/05/2017, you can clearly see that your format string is in the wrong order. Also, it contains an extra space at the end. Try this:

DateTime billDateTime = DateTime.ParseExact(pBillDateLbl.Text, "hh:mm:ss dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture);
rory.ap
  • 34,009
  • 10
  • 83
  • 174