Solved

E-mail SQL

Posted on 2011-09-07
12
235 Views
Last Modified: 2012-05-12
I am trying to send a mass e-mail to only selected people using a checkbox list linked to a SQL database. Is there a way to add the Db filed into the mail.cc field?
0
Comment
Question by:pai_01
  • 6
  • 5
12 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 36495797
I do this in my app using the following logic:

1. Use the database user ID field as the Value string in the checkbox
2. On page submission, iterate through the checkboxes and read in each ID into a CSV string,
e.g. "3,6,8,45,75,757,453,7745,234,565,"
3. Pass that back to SQL and select based on those user IDs, and return their name and email.  Return to ASP.NET as a datatable
4. Create your email in ASP.NET, then iterate the datatable, adding each person as a CC
0
 

Author Comment

by:pai_01
ID: 36496142
do you think you can write me out a sample code, you have me a little confused right now.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 36496171
Okay how far have you got?  How currently are you displaying the checkboxes, and what data have you currently got access to on the page?  Are you displaying using a gridview, repeater, checkboxlist etc?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:pai_01
ID: 36496191
right now i am using a checkbox list to display the names, i have all the code wrote for the e-mail to send out. when a user clicks a checkbox it auto populates a label
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 36496231
Do you have the email addresses as part of the CheckBoxList, or are they still in the database.  Is the UserID for each person stored in the CheckBoxList?
0
 

Author Comment

by:pai_01
ID: 36496259
only the name shows in the cblist. the e-mail is in the database in a separate field.
0
 
LVL 19

Expert Comment

by:Manoj Patil
ID: 36496312
Hello pai_01
I had given below my code for mail send to multiple users. Use the following code with little bit changes and your problem is solved....!!!


protected void btnSend_Click(object sender, EventArgs e)
 {
         try
        {
            String connstring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            SqlConnection con = new SqlConnection(connstring.ToString());
            con.Open();             
            SqlCommand cmd;
            String query = "SELECT Count(Cust_EmailID) FROM Customer_Master WHERE (Cust_IsActive = 1) AND (Cust_Subscribed = 1)";
            cmd = new SqlCommand(query, con);
            Object o = cmd.ExecuteScalar();
            int cnt = int.Parse(o.ToString());
            
            String[] ids = new String[cnt];
            int i=0;
            int emailsent=0;
            
            query = "SELECT Cust_EmailID FROM Customer_Master WHERE (Cust_IsActive = 1) AND (Cust_Subscribed = 1)";
            cmd = new SqlCommand(query,con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ids[i] = dr[0].ToString();
            }
            dr.Close();
            con.Close();
            con.Dispose();
            String msgB=this.Request.Form.Get("elm1").ToString();
             

            for (i = 0; i < cnt; i++)
            {
                EmailSend em = new EmailSend(ids[i].ToString(), msgB);

                emailsent++;
            }
            emailmsg.Text="Mail Send to "+emailsent+"Peoples";
        }
        catch (Exception ex)
        {
            lblDisplay.Visible = true;
            lblDisplay.Text = ex.Message.ToString();          
        }
    }

Open in new window


In above code,
With first query I am trying to get the count of users which are active ( Here you write your code to get the selected users with CheckBox ) In next query I am getting Email Addresses which will be stored in an Array of count which got in privious query.
     After that in for loop, I am writing mail send code which will send mail one by one to user till count limit.  

 
0
 

Author Comment

by:pai_01
ID: 36496419
Sorry to be a pain but can you re write that for vb.net instead and C#?
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 36496473
Is this part of a previous question?
0
 

Author Comment

by:pai_01
ID: 36496539
yes
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 36497009
As you've not had a response yet, here's how to collect the ID's.

Dim sb as New StringBuilder
For Each li as ListItem in MyCheckListID.Items
   If li.Selected Then
       sb.Append(li.Value & ",")
   End If
Next

Open in new window


That will then give a list of ID values for the selected fields.  So you'll get 1,2,4,5,6,784,467,43, etc

Then in SQL you can use a trick like this to get the string into a table variable:

DECLARE @separator CHAR(1)
SET @separator = ","
DECLARE @tbl_Ids TABLE (ID INT)
DECLARE @separator_position INT
DECLARE @array_value nvarchar(10)
WHILE PATINDEX('%' + @separator + '%' , @Ids) <> 0
	BEGIN -- feed array of integers into tempTable
		SELECT @separator_position =  PATINDEX('%' + @separator + '%' , @Ids)
		SELECT @array_value = LEFT(@Ids, @separator_position - 1)
		INSERT INTO @tbl_Ids (IdVal) VALUES (@array_value)
		SELECT @Ids = STUFF(@Ids, 1, @separator_position, '')
	END
SELECT * FROM tbl_Ids

Open in new window

0
 
LVL 25

Accepted Solution

by:
Rouchie earned 500 total points
ID: 36497020
There are TONS of other approaches for converting comma separated strings to table values in SQL, just do a Google search before somebody says there's a better method...!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Find out what you should include to make the best professional email signature for your organization.
In this video we show how to create a User Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Recipients >> Mailb…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question