Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime conversion and parsing

My code gives the following error: Input string was not in a correct format.

with this hint:

When converting a string to DateTime, parse the string to take the date before putting each variable into the DateTime object

Below is my code: (Edited to only show problematic code)

 string username ="aits";
 string password = "12345";
 Int32 intresortid=7;
 string strPartySurname = "Kumar"; ;
 string strPartyFirstName = "Test";
 string strPartyPrefix = "Mr & Mrs";
 Int16 intQtyAdults=1;
 Int16 intQtyTotal=1;
 string PromotionCode = "TIF";
 string flightNO = "FlighDJ76";
 string strNotes = "<nl>Provide Breakfast<nl>Honeymooners";

 try
    {

        string url = string.Format("http://localhost/insHold.asp?username={0}&password={1‌​}&intresortid={2}&strPartySurname={3}&strPartyFirstName={4}&strPartyPrefix={5}&intQtyAdults={6}&intQtyTotal={7}&dtmLine1={8:yyyy-MM-dd}&strRoomType1={9}&intRooms1={10}&intnights1={11}&strFlightNo={12}&strNotes={13}&strBookingCode={14}&strPromotionCode={15}", username, password, intresortid, strPartySurname, strPartyFirstName, strPartyPrefix, intQtyAdults, intQtyTotal, CheckInDate, BBRoom, RoomQty, NoofNights, flightNO, strNotes, ResBookingID, PromotionCode);                                                                                                                                                                                                                                                        
        WebRequest request = HttpWebRequest.Create(url);
        WebResponse response = request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string urlText = reader.ReadToEnd();
        bookid = Convert.ToInt16(urlText);
    }
    catch (System.ApplicationException ex)
    {
        throw ex;
    }

I don't know how to correct this as my DateTime value: CheckInDate is already of type date.

Can somebody please tell me how to fix this or point me in the right direction.

like image 341
user1270384 Avatar asked Apr 15 '26 09:04

user1270384


1 Answers

try doing this like

DateTime ckDate = new DateTime(2012, 09, 24);

more can be found here http://www.dotnetperls.com/datetime

you can also convert to string to datetome but you have to check ahead of time the the string is date

 DateTime ckDate =Convert.DateTime(yourinputstring);

you can also use parse

DateTime ckDate=DateTime.Parse(yourinputstring);
like image 86
COLD TOLD Avatar answered Apr 16 '26 23:04

COLD TOLD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!