Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: Inserting date values into SQL


Message #1 by "Joseph Janick" <joseph_janick@u...> on Thu, 20 Feb 2003 17:39:39
If this isn't in the right subject forum, feel free to move it to a more 
appropriate one.  This involves ASP and VB issues as well as SQL.

I'm trying to insert values from dropdown lists into a SQL Field who's 
data type is 'datetime'.  Since the values from the lists are string, I've 
tried using Cdate to convert the data but I get an error when I do that:

System.InvalidCastException: Cast from string "" to type 'Date' is not 
valid.

Here's what I have in the code, server-side:

Public Sub btnSave_Click(ByVal sender As Object, ByVal e As 
System.EventArgs) Handles btnSave.Click
    Dim dateInfo As New System.Globalization.DateTimeFormatInfo()
    Dim MyMonth, MyMonth2, MyDay, MyDay2, MyYear, MyYear2 As Date
    MyMonth = CDate(ddlMonth.SelectedItem.Value)
    MyDay = CDate(ddlDay.SelectedItem.Value)
    MyYear = CDate(ddlYear.SelectedItem.Value)
    MyMonth2 = CDate(ddlMonth2.SelectedItem.Value)
    MyDay2 = CDate(ddlDay2.SelectedItem.Value)
    MyYear2 = CDate(ddlYear2.SelectedItem.Value)
    If txtWhatObjectGetsFocus.Text = "lnkAdd" Then
        'adding record to umv_messages SQL table
        Dim objSQLMsgInfo As New SQLComponent()
        objSQLMsgInfo.strConnection = objConstants.DBConnectionString_Umove
        objSQLMsgInfo.strSQL = "INSERT INTO umv_memoManagement 
