Reputation: 641
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.
Upvotes: 1
Views: 1332
Reputation: 4330
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
Upvotes: 0
Reputation: 5787
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
Upvotes: 2
Reputation: 103428
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
Upvotes: 1