Search code examples
asp.net.netvb.netvisual-studioasp.net-controls

Input string was not in a correct format


Afternoon All,

I have a dropdown list which extracts data from my SQL database and gives the users two options to choose from (Weekly / Monthly). The database has an ID for each of these. Weekly is set to 1 and Monthly is set to 2. This drop down is linked to a gridview which extracts / displays the data based on the selected item. All of this works perfectly fine.

This issue i have is that i want to add some code in my Page_ load event to populate a text box with the selected item. I would also like to set the dropdownlist as default to weekly when a users access thie page. i thought that the two following bits of code would work but i get the messege 'Input string was not in a correct format'.

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    'This works fine
    lblTodaysDate.Text = GetDate()


    'I thought i could complete an If Statement to get the text box to work.
    If DropDownList1.SelectedValue = 1 Then
        txtMeeting.Text = "SMC Weekly Meeting"
    Else
        txtMeeting.Text = "SMC Monthly Meeting"
    End If


End Sub

Im new to .net but have read that i might need to convert my int to a string?

Any help in advance would be much appriechiated.

Regards Betty.


Solution

  • First check that your value is numeric, if it is, then convert it to an integer, and compare it to 1:

    If IsNumeric(DropDownList1.SelectedValue) AndAlso CInt(DropDownList1.SelectedValue)=1
        txtMeeting.Text = "SMC Weekly Meeting"
    Else
        txtMeeting.Text = "SMC Monthly Meeting"
    End If