Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,
I have the following exception. Please have a look at this.

String was not recognized as a valid DateTime.
VB
[FormatException: String was not recognized as a valid DateTime.]
   booking.btSubmit_Click(Object sender, EventArgs e) in e:\Live Projects\VOGwebsite\booking.aspx.cs:283
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565


Following is the details of code and store procedure.
C# code:-
SQL
cmd.Connection = con.getCon();
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.CommandText = "sp_InsertBookingInfo";
         cmd.Parameters.Add("@BookingType", SqlDbType.VarChar).Value = bookingtype.ToString();
         cmd.Parameters.Add("@RoomType", SqlDbType.VarChar).Value = drpRoom.SelectedItem.ToString();
         cmd.Parameters.Add("@Package", SqlDbType.VarChar).Value = drpPackage.SelectedItem.ToString();
         cmd.Parameters.Add("@TourType", SqlDbType.VarChar).Value = drpTour.SelectedItem.ToString();
         cmd.Parameters.Add("@SubTourType", SqlDbType.VarChar).Value = drpSubTour.SelectedItem.ToString();
         cmd.Parameters.Add("@ArivalDate", SqlDbType.DateTime).Value = Convert.ToDateTime( txtArival.Text);
         cmd.Parameters.Add("@CheckIn", SqlDbType.DateTime).Value = Convert.ToDateTime( txtCheckIn.Text);
         cmd.Parameters.Add("@CheckOut", SqlDbType.DateTime).Value =Convert.ToDateTime(  txtCheckOut.Text);
         cmd.Parameters.Add("@AdultCount", SqlDbType.Int).Value = drpAdult.SelectedValue.ToString();
         cmd.Parameters.Add("@ChildCount", SqlDbType.Int).Value = drpChild.SelectedValue.ToString();
         cmd.Parameters.Add("@StayPeriod", SqlDbType.VarChar).Value = txtStay.Text;
         cmd.Parameters.Add("@PaymentMode", SqlDbType.VarChar).Value = drpPayment.SelectedValue.ToString();
         cmd.Parameters.Add("@BookingComments", SqlDbType.VarChar).Value = txtQuery.Text;
         cmd.Parameters.Add("@BookingId", SqlDbType.Int).Direction = ParameterDirection.Output;
         cmd.ExecuteNonQuery();
         bookingid = Convert.ToInt32(cmd.Parameters["@BookingId"].Value.ToString());
         //bookingid = Convert.ToInt32(cmd.ExecuteScalar().ToString());


The Store Procedure is as follows:-

ALTER PROCEDURE [dbo].[sp_InsertBookingInfo]

@BookingType varchar(50),
@RoomType varchar(50)=null,
@Package varchar(50)=null,
@TourType varchar(50)=null,
@SubTourType varchar(50)=null,
@ArivalDate datetime=null,
@CheckIn datetime=null,
@CheckOut datetime=null,
@AdultCount int=null,
@ChildCount int=null,
@StayPeriod varchar(50)=null,
@PaymentMode varchar(50)=null,
@BookingComments varchar(MAX),
@BookingId int output

AS

BEGIN

SET NOCOUNT ON;
Insert into BookingInfo(BookingType, RoomType,Package, TourType, SubTourType, ArivalDate, CheckIn, CheckOut,AdultCount, ChildCount, StayPeriod, PaymentMode, BookingComments)
values(@BookingType, @RoomType,@Package, @TourType, @SubTourType, @ArivalDate, @CheckIn, @CheckOut, @AdultCount, @ChildCount, @StayPeriod, @PaymentMode, @BookingComments)
set @BookingId= SCOPE_IDENTITY();
END

i don't able to find the exception because their is no error in data mismatch.
so where is the problem i am understand.
Please help me
Thanks.
Balwant.
Posted
Comments
Sergey Alexandrovich Kryukov 17-Apr-11 13:18pm    
OK, and where is that line 283? It's good you provided exception stack, but what's the use? You should show a relevant piece of code and the data in the offending call, in this case this should be a sting to be parsed as DateTime.
--SA

MIDL
cmd.Parameters.Add("@CheckIn", SqlDbType.DateTime).Value = Convert.ToDateTime( txtCheckIn.Text);
cmd.Parameters.Add("@CheckOut", SqlDbType.DateTime).Value =Convert.ToDateTime(  txtCheckOut.Text);


Try debugging your source code. The error is probably occuring in one of the lines above.
The textbox contains a string that cannot be converted to a date time field. Hence this error.

Try using DateTime.TryParse(). That will avoid such errors in the future.
 
Share this answer
 
Comments
Pong D. Panda 17-Apr-11 21:18pm    
Nice spot!
Abhinav S 18-Apr-11 4:24am    
Thanks
Balwant.mnd 17-Apr-11 23:42pm    
Hi, no error is not due to this because i had added this instead of txt.CheckIn.Text.
So i am unable to figure out this problem......
Balwant.mnd 18-Apr-11 12:52pm    
DateTime.TryParser() does not work...........
Abhinav S 19-Apr-11 2:01am    
DateTime.TryParse() not Parser(). :)
Hi,

I think this is due to store procedure return the bookingId as output parameter. while i am using ExecuteNonQuery(); which is only used when we just insert data.

Please have a look at this point.

If their is any other way ot insert data. Please specified.

Thanks,

Balwant
 
Share this answer
 
MIDL
//Set default value for output parameter
command.Parameters.Add("@BookingId",  SqlDbType.Int, 4).Value = 0
cmd.Parameters.Add("@BookingId", SqlDbType.Int).Direction = ParameterDirection.Output;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900