Link to home
Start Free TrialLog in
Avatar of introlux
introluxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Email Composer

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#
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

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
https://www.experts-exchange.com/questions/23840055/Good-example-of-email-helper-class-to-send-email.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

Avatar of introlux

ASKER

I do not understand what this is for:

myCommand.CommandType = CommandType.Text;

As I do not have a  text box called CommandType.Text;
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
ASKER CERTIFIED SOLUTION
Avatar of jinn_hnnl
jinn_hnnl
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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() + "; ");
      }
     

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


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("; ");
 
Just used this instead:

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

Does the job :-)
Thanks! Top Answer!
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
Glad to help ^^
JINN
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