Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending Emails using checkboxes

Status
Not open for further replies.

kennyaidan

Programmer
Apr 9, 2003
54
IE
Hi, I'm a beginner asp.net developer and i am trying to learn how to send emails. I can get the emails to send when i hard code the "objMail.From" and "objMail.To" email addresses in. However what i was trying to do was to have a list of email addresses in my database and on the asp.net email form when i click on a check box the email is sent to those people within the database.
For example here's my database table

usertype | email

normal blah@blah.com
normal hello@blah.com
advanced advanced@blah.com
advanced bye@blah.com


And my check boxes set out like this
Checkbox 1 - normal
Checkbox 2 - advanced

So when i click checkbox 1 I want to send mails to "blah@blah.com" and "hello@blah.com".

Here's my code

'Database connection code
Dim cn As SqlConnection
Dim cmd As String
Dim ecmd As SqlCommand
Dim dr As SqlDataReader
Dim aidan As String

cn = New SqlConnection("pwd=*****;UID=****;server=****")
cn.Open()

dim i
'for loop for all selected items in checkbox list "check1"
for i=0 to check1.Items.Count-1

'if checkbox i has been selected
if check1.Items(i).Selected then

'assign checked the value from the checkbox
Dim checked as String = check1.Items(i).Text

'select all email addresses from table users
cmd = "select email from users where usertype = '" & checked & "'"

ecmd=new SqlCommand(cmd,cn)
ecmd.ExecuteNonQuery()
dr = ecmd.ExecuteReader()

'While there are records in the database send emails
while (dr.Read())

aidan=dr.GetString(0)
Dim objMail As New MailMessage()
objMail.From = "aidan@blah.com"

'Send to all users with type "aidan"
objMail.To = aidan

objMail.Subject = "Message
objMail.Body = "hello "
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = "****.****"
SmtpMail.Send(objMail)
End While
dr.close()
End If
next
cn.close()

Can anyone help me with where i'm going wrong. When i hardcode in the destination email addresses it works fine but it doesn't move into the while (dr.Read()) loop when i use the check boxes as the destination. Any help greatly appreciated.
Thanks
Aidan
 
It sounds like the variable checked isn't being loaded with anything. As well the ecmd.ExecuteNonQuery() doesn't need to be there. By including this line your simply running the query twice. You only need to run ExecuteReader.

Try stepping through your code. Put a breakpoint on this line ecmd=new SqlCommand(cmd,cn) and check the value of cmd. Just to double check things run the value of cmd through sql to see if anything does pop up.

If you do get results then check some of the properties of your dr object to make sure that it got the data correctly.

If none of this works post again and we'll see what we can do.



That'l do donkey, that'l do
[bravo] Mark
If you are unsure of forum etiquette check here faq796-2540
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top