Comboxes + Date?

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
Hi,

I have three comboboxes, one for day, one for month, one for year. i need to combine these into a single string in order to update a Date of Birth entry in my database,

Heres what i have so far:

Dim day As Integer = Integer.Parse(dobDayComb.SelectedItem.ToString())
Dim month As Integer = Integer.Parse(dobMonthComb.SelectedItem.ToString())
Dim year As Integer = Integer.Parse(dobYearComb.SelectedItem.ToString())
Dim myDate As New Date(day, month, year)


myCommand.CommandText = "INSERT INTO table(dob) " & _
" VALUES ('?theDob');" & _



myCommand.Parameters.AddWithValue("?theDob", myDate)

apologies for the code, im a forum noob, dunno how to embed code :(
 

kulrom

Well-known member
Joined
May 10, 2005
Messages
2,854
Location
Republic of Macedonia
Programming Experience
10+
 
        Dim day As String = dobDayComb.SelectedItem.ToString
        Dim month As String = dobMonthComb.SelectedItem.ToString
        Dim year As String = dobYearComb.SelectedItem.ToString
        Dim myDate As New Date(year, month, day)
        
        myCommand.CommandText = "INSERT INTO table(dob) VALUES (?);"
        myCommand.Parameters.AddWithValue("ParamName", myDate)
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,746
Location
Sydney, Australia
Programming Experience
10+
Why not just use a DateTimePicker? Easier for the user, easier for you. Most importantly, there's no way that the user can specify an invalid date, e.g. February 31.

Also, get rid of the single quotes around your parameter in the SQL code.
 

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
Why not just use a DateTimePicker? Easier for the user, easier for you. Most importantly, there's no way that the user can specify an invalid date, e.g. February 31.

Also, get rid of the single quotes around your parameter in the SQL code.

i knoooowww lol, i just wanted to look a bit more swish with my combo boxes, you know, like a web form, your gonna tell me im a fool and that datetimepickers Are cooler than combo boxes now aren't you? lol ur right though, that would be much easier considering i already know how to do that!!
 

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
no wait, scratch that, i had a nightmare with the datetimepicker coz my mySql datatype is a date column however the dateTimePicker kept submitting it as a date and a time, gave me headaches with the formatting, any workarounds you know, ill post an example if you want?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,746
Location
Sydney, Australia
Programming Experience
10+
There is no data type for date only in .NET. The DateTime type always has a date portion and a time portion. If you want to use just the date portion, you simply get the Date property of your DateTime value and that will return another DateTime with the same date and the time zeroed.
Dim dt1 As Date = Date.Now
Dim dt2 As Date = dt1.Date
Dim dt3 As New Date(dt1.Year, dt1.Month, dt1.Day)

MessageBox.Show(dt1.ToString())
MessageBox.Show(dt2.ToString())
MessageBox.Show(dt3.ToString())
In case you're not aware, Date is the VB alias for the .NET DateTime type, just as Integer is the VB alias for the Int32 type. It is not the case that Date contains just a date and DateTime contains date and time. They are just two names for the same type.
 

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
thats great, thanks, im still unsure of exactly how to pass the SELECTED DATE in the datetimepicker into a string though :S
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,746
Location
Sydney, Australia
Programming Experience
10+
Why exactly do you need a String? I called ToString in my example because I was using a MessageBox, which displays a String. You want to use your value as a parameter for a MySqlCommand, so you should be using a Date, not a String.
 

Latest posts

Top Bottom