简体   繁体   中英

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.

Just try enclosing the desired value in quotes, like this:

If DropDownList1.SelectedValue = "1" Then
    txtMeeting.Text = "SMC Weekly Meeting"
Else
    txtMeeting.Text = "SMC Monthly Meeting"
End If

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

You'll need to check for IsPostBack otherwise the ddl SelectValue would be reset

NB: My Vb.Net is a bit rusty

ie

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

    If Me.IsPostBack Then

        '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
    Else
        ' Put your code to populate the ddl here


    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM