son Posted May 3, 2004 Posted May 3, 2004 hi have have 3 drop down lists one for day one for month and one for year... i would like to add validation to the user so that if for example they select from the drop down list the date 31st february it will tell them that it is an invalid date... anyone have any suggestions for me.. thanks in advance sonia... Quote
wessamzeidan Posted May 3, 2004 Posted May 3, 2004 You have to use a client script to do that. You can do it another way, when the user selects a month, fill the days ddl dynamicaly, this way the user won't be able to do a wrong selection, but it would require an additional postback to the server... Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
son Posted May 3, 2004 Author Posted May 3, 2004 hi i am trying to do it this way but it keeps on giving me errors that i have my input string in an incorrect format... can some one please help... getting really frustrated me been on it all day.. my code for the drop down lists is as follows Private Sub ddlMonth_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.Init 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.Init 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 the code i have to save the changes is as follows Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim a1 As Integer = ddlMonth.SelectedIndex If ddlDay.SelectedValue = "31" Then If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then Response.Write("<script>alert('Invalid Date')</script>") End If End If Dim mydate As String mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text Dim a As DateTime = 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) lblMessage.Text = "The News has been saved!" End Sub when i am debbugging the code it gets stuck where i have the a variable which is declared as a string and tells me that i have the input string in invalid format..... thanks in advance sonia.. ;) Quote
sj1187534 Posted May 3, 2004 Posted May 3, 2004 Can you please tell exactly where you are getting the error? I didn't understand what you are pointing at. Or maybe you can try this: ... Dim a as DateTime = cDate(mydate) ... SJ Quote
son Posted May 3, 2004 Author Posted May 3, 2004 (edited) hi thanks for ur reply i did try what u recommended but i still keep on getting the error - Cast from string "31/6/2004" to type 'Date' is not valid. i keep on getting the error cause since i am putting the values that i have in the three dropdownlists into a string and then passing it to datetime variable it brings this error that i have mentioned above. the error is cropping up in the code below where i have the "a" declared as datetime. its not accepting the string of "mydate" variable to be put into the textbox Dim a1 As Integer = ddlMonth.SelectedIndex If ddlDay.SelectedValue = "31" Then If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then Response.Write("<script>alert('Invalid Date')</script>") End If End If Dim mydate As String mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text Dim a As DateTime = mydate textbox1.text = a do u know how i can go around this.. thanks in advance..;) Edited May 3, 2004 by son Quote
sj1187534 Posted May 4, 2004 Posted May 4, 2004 Hi.. I have a few questions on your code. Can you paste the exact error? One of the questions is the usage of '+' for string concatenation. I think you need to use '&' for that. SJ hi thanks for ur reply i did try what u recommended but i still keep on getting the error - Cast from string "31/6/2004" to type 'Date' is not valid. i keep on getting the error cause since i am putting the values that i have in the three dropdownlists into a string and then passing it to datetime variable it brings this error that i have mentioned above. the error is cropping up in the code below where i have the "a" declared as datetime. its not accepting the string of "mydate" variable to be put into the textbox Dim a1 As Integer = ddlMonth.SelectedIndex If ddlDay.SelectedValue = "31" Then If a1 = 1 Or a1 = 3 Or a1 = 5 Or a1 = 8 Or a1 = 10 Then Response.Write("<script>alert('Invalid Date')</script>") End If End If Dim mydate As String mydate = ddlDay.SelectedItem.Text + "/" + ddlMonth.SelectedItem.Text + "/" + ddlYear.SelectedItem.Text Dim a As DateTime = mydate textbox1.text = a do u know how i can go around this.. thanks in advance..;) Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.