问题
Converting a Database into a list in VB. I am essentially using 2 lists the 1st list contains several email addresses or the ends of them like EXMPLE "@hotmail.com" the Second list is read from a column from a database that i already linked to the form. I will post the code I was trying to make work but it doesn't seem to be taking to. It is suppose to act as an alert system with the option to send to 1 person or everyone in the Database So Please Help Make this work?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Dim self As New MailAddress("archernolan151@gmail.com")
Dim strCarriers As New List(Of String) '@nd half of carriers email
Dim Scall As New List(Of String) 'List from the linked Column
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("blank@gmail.com", "password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
strCarriers.Add("@pm.sprint.com")
strCarriers.Add("@vtext.com")
strCarriers.Add("@tmomail.net")
strCarriers.Add("@txt.att.net")
rw = ContactsDataSet.Tables(0).NewRow 'Database Columns
rw.Item("Call") = Scall
ContactsDataSet.Tables(0).Rows.Add(rw) 'End of database
If rad1.Checked = True Then 'If the radio button is clicked it will take the data from the Database verses from the text box
For Each item In Scall
For Each Carrier As String In strCarriers
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add("item" + "Carrier")
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next Carrier
Next
ElseIf rad1.Checked = False Then
For Each Carrier As String In strCarriers
e_mail = New MailMessage()
e_mail.From = self
e_mail.To.Add(txtTo.Text + Carrier)
e_mail.Subject = txtSubject.Text
e_mail.IsBodyHtml = False
e_mail.Body = txtMessage.Text
Smtp_Server.Send(e_mail)
Next
End If
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
回答1:
the line in the first for each loop that shows {e_mail.To.Add("item" + "Carrier")} will create as string that equals "itemCarrier" as an email address and you probably want to have it use the variable item and the variable carrier and when they are in quotes like they are it will not create a valid TO address. If you remove the quotes and use the variables directly and access the appropriate properties it might set you on the right path.
Your second for..each is correct in its use of the variables as far as I can tell.
Another point about your code... You are diming Scall as a new list but then you are setting the database variable to the value of Scall and Scall does not seem to be getting a value from anywhere...In order for the outer for-each loop that loops the Scall list that list needs to get a series of values from somewhere.
To load the list from the "Call" column you would run something like this
...code to connect to database
...code to read to a dataset called tmp with a table that contains the column "Call"
For each dbRow as DataRow in tmp.tables(0).rows
Scall.Add(dbRow.Item("Call"))
Next
Now I have not added any kind of error checking and there are more optimized ways of loading a list if you use LINQ to SQL that make the process much much faster but in your example you appear to be using ADO so I used ADO in my answer.
You were pretty generic about the code "not working" and did not provide any real details as WHAT exactly was not working so I am just looking at the code and indicating what I think is not working based on straight up analysis of what you posted.
来源:https://stackoverflow.com/questions/20381964/reading-a-database-to-a-list-and-then-doing-something-with-the-info