By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,136 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,136 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.RaisePostBackEven t(String
eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection
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

Mar 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Try

Convert.ToInt32(string)
Convert.ToDateTime(string)

HTH

Elton Wang

"Learner" wrote:
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.RaisePostBackEven t(String
eventArgument)
at
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String
eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection
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

Mar 10 '06 #2

P: n/a
On 9 Mar 2006 18:27:36 -0800, Learner wrote:
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.
Mar 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.