Convert selectedindex value to an email recipient
-
Hey all, I wrote a simple web form that has a drop down list with various property locations. I need my code to assign a specific email address to a selectedindex value based on the location. For instance: If the user selects Detroit as the location, then the recipient of the mailmessage should be detroit@company.com. Can I do this correctly in an If/Then statement outside of my MailMessage sub? Like :
Dim Address As System.Net.Mail.MailAddress If ddlLocation.SelectedIndex = 1 Then Address = "helpdesk@turnberry.com" End If
Obviously I need to convert the string somehow, so any help here would be great. Thanks!! -
Hey all, I wrote a simple web form that has a drop down list with various property locations. I need my code to assign a specific email address to a selectedindex value based on the location. For instance: If the user selects Detroit as the location, then the recipient of the mailmessage should be detroit@company.com. Can I do this correctly in an If/Then statement outside of my MailMessage sub? Like :
Dim Address As System.Net.Mail.MailAddress If ddlLocation.SelectedIndex = 1 Then Address = "helpdesk@turnberry.com" End If
Obviously I need to convert the string somehow, so any help here would be great. Thanks!!You could do it that way, or you could just get the data from the SelectedItem in the ComboBox, or from the SelectedValue property if you're using a bound ComboBox, or looking up in a table you have. This really depends on how you filled the ComboBox and from what kind of source and the format of the email address. If it were me, and I'm making a couple of large assumptions here, I'd probably have the Location and email address in a table, bind the combobox to it setting the Location column as the DisplayMember and the email address as the ValueMember, then do something like:
Dim emailAddr As String = ComboBox.SelectedValue
Or
Dim emailAddr As String = ComboBox.SelectedValue @ "@someplace.com"
The second way is less flexible though.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
You could do it that way, or you could just get the data from the SelectedItem in the ComboBox, or from the SelectedValue property if you're using a bound ComboBox, or looking up in a table you have. This really depends on how you filled the ComboBox and from what kind of source and the format of the email address. If it were me, and I'm making a couple of large assumptions here, I'd probably have the Location and email address in a table, bind the combobox to it setting the Location column as the DisplayMember and the email address as the ValueMember, then do something like:
Dim emailAddr As String = ComboBox.SelectedValue
Or
Dim emailAddr As String = ComboBox.SelectedValue @ "@someplace.com"
The second way is less flexible though.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007As you mentioned using the value, I realized how complicated I was making this. Thanks for your direction. I just added a single line of code in my submit click event and added the emails to the ddl as values and everything works great!
Dim Email As New System.Net.Mail.MailMessage( _ "email@email.com", ddlLocation.SelectedValue)
Thanks again!