Solved

Email Composer

Posted on 2008-10-28
13
389 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
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
Comment Utility
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
Comment Utility
Just used this instead:

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

Does the job :-)
0
 

Author Closing Comment

by:introlux
Comment Utility
Thanks! Top Answer!
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
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
Comment Utility
Glad to help ^^
JINN
0
 
LVL 10

Expert Comment

by:jinn_hnnl
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now