Showing posts with label fields. Show all posts
Showing posts with label fields. Show all posts

Saturday, March 31, 2012

String to Date conversion

Im trying to convert the input of several dropdownlists into a Date, but I can get an error and dont know how to convert these fields to a date type...
This is the code:
Dim BirthDate As DateTime = CType(ddlDay.SelectedValue + ddlMonth.SelectedValue + ddlYear.SelectedValue, Date)
And now im here anyways :p ...how can I calculate someones age when I have his birth date?

It would probably be best if you keep the two types the same. Try using this:

Dim BirthDate As DateTime = CType(ddlDay.SelectedValue & "/" & ddlMonth.SelectedValue & "/" & ddlYear.SelectedValue, DateTime)


Try using something like this, for the calculation (assuming a sub, based on a button's click event):

Sub getDiff(Source as Object, E as EventArgs)
Dim myBirth as DateTime
Dim sDate as String
Dim Age As Integer
sDate=ddMonth.SelectedItem.Value & "/" & ddDay.SelectedItem.Text & "/" & ddYear.SelectedItem.Text
myBirth=cDate(sDate)
Age=DateTime.Now.Year - myBirth.Year
If DateTime.Now.Month < myBirth.Month Or (DateTime.Now.Month = myBirth.Month And DateTime.Now.Day < myBirth.Day) Then
Age = Age - 1
End If
lblDate.text="Age=" & Age.ToString
End Sub

Check out full code sample:
http://aspnet101.com/aspnet101/aspnet/codesample.aspx?code=calcage


Hi Peter Smith,

Give the following a try. Example:

Dim tempAsString = ddlDay.SelectedValue + "/" + ddlMonth.SelectedValue + "/" + ddlYear.SelectedValue
Dim birthDateAs DateTime = DateTime.Parse(temp)

The following function can be used to calculate Age, given a BirthDate. Example:

Public Shared Function Age(ByVal dateOfBirth As DateTime) As Integer
If DateTime.Today.Month < dateOfBirth.Month Or DateTime.Today.Month = dateOfBirth.Month And DateTime.Today.Day < dateOfBirth.Day Then
Return DateTime.Today.Year - dateOfBirth.Year - 1
Else
Return DateTime.Today.Year - dateOfBirth.Year
End If
End Function

Hope this helps.

Saturday, March 24, 2012

Stripping a Currency Symbol

I'm building a data-entry form which pulls several "money" data type fields from a database.
Formatting the data in my datagrid using {0:c} is the easy part, but the database rejects the currency symbols when I try to re-insert them into the database.

I've read several threads that say to use a separate "hidden" field to hold the unformatted data, but that doesn't help since the user is entering data using the currency symbol and I'd have to update the hidden field first anyway!!

Another option I'm hearing is to "strip off the currency symbol" before submitting the form to the database.

Can someone show me some code samples demonstrating "stripping" a string? I suppose the optimal way to do this would be to check for (and strip, if found) a non-numeric character at the beginning of the string, but I have no idea what the code syntax would be.

Thanks in advance...Hello, you can always store data in database using any type of characters, have you tried to use the field of currency in the database as a TEXT field, then you can store whatever data or sybmols you want in that field,
also, if you are using ms access, you might set at the table creation the type of a field to be currency and store data in it, without having to put the $ sign for example, it will directly add thaqt symbol to it!!!! but the easiet to use a Text field and don't worry about symbols.

also, in the datagrid, you migth use a templatecolumn, that you can render the way youwant to place the currency as you like,.

hope i was able to help..
best of luck.
Unfortunately I don't get to choose the datatype in the sql server tables, I have to code against what the DBA gives me!

I really just need code samples of how to test for a non-numeric character at the beginning of a string, and then strip it off it it finds one.

Thanks anyway!
string yourstring = "$50";
char firstchar = yourstring[0];
if (!( ((int)firstchar >= (int)'0') && (int)firstchar <= (int)'9') )) {
// bad, bad first char! ;)
}