1

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); } } 
2
  • 1
    This is my main gripe about this var business. Though it's useful for anonymous types, it makes code like this very unclear about what's what. Commented Dec 20, 2012 at 17:02
  • Please don't edit your posts to be gibberish in the future. If you'd like to be disassociated from a question, flag for moderator attention, select 'other' and let us know. Commented Dec 21, 2012 at 12:02

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.