?
Solved

Email Composer

Posted on 2008-10-28
13
Medium Priority
?
411 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 2000 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

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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