john
john

Reputation: 41

email containing with SQL

I have a button called "lnkupdate_click" basically all it does when you click is access a table called "tbl_security" and from there it will update the field "pass" with a random 4 digit number where the field "patrol" = 1 or true.

an example is shown below;

Code    Pass   Patrol
TES     1234        1
ASD     4321        1
MOR     6789        1
SAI     0959        1 

The bit I am stuck on is this: All I want it to do from here is send the updated info from the database in an email. So basically to generate me an email containing all of the information in the table above. However I keep getting a red line underneath my "foreach" loop with this error message.

"foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'"

My code is below:

protected void lnkUpdate_Click(object sender, EventArgs e)
   {


        {
            string queryUpdateAllFields;
            string queryGetAllUpdatedField;
            StringBuilder sb = new StringBuilder();

            queryGetAllUpdatedField = @"update tbl_Security set
                                      Pass = round(rand(CAST(CAST(NEWID() AS VARBINARY(4)) AS SMALLINT))* 9000,0) + 1000
                                      WHERE Patrol = 1";

            queryGetAllUpdatedField = @"SELECT Code, Pass, Patrol 
                                        FROM tbl_Security
                                        WHERE Patrol = 1";

            var random = new Random();

            //queries to update and retrieve
            SqlHelper.ExecuteSqlNonQuery(queryUpdateAllFields);

            var results = SqlHelper.ExecuteSqlNonQuery(queryGetAllUpdatedField);


            //loop over the results and append to StringBuilder instance.
            sb.Append("Below are all the fields that have been updated: <br /><br />");

            foreach(var r in results)
            {
                sb.AppendLine("<hr />");
                sb.AppendLine("Code: " + r.Code + " Pass: " + r.Pass + " Patrol: " + r.Patrol); //for Patrol
            }   



       //Email showing all updated pass's with codes
            MailAddress to = new MailAddress("");
            MailAddress from = new MailAddress("");
            MailMessage message = new MailMessage(from, to);
            message.Subject = "Subject Line Here";
            message.Body = sb.ToString(); //attach StringBuilder - This will be built up of all the rows updated in the DataBase

         SmtpClient mailClient = new SmtpClient();
            mailClient.Port = 25;

            //finally send message here
            mailClient.Send(msg);

      }
  }

Upvotes: 1

Views: 133

Answers (2)

Developer
Developer

Reputation: 3057

try: change the loop like so: for(int i= 0; i < results; i++)

Upvotes: 0

Barry Kaye
Barry Kaye

Reputation: 7759

ExecuteSqlNonQuery returns an int not a collection which I think you are assuming.

Upvotes: 9

Related Questions