How do I convert string field to an Int and Date fields to store the data in the database?

L

Learner

Hello,
I am trying to store the data entered in a webform in the database. I
have few Int and one SmallDateTime filed in my table in SQL Server 2005
database. I have made a storedproc to store the values.

My parameters in the database are like this


@DealerShipID int,
@ReturnDate smallDateTime,
@Odometer varchar(6),
@Year int,
@MakeID int,
@ModelID int,
@SubModelID int

here is what I am trying to do to convert the data that was entered in
the webform to its correspoding type


CInt(_drplDealerShips.DataValueField.Trim), _

CDate(_txtReturnDate.Text.Trim), _
_txtOdometer.Text.Trim,
_

CInt(_txtYear.Text.Trim), _

CInt(_drplMake.DataValueField.Trim), _

CInt(_drplModels.DataValueField.Trim), _

CInt(_txtSubModel.Text.Trim)


But when I debug the application it is complaining about the below
error :

******************************************************************************
System.InvalidCastException was unhandled by user code
Message="Conversion from string "DealerShipID" to type 'Integer' is
not valid."
Source="Microsoft.VisualBasic"
StackTrace:
at
Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String
Value)
at NewVehicleFeed._btnNewVehicleFeedSave_Click(Object sender,
EventArgs e) in C:\Documents and Settings\devi\My Documents\Visual
Studio 2005\WebSites\GroundingDemo\NewVehicleFeed.aspx.vb:line 39
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String
eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection
postData)
at System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
*************************************************************************************


Could any body suggest me how do I convert the string values into Int
and SmallDateTime values?

Thanks
-L
 
E

Erik Funkenbusch

Could any body suggest me how do I convert the string values into Int
and SmallDateTime values?

There are several solutions. A few good choices might be the Convert
classs, which has a number of static methods to convert data from one type
to another, and the Parse method on most native types. So, you can, for
example, say string s = Integer.Parse(field.Text).

Of course, you may need to do some validation to ensure that the text is of
the right type, or handle exceptions if the parse fails.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top