Solved

Email Composer

Posted on 2008-10-28
13
396 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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