Solved

Email Composer

Posted on 2008-10-28
13
398 Views
Last Modified: 2013-12-17
Hi,

I would like to know if it can be done and how please. I have a user table with the user details etc. I have created an email composer to send emails out.

I would like to know if it is possible to load up all the values in the database for field email and get them all pasted in the TO text box. I would need to have ',' to seperate each email or it will not work.

Any idea how this can be achieved??

Regards,

introlux

Noe: using SQL Server, C#
0
Comment
Question by:introlux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
13 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22820305
Can you already access to the database?

What is exactly your problem? Sending email in dot Net or concatenating list of user email?

For emailing problem
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23840055.html

For setting up the list of user email:
Make sure you can connect to Users Database

Look at my example

JINN



     string cString = "Persist Security Info=False;Integrated Security=SSPI;database=mydatabase;server=mySQLServer"; //your connection string
      SqlConnection myConnection = new SqlConnection(cString);
 
// Create a SqlDataAdapter.
      SqlDataAdapter myAdapter = new SqlDataAdapter();    
      myConnection.Open();
      SqlCommand myCommand = new SqlCommand("SELECT Email FROM Users",
      myConnection);
      myCommand.CommandType = CommandType.Text;   
      myAdapter.SelectCommand = myCommand;
      DataSet ds = new DataSet("Users");
      myAdapter.Fill(ds);
      StringBuilder sb = new StringBuilder();
      foreach datarow in ds.Tables[0].Rows
      {
		sb.Append(datarow["Email"].ToString() + "; ");
      }
      
      tbEmailList.Text = sb.ToString();
      myConnection.Close();

Open in new window

0
 

Author Comment

by:introlux
ID: 22820557
I do not understand what this is for:

myCommand.CommandType = CommandType.Text;

As I do not have a  text box called CommandType.Text;
0
 

Author Comment

by:introlux
ID: 22820605
Also this code:

foreach datarow in ds.Tables[0].Rows

Does not seem right. I dont even mind loading the emails into the txtbox.Text rather than embedding them in the email.

Whatever way is easier!

Regards,

introlux
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22820612
Ahh,

It's the standard Class of DotNet to tell what type of command you are using (Text is not the same property as the textbox one)

You dont have to do it that way You can just have to:

myConnection.Open();
SqlDataAdapter myAdapter = new SqlDataAdapter("Select Email From Users", myConnection);
DataSet ds = new DataSet()
myAdapter.Fill(ds, "Users");

foreach datarow in ds.Tables[0].Rows
      {
                sb.Append(datarow["Email"].ToString() + "; ");
      }
     
      tbEmailList.Text = sb.ToString();
      myConnection.Close();

Remember to use System.text at the beginning to use StringBuilder (best way to work with concatenating string):
using System.Text;

Hope this helps
JINN



0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22820629
Sorry ^^ my bad I dont have to compiler here:

that should be:

foreach(DataRow dr in ds.Tables[0].Rows)
      {
                sb.Append(datarow["Email"].ToString() + "; ");
      }
     

0
 

Author Comment

by:introlux
ID: 22820739
This works perfectly! Only one slight thing. If it cant be sorted, not a problem and will still award the points! The last email displays "," to remove this.

Is this possible???

Regards,

introlux
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22821394
You can sort it of course, You can sort it in your SQL statement: Select Email From User ORDER BY Email DESC

or DataTable: I have recommend to use DataSet for the same reason, you can do anything with DataTable, sorting, modifying data:

Create dataView first (for easier reading)

DataView dtv = ds.Tables[0].DefaultView
dtv.Sort = "Email DESC"
foreach(DataRowView drv in dtv.Rows)

But for your case sorting in SQL is better.

To remove the last ; I dont think you need to remove it, if you really want you can replace it or cut it from the string, or put the if statement in that foreach loop

int index = 1;
foreach(DataRow dr in ds.Tables[0].Rows)
      {
                sb.Append(datarow["Email"].ToString() );
              if(index != dsTables[0].Rows.Count)
                      sb.Append("; ");
             index += 1
      }

Hope this helps


0
 

Author Comment

by:introlux
ID: 22829416
Compiler Error Message: CS0103: The name 'datarow' does not exist in the current context

Source Error:

 

Line 113:        foreach(DataRow dr in ds.Tables[0].Rows)
Line 114:     {
Line 115:               sb.Append(datarow["Email"].ToString() );
Line 116:             if(index != dsTables[0].Rows.Count)
Line 117:                     sb.Append("; ");
 
0
 

Author Comment

by:introlux
ID: 22829468
Just used this instead:

txtTo.Text = txtTo.Text.Substring(0, txtTo.Text.Length - 2);

Does the job :-)
0
 

Author Closing Comment

by:introlux
ID: 31510659
Thanks! Top Answer!
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22829477
Hi,

Sorry, syntax error

int index = 1;
foreach(DataRow dr in ds.Tables[0].Rows)
      {
                sb.Append(dr["Email"].ToString() );
              if(index != dsTables[0].Rows.Count)
                      sb.Append("; ");

             index += 1
      }

Just be aware, this part is only for removing the last ; (Nothing to do with the solution of sorting using DefaultView I mentioned above).

JINN
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22829488
Glad to help ^^
JINN
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22829515
txtTo.Text = txtTo.Text.Substring(0, txtTo.Text.Length - 2);

precisely ^^. that's much better. I forgot the syntax and was too lazy to google for it :D

Good job

JINN
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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