(mgt_memoURL, mgt_memoDescription, mgt_forWhatGroup, 
mgt_forWhatMemberInGroup, mgt_effectiveStartDate, mgt_effectiveEndDate) _
	VALUES ('" & lstMemos.SelectedItem.Text & "' , '" & 
txtDescrip.Text & "' , '" & ddlGroup.SelectedItem.Value & "' , '" & 
ddlMember.SelectedItem.Value & "' , '" & _
	Trim(MyMonth) & "/" & Trim(MyDay) & "/" & Trim(MyYear) & "' , '" & 
Trim(MyMonth2) & "/" & Trim(MyDay2) & "/" & Trim(MyYear2) & "')"
        objSQLMsgInfo.ExecuteSqlStatement()
        'indicating message was added
        lblMessage.Text = "MESSAGE ADDED!"
    Else
        'update the umv_message table indicating message has been read
        Dim objSqlMessageUpdate As New SQLComponent()
        objSqlMessageUpdate.strConnection = 
objConstants.DBConnectionString_Umove
        objSqlMessageUpdate.strSQL = "UPDATE umv_memoManagement SET 
mgt_memoDescription = " & txtDescrip.Text & ", mgt_forWhatGroup = " & 
ddlGroup.SelectedItem.Value & ", mgt_forWhatMemberInGroup = " & 
ddlMember.SelectedItem.Value & _
	", mgt_effectiveStartDate = " & Trim(MyMonth) & "/" & Trim(MyDay) 
& "/" & Trim(MyYear) & ", mgt_effectiveEndDate = " & Trim(MyMonth2) & "/" 
& Trim(MyDay2) & "/" & Trim(MyYear2) & " _
	WHERE mgt_memoURL = " & Trim(lstMemos.SelectedItem.Text)
        objSqlMessageUpdate.ExecuteSqlStatement()
        'indicating message was updated
        lblMessage.Text = "MESSAGE UPDATED!"
    End If
End Sub

On the client-side for the Dropdown control "ddlMonth", I've given the 
alpha months numeric values.  I had hoped this would ease the date-
conversion process; apparently it hasn't worked.

Can anyone help?
Message #2 by "Peter Lanoie" <planoie@n...> on Thu, 20 Feb 2003 12:30:43 -0500
It looks like the cast is trying to convert an empty string...

System.InvalidCastException: Cast from string "" to type 'Date' is not
valid.

Which line of code is it crashing on?  Are all the DDLs returning values?


-----Original Message-----
From: Joseph Janick [mailto:joseph_janick@u...]
Sent: Thursday, February 20, 2003 17:40
To: aspx_beginners
Subject: [aspx_beginners] Inserting date values into SQL


If this isn't in the right subject forum, feel free to move it to a more
appropriate one.  This involves ASP and VB issues as well as SQL.

I'm trying to insert values from dropdown lists into a SQL Field who's
data type is 'datetime'.  Since the values from the lists are string, I've
tried using Cdate to convert the data but I get an error when I do that:

System.InvalidCastException: Cast from string "" to type 'Date' is not
valid.

Here's what I have in the code, server-side:

Public Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click
    Dim dateInfo As New System.Globalization.DateTimeFormatInfo()
    Dim MyMonth, MyMonth2, MyDay, MyDay2, MyYear, MyYear2 As Date
    MyMonth = CDate(ddlMonth.SelectedItem.Value)
    MyDay = CDate(ddlDay.SelectedItem.Value)
    MyYear = CDate(ddlYear.SelectedItem.Value)
    MyMonth2 = CDate(ddlMonth2.SelectedItem.Value)
    MyDay2 = CDate(ddlDay2.SelectedItem.Value)
    MyYear2 = CDate(ddlYear2.SelectedItem.Value)
    If txtWhatObjectGetsFocus.Text = "lnkAdd" Then
        'adding record to umv_messages SQL table
        Dim objSQLMsgInfo As New SQLComponent()
        objSQLMsgInfo.strConnection = objConstants.DBConnectionString_Umove
        objSQLMsgInfo.strSQL = "INSERT INTO umv_memoManagement
(mgt_memoURL, mgt_memoDescription, mgt_forWhatGroup,
mgt_forWhatMemberInGroup, mgt_effectiveStartDate, mgt_effectiveEndDate) _
	VALUES ('" & lstMemos.SelectedItem.Text & "' , '" &
txtDescrip.Text & "' , '" & ddlGroup.SelectedItem.Value & "' , '" &
ddlMember.SelectedItem.Value & "' , '" & _
	Trim(MyMonth) & "/" & Trim(MyDay) & "/" & Trim(MyYear) & "' , '" &
Trim(MyMonth2) & "/" & Trim(MyDay2) & "/" & Trim(MyYear2) & "')"
        objSQLMsgInfo.ExecuteSqlStatement()
        'indicating message was added
        lblMessage.Text = "MESSAGE ADDED!"
    Else
        'update the umv_message table indicating message has been read
        Dim objSqlMessageUpdate As New SQLComponent()
        objSqlMessageUpdate.strConnection 
objConstants.DBConnectionString_Umove
        objSqlMessageUpdate.strSQL = "UPDATE umv_memoManagement SET
mgt_memoDescription = " & txtDescrip.Text & ", mgt_forWhatGroup = " &
ddlGroup.SelectedItem.Value & ", mgt_forWhatMemberInGroup = " &
ddlMember.SelectedItem.Value & _
	", mgt_effectiveStartDate = " & Trim(MyMonth) & "/" & Trim(MyDay)
& "/" & Trim(MyYear) & ", mgt_effectiveEndDate = " & Trim(MyMonth2) & "/"
& Trim(MyDay2) & "/" & Trim(MyYear2) & " _
	WHERE mgt_memoURL = " & Trim(lstMemos.SelectedItem.Text)
        objSqlMessageUpdate.ExecuteSqlStatement()
        'indicating message was updated
        lblMessage.Text = "MESSAGE UPDATED!"
    End If
End Sub

On the client-side for the Dropdown control "ddlMonth", I've given the
alpha months numeric values.  I had hoped this would ease the date-
conversion process; apparently it hasn't worked.

Can anyone help?

Message #3 by "Jerry Lanphear" <jerrylan@q...> on Thu, 20 Feb 2003 11:31:33 -0700
cast dates this way          #01/23/2003#

Regards

----- Original Message -----
From: "Peter Lanoie" <planoie@n...>
To: "aspx_beginners" <aspx_beginners@p...>
Sent: Thursday, February 20, 2003 10:30 AM
Subject: [aspx_beginners] RE: Inserting date values into SQL


> It looks like the cast is trying to convert an empty string...
>
> System.InvalidCastException: Cast from string "" to type 'Date' is not
> valid.
>
> Which line of code is it crashing on?  Are all the DDLs returning values?
>
>
> -----Original Message-----
> From: Joseph Janick [mailto:joseph_janick@u...]
> Sent: Thursday, February 20, 2003 17:40
> To: aspx_beginners
> Subject: [aspx_beginners] Inserting date values into SQL
>
>
> If this isn't in the right subject forum, feel free to move it to a more
> appropriate one.  This involves ASP and VB issues as well as SQL.
>
> I'm trying to insert values from dropdown lists into a SQL Field who's
> data type is 'datetime'.  Since the values from the lists are string, I've
> tried using Cdate to convert the data but I get an error when I do that:
>
> System.InvalidCastException: Cast from string "" to type 'Date' is not
> valid.
>
> Here's what I have in the code, server-side:
>
> Public Sub btnSave_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles btnSave.Click
>     Dim dateInfo As New System.Globalization.DateTimeFormatInfo()
>     Dim MyMonth, MyMonth2, MyDay, MyDay2, MyYear, MyYear2 As Date
>     MyMonth = CDate(ddlMonth.SelectedItem.Value)
>     MyDay = CDate(ddlDay.SelectedItem.Value)
>     MyYear = CDate(ddlYear.SelectedItem.Value)
>     MyMonth2 = CDate(ddlMonth2.SelectedItem.Value)
>     MyDay2 = CDate(ddlDay2.SelectedItem.Value)
>     MyYear2 = CDate(ddlYear2.SelectedItem.Value)
>     If txtWhatObjectGetsFocus.Text = "lnkAdd" Then
>         'adding record to umv_messages SQL table
>         Dim objSQLMsgInfo As New SQLComponent()
>         objSQLMsgInfo.strConnection 
objConstants.DBConnectionString_Umove
>         objSQLMsgInfo.strSQL = "INSERT INTO umv_memoManagement
> (mgt_memoURL, mgt_memoDescription, mgt_forWhatGroup,
> mgt_forWhatMemberInGroup, mgt_effectiveStartDate, mgt_effectiveEndDate) _
> VALUES ('" & lstMemos.SelectedItem.Text & "' , '" &
> txtDescrip.Text & "' , '" & ddlGroup.SelectedItem.Value & "' , '" &
> ddlMember.SelectedItem.Value & "' , '" & _
> Trim(MyMonth) & "/" & Trim(MyDay) & "/" & Trim(MyYear) & "' , '" &
> Trim(MyMonth2) & "/" & Trim(MyDay2) & "/" & Trim(MyYear2) & "')"
>         objSQLMsgInfo.ExecuteSqlStatement()
>         'indicating message was added
>         lblMessage.Text = "MESSAGE ADDED!"
>     Else
>         'update the umv_message table indicating message has been read
>         Dim objSqlMessageUpdate As New SQLComponent()
>         objSqlMessageUpdate.strConnection 
> objConstants.DBConnectionString_Umove
>         objSqlMessageUpdate.strSQL = "UPDATE umv_memoManagement SET
> mgt_memoDescription = " & txtDescrip.Text & ", mgt_forWhatGroup = " &
> ddlGroup.SelectedItem.Value & ", mgt_forWhatMemberInGroup = " &
> ddlMember.SelectedItem.Value & _
> ", mgt_effectiveStartDate = " & Trim(MyMonth) & "/" & Trim(MyDay)
> & "/" & Trim(MyYear) & ", mgt_effectiveEndDate = " & Trim(MyMonth2) & "/"
> & Trim(MyDay2) & "/" & Trim(MyYear2) & " _
> WHERE mgt_memoURL = " & Trim(lstMemos.SelectedItem.Text)
>         objSqlMessageUpdate.ExecuteSqlStatement()
>         'indicating message was updated
>         lblMessage.Text = "MESSAGE UPDATED!"
>     End If
> End Sub
>
> On the client-side for the Dropdown control "ddlMonth", I've given the
> alpha months numeric values.  I had hoped this would ease the date-
> conversion process; apparently it hasn't worked.
>
> Can anyone help?
>
>
>
>


  Return to Index