Hi all!

I need help getting the value of a selected listbox and transfer it to a textbox.text. I tried everything but seems that I am only getting frustrated.

There's something definitely worng with my code:

VB Code:
  1. Imports System.Web.Mail
  2.  
  3. Public Class WebForm1
  4.     Inherits System.Web.UI.Page
  5.  
  6. #Region " Web Form Designer Generated Code "
  7.  
  8.     'This call is required by the Web Form Designer.
  9.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  10.  
  11.     End Sub
  12.     Protected WithEvents Button2 As System.Web.UI.WebControls.Button
  13.     Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
  14.     Protected WithEvents ListBox2 As System.Web.UI.WebControls.ListBox
  15.     Protected WithEvents ListBox3 As System.Web.UI.WebControls.ListBox
  16.  
  17.     'NOTE: The following placeholder declaration is required by the Web Form Designer.
  18.     'Do not delete or move it.
  19.     Private designerPlaceholderDeclaration As System.Object
  20.  
  21.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  22.         'CODEGEN: This method call is required by the Web Form Designer
  23.         'Do not modify it using the code editor.
  24.         InitializeComponent()
  25.     End Sub
  26.  
  27. #End Region
  28.  
  29.     Public Sub AutoGen(ByRef EmailMessage As String)
  30.         Dim RanInteger As Integer
  31.         Randomize()
  32.         RanInteger = Int((3 - 1 + 1) * Rnd() + 1)
  33.         Select Case RanInteger
  34.             Case 1
  35.                 EmailMessage = "Dear Mr. Yehhui," & _
  36.                                "<br>" & _
  37.                                "<br> USCNC has cheap and efficient products! Please visit our site!" & _
  38.                                "<br>" & _
  39.                                "<br> <a href=""\\server\public\IT Folder\Ian Paul\Ian\a.htm"">[url]http://localhost/autoemail/a.htm[/url] </a> "
  40.             Case 2
  41.                 EmailMessage = "Dear Yehhui," & _
  42.                                "<br>" & _
  43.                                "<br> We, at USCNC, have a huge selection of IT products at our disposal." & _
  44.                                "<br>" & _
  45.                                "<br> Please click to this link." & _
  46.                                "<br>" & _
  47.                                "<br> <a href=""\\server\public\IT Folder\Ian Paul\Ian\a.htm"">[url]http://localhost/autoemail/a.htm[/url] </a> "
  48.             Case 3
  49.                 EmailMessage = "Mr. Yehhui," & _
  50.                                "<br>" & _
  51.                                "<br> Want to buy a cool computer but short of cash?" & _
  52.                                "<br>" & _
  53.                                "<br> Then we have the solution for you." & _
  54.                                "<br>" & _
  55.                                "<br> USCNC have a lot of cool computers at affordable prices." & _
  56.                                "<br>" & _
  57.                                "<br> Want to know more?" & _
  58.                                "<br>" & _
  59.                                "<br> Please click this link." & _
  60.                                "<br> <a href=""\\server\public\IT Folder\Ian Paul\Ian\a.htm"">[url]http://localhost/autoemail/a.htm[/url] </a> "
  61.         End Select
  62.     End Sub
  63.  
  64.     Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  65.         Dim AutoEmail As New MailMessage
  66.         Dim EmailMessage As String
  67.         Dim ad As ListItem
  68.         Dim ae As Integer
  69.         Dim x As String
  70.         AutoGen(EmailMessage)
  71.         ad = ListBox1.Items.Item(ae)
  72.         MsgBox(ad)
  73.         AutoEmail.To = ListBox1.SelectedValue
  74.         AutoEmail.From = "[email protected]"
  75.         AutoEmail.Subject = "Test subject from VB.NET"
  76.         AutoEmail.BodyFormat = MailFormat.Html
  77.         AutoEmail.Body = EmailMessage
  78.         AutoEmail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25")
  79.         System.Threading.Thread.Sleep(200)
  80.         SmtpMail.SmtpServer = "192.168.11.11"
  81.         System.Threading.Thread.Sleep(200)
  82.         SmtpMail.Send(AutoEmail)
  83.     End Sub
  84.  
  85.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  86.         Dim a As New DataSet
  87.         Dim g As New DataSet
  88.         Dim h As New DataSet
  89.         Dim b As New OleDb.OleDbDataAdapter
  90.         Dim d As New OleDb.OleDbDataAdapter
  91.         Dim f As New OleDb.OleDbDataAdapter
  92.         Dim c As New OleDb.OleDbConnection
  93.         c.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\phil\wa.mdb;Persist Security Info=true"
  94.         c.Open()
  95.         b.SelectCommand = New OleDb.OleDbCommand("SELECT Fname + ' ' + [Emailadd] as expr1 FROM Table1 ;", c)
  96.         d.SelectCommand = New OleDb.OleDbCommand("SELECT Fname + ' ' + EmailS as expr2 FROM Table2 ;", c)
  97.         f.SelectCommand = New OleDb.OleDbCommand("SELECT Product FROM Table3 ;", c)
  98.         b.Fill(a)
  99.         d.Fill(g)
  100.         f.Fill(h)
  101.         ListBox1.DataSource = a
  102.         ListBox1.DataTextField = "expr1"
  103.         ListBox1.DataValueField = "expr1"
  104.         ListBox1.DataBind()
  105.         ListBox2.DataSource = g
  106.         ListBox2.DataTextField = "expr2"
  107.         ListBox2.DataValueField = "expr2"
  108.         ListBox2.DataBind()
  109.         ListBox3.DataSource = h
  110.         ListBox3.DataTextField = "Product"
  111.         ListBox3.DataValueField = "Product"
  112.         ListBox3.DataBind()
  113.         c.Close()
  114.     End Sub
  115.  
  116. End Class