可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
I just rewrite the format string and it resolves the issue. Found that rewriting { } braces resolved it. Please try it.
回答2:
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);
回答3:
You have 15 format specifier in your format string, but only 14 parameters. Unless I have missed one, your format string is invalid.
/*
username={0}& ->username,
password={1}& -> password,
intResortID={2}& -> intresortid,
strpartysurname={3}& -> strPartySurname,
strpartyfirstname={4}& ->strPartyFirstName,
strpartyprefix={5}& -> intQuantityTotal,
intQuantityTotal={6}& -> ckDate,
dtmLine1={7}& -> BBRoom,
strRoomType1={8}& -> RoomQty,
intRooms1={9}& -> NoofNights,
intnights1={10}& -> flightNO,
strFlightNo&{11}& -> strNotes,
strNotes{12}& -> ResBookingID,
strBookingCode{13}& -> PromotionCode
strPromotionCode={14}", -> ????? Missing ????
);
*/
As far as date it concerned, you haven't shown how you got ckDate
, if it's a string
with date in it, or is it a DateTime
. Also, from parameter list, looks like ckDate
is mapped to something that is called "intQuantityTotal
".
If ckDate
is a string, then use DateTime.TryPrase
method to convert it actual DateTime.
If you are converting from string follow this, assuming your input date is in a string variable called "inputDate
":
DateTime checkInDate;
if (!DateTime.TryParse(inputDate, out checkInDate))
{
//This is error condition, which means your string date wasn't convertible to DateTime
}
else
{
// Variable checkInDate now contains converted DateTime.
// You can put your format string here and your DateTime should work fine.
}
回答4:
Try using DateTime.ParseExact Method
DateTime result = DateTime.ParseExact("2012-09-24", "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);
回答5:
I advise you to use DateTimes constructor when possible, and not bother with converting strings to dates.
DateTime t = new DateTime(2012, 9, 24);
If you still want to start from a string, you need to use variations of DateTime.Parse
The simplest thing would work:
DateTime t = DateTime.Parse("2012-9-24");
in order to explicitly specify the format of the date use DateTime.ParseExact
Since you mentioned that the date string is entered by the user, I would assume you want a feedback loop in case the user enters an malformed date. In this case you should use DateTime.TryParse
回答6:
loopedcode's answer is probably right: you don't have a parameter for the strpartyprefix format specifier.
In addition, you should explicitly format ckDate
into the format expected by holdbooking.asp
. This probably needs to be a culture invariant format.
E.g. if holdbooking.asp
is expecting 'yyyy-MM-dd', then replace
String.Format(" ...&dtmLine1={7}&...", ...,ckDate,...);
by:
String.Format(CultureInfo.InvariantCulture,
" ...&dtmLine1={7:yyyy-MM-dd}&...", ...,ckDate,...);