error to save date to sql server table

son

Regular
Joined
Feb 21, 2004
Messages
67
i have three dropdownlist one is showing day, the other is showing month and the other is showing year they are as follows...

Visual Basic:
Private Sub ddlDay_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDay.Load

        Dim i As Integer
        For i = 1 To 31
            ddlDay.Items.Add(i)
        Next
        ddlDay.SelectedIndex = Date.Now.Day - 1
    End Sub

    Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.Load

        Dim i As Integer
        For i = 1 To 12
            ddlMonth.Items.Add(MonthName(i).ToString)
        Next
        ddlMonth.SelectedIndex = Date.Now.Month - 1
    End Sub

    Private Sub ddlYear_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlYear.Load

        Dim i As Integer
        For i = Date.Now.Year - 5 To Date.Now.Year + 5
            ddlYear.Items.Add(i)
        Next
        ddlYear.SelectedIndex = Date.Now.Year - (Date.Now.Year - 5)
    End Sub

i have this procedure that is saving to a table in my sql server database



Visual Basic:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim mydate As DateTime
     

       
         mydate = CDate((ddlDay.SelectedIndex + 1).ToString + "/" + (ddlMonth.SelectedIndex() + 1).ToString + "/" + (ddlYear.SelectedIndex()).ToString)

        







        Dim SqlSPParam2 As String = "@news_TitleEN"
        Dim SqlSPParam3 As String = "@news_TextEN"
        Dim SqlSPParam8 As String = "@news_dt"

        Dim SqlParams As SqlParameter() = {New SqlParameter(SqlSPParam3, TextBox3.Text), New SqlParameter(SqlSPParam3, TextBox2.Text), New SqlParameter(SqlSPParam8, mydate)}
        WfSqlDataPump.ExecuteNonQuery(connectionString, "sp_addnews", SqlParams)
        Response.Redirect("newsadmin.aspx")

    End Sub

it is giving me an error which states

Cast from string "30/4/5" to type 'Date' is not valid.
Exception Details: System.InvalidCastException: Cast from string "30/4/5" to type 'Date' is not valid.

can someone please help me would really appreciate it
thanks in advance
 
Can you run what your SQL string against the database?

You probably need to see how your date field is in the database and see how you can update that field, then use the same string in your code..
 
it works fine when i do it like that now i am using this code and it keeps the same date although i change it

Visual Basic:
 Dim mydate As String
        Dim a As DateTime


        mydate = ddlDay.SelectedValue.ToString + "/" + ddlMonth.SelectedValue.ToString + "/" + ddlYear.SelectedValue.ToString

        a = CDate(DateTime.Parse(mydate))

        Dim SqlSPParam2 As String = "@news_TitleEN"
        Dim SqlSPParam3 As String = "@news_TextEN"
        Dim SqlSPParam8 As String = "@news_dt"

        Dim SqlParams As SqlParameter() = {New SqlParameter(SqlSPParam3, TextBox3.Text), New SqlParameter(SqlSPParam3, TextBox2.Text), New SqlParameter(SqlSPParam8, a)}
        WfSqlDataPump.ExecuteNonQuery(connectionString, "sp_addnews", SqlParams)
        Response.Redirect("newsadmin.aspx")

    End Sub
 
Back
Top