?
Solved

E-mail SQL

Posted on 2011-09-07
12
Medium Priority
?
243 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

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 2000 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

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

Question has a verified solution.

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

What is the biggest problem in managing an exchange environment today? It is the lack of backups, disaster recovery (DR) plan, testing of the DR plan or believing that it won’t happen to us.
Organisation is organized in a pattern to flow the day to day business, every application and system is interdepended on each other and when very important “Exchange Server downtime” happened.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

598 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