Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a website where a queue is created in a gridview. This queue is for setting up access to applications that someone will need to have access to. Depending on what applications they need emails might need to be sent out to other areas to process that access. If not there is not an email address in the table if so there is an email address in the table. So what I would like is on the page load if an email needs to be sent out one gets sent out for each application and then a fieldd is changed to true after the email is sent so that it does not get sent again. I am new to programming and I am not sure how to go about doing this. I know I would need to loop through the records somehow but not sure how to go about doing that. I do know the code to send emails but not the rest. Can someone please help me. Thank you.

Here's a more specific question then as an add-on. I know how to pull the information from the database. What I need to know is how to loop through the returned data. Say there are 10 items that get pulled from the database. 3 out of those 10 need to have an email sent out. Emails need to be sent only if there is an email address attached to that record. So if the email address it not null and an email has not been sent already, send an email and then update a bit field to true saying that the email has been sent. I am having a great amount of trouble figuring out how to do that. Any advise/code would be helpful. Thank you.

Here's the code I do have can someone pleae help with the rest, i figured I have to do an array but I have not worked with them before so this is probably all wrong. I am getting that email address cannot be null but I thought I was excluding null values in my if statement. Please let me know what I can do to correct my issue:

C++
protected void Page_Load(object sender, EventArgs e)
   {
       string request = Request.QueryString["request"];
       string requestLink = "<html>http://Default.aspx?request=" + request + "</html>";
       ArrayList emailAddressList = new ArrayList();

       string sqlString = "Select Application.ownerEmail, dbo.RequestItems.applicationProfile, dbo.RequestItems.emailToSystemAdmin from Application join dbo.ApplicationProfile on dbo.ApplicationProfile.application = dbo.Application.aAuid join dbo.RequestItems on dbo.RequestItems.applicationProfile = dbo.ApplicationProfile.apAuid where request = " + request;
       string connString = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
       SqlConnection sqlConn = new SqlConnection(connString);
       SqlCommand sqlComm = new SqlCommand(sqlString, sqlConn);

       sqlConn.Open();
       SqlDataReader reader = sqlComm.ExecuteReader();

       while (reader.Read())
       {
           emailAddressList.Add(reader["ownerEmail"].ToString());
           string applicationProfile = reader["applicationProfile"].ToString();
           bool emailToSystemAdmin = reader.GetBoolean(reader.GetOrdinal("emailToSystemAdmin"));

           //then loop if email is not null and emailToSystemAdmin is not true
           foreach (string emailAddress in emailAddressList)
           {
               if (emailAddress != null && emailToSystemAdmin != true)
               {
                   //thencode to send the email
                   SendEmail(emailAddress, "The followiing link is provided to you to set up User Access for a new employee.  Once IS sets up the network ID you can set-up access for this employee.  Check back if network ID is not set-up yet.</br>" + requestLink);

                   //then update database
                   string sqlString2 = "UPDATE RequestItems SET emailToSystemAdmin=1 where request= " + request + "and applicationProfile = " + applicationProfile;
                   SqlCommand sqlComm2 = new SqlCommand(sqlString2, sqlConn);

                   sqlComm2.ExecuteNonQuery();
               }

               //then after the loop and the update
               reader.Close();
               sqlConn.Close();
           }
       }
   }
Posted
Updated 26-Oct-10 2:23am
v6
Comments
Kschuler 21-Oct-10 14:04pm    
Your question is quite vague. Break down your task into smaller parts. Start working on the smaller parts and when you get stuck come back and post a more specific question including the code you have so far. For example, you stated that on page load if an email needs to be sent, then send it. We can't help you with that because we don't know the requirements your project has for deciding if an email needs to be sent. So what specifically are you having trouble with? Pulling information from a database? If so, what kind of database?
shakil0304003 22-Oct-10 0:52am    
Your requirement is unclear, clear it?

Use google, there are THOUSANDS of available examples of sending email with C#.
 
Share this answer
 
Comments
Jesi523 21-Oct-10 12:57pm    
Yeah, I tried but I could find anything on looping through. As I send I know how to send emails its the rest I am looking for help with. I will keep looking thank you.
I know how to pull the information from the database.

//sample
// Get the email field 
SqlDataReader Sqldr = SqlCmd1.ExecuteReader();
while (Sqldr.Read())
{
//check email is empty or not
string email=//email value
 if(//email not null and bit field is false)
 {
    //send email.
    // update bit field to true
 }
 else
 {
 //some code
 }
}
 
Share this answer
 
I got it accept in my test it should only send 3 emails but it is sending 6 instead:

C++
protected void Page_Load(object sender, EventArgs e)
   {
       string request = Request.QueryString["request"];
       string requestLink = "<html>http://Default.aspx?request=" + request + "</html>";
       ArrayList emailAddressList = new ArrayList();

       string sqlString = "Select Application.ownerEmail, dbo.RequestItems.applicationProfile, dbo.RequestItems.emailToSystemAdmin from Application join dbo.ApplicationProfile on dbo.ApplicationProfile.application = dbo.Application.aAuid join dbo.RequestItems on dbo.RequestItems.applicationProfile = dbo.ApplicationProfile.apAuid where request = " + request;
       string connString = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
       SqlConnection sqlConn = new SqlConnection(connString);
       SqlCommand sqlComm = new SqlCommand(sqlString, sqlConn);

       sqlConn.Open();
       SqlDataReader reader = sqlComm.ExecuteReader();

       while (reader.Read())
       {
           emailAddressList.Add(reader["ownerEmail"].ToString());
           string applicationProfile = reader["applicationProfile"].ToString();
           bool emailToSystemAdmin = reader.GetBoolean(reader.GetOrdinal("emailToSystemAdmin"));

           //then loop if email is not null and emailToSystemAdmin is not true
           foreach (string emailAddress in emailAddressList)
           {
               if (emailAddress != "" && emailToSystemAdmin != true)
               {
                   //thencode to send the email
                   SendEmail(emailAddress, "The followiing link is provided to you to set up User Access for a new employee.  Once IS sets up the network ID you can set-up access for this employee.  Check back if network ID is not set-up yet. </br>" + requestLink);

                   //then update database
                   string sqlString2 = "UPDATE RequestItems SET emailToSystemAdmin=1 where request= " + request + "and applicationProfile = " + applicationProfile;
                   string connString2 = ConfigurationManager.ConnectionStrings["UserAccessConnectionString"].ConnectionString;
                   SqlConnection sqlConn2 = new SqlConnection(connString2);
                   SqlCommand sqlComm2 = new SqlCommand(sqlString2, sqlConn2);
                   sqlConn2.Open();
                   sqlComm2.ExecuteNonQuery();
                   sqlConn2.Close();
               }
           }
       }

       reader.Close();
       sqlConn.Close();
   }

Help anyone? Please
 
Share this answer
 
Comments
Jesi523 28-Oct-10 10:51am    
I figured it out. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900