1

I'm trying in c# to simply pull a set of rows from a SQL DB and run through each row I get a result with some code that will email me one time for each row returned.

the below code should work as far as I can tell and returns no errors, I'm stumped. You can assume that the "ConnectionString" is valid and used in plenty of places on the site, and that the query as defined there should definitely return back a row from my DB, as I've verified the exact same line by running it through the raw DB with sql mgmt studio express and I get the results I'm expecting.

all I'm getting though is the single "i got here" email that happens just before I try to enter the loop.

Would really appreciate any insight from smarter people, thanks guys!

string query4 = "SELECT TOP(1)* FROM subscribers WHERE subscriber = 'test'";

string number; string pix; string watcheremail; string watcher;

MailMessage message25 = new MailMessage();
message25.From = new MailAddress("[email protected]");
message25.To.Add(new MailAddress("myemailaddress"));
message25.Body = "i got here";
SmtpClient client25 = new SmtpClient();
client25.Send(message25);
using (SqlConnection connection = new SqlConnection("ConnectionString"))
{
    connection.Open();
    using (SqlDataAdapter people = new SqlDataAdapter(query4, connection))
    {
        DataTable people1 = new DataTable();
        people.Fill(people1);

        foreach (DataRow row in people1.Rows)
        {
            watcher = row["subscriber"].ToString();
            MailMessage message252 = new MailMessage();
            message252.From = new MailAddress("[email protected]");
            message252.To.Add(new MailAddress("myemailaddress"));
            message252.Body = "AND THEN I GOT HERE TOO - " + watcher;
            SmtpClient client252 = new SmtpClient();
            client252.Send(message252);
        }
    }
}
5
  • Have you tried stepping through the code in a debugger? Additionally - are any exceptions being caught / logged anywhere? Commented Aug 19, 2010 at 23:06
  • debug? Are you stepping through the loop to see what happens? Commented Aug 19, 2010 at 23:06
  • i guess i should mention i'm a novice, very new to all this and teaching myself. i don't know how to run through a debugger or anything like that. the email in the loop is my method of 'settping through the loop' and that email never reaches me, meaning i'm not entering the loop successfully at all Commented Aug 20, 2010 at 0:07
  • please don't put tags like "C#" in the title of your questions. Just use the tags to categorize your questions. Commented Aug 20, 2010 at 0:16
  • 1
    I am not trying to be mean or condescending, so please don’t take this the wrong way, but I think you might benefit from reading an introduction to programming, which I hope will include a description of how to use a debugger. If you can’t find a good website, then you might consider buying a book. Once you know how to use a debugger, you will be able to see what actually goes on when your code runs — we can’t, because we cannot run your code and we can only speculate about your environment. I hope you will take this as constructive criticism. Good luck! Commented Aug 20, 2010 at 0:41

2 Answers 2

2

Here are a few hints on how to debug your code. Please don’t take this as a complete guide — there is a lot that you can learn about debugging by reading a proper introduction. But this might get you a bit further in your particular problem that you have right now.

  • Move your cursor to a place in the code before the place where you think there might be a problem. (In your example, go to the first line. Start from the top.)
  • Press F9. The line will turn red. This is called a “breakpoint”: it tells Visual Studio that you want to stop here when the program reaches this point.
  • Press F5. This runs your program. Do whatever necessary in your program to get the program to the point where you have the red line (the breakpoint).
  • You will notice that Visual Studio suddenly comes to the top. The line that was red, is now yellow. This is because it is the line that is currently executing. The program has stopped right here, and you can now examine how it runs from this point onwards.
  • Press F10. You will notice that the yellow line will go down one command. Press F10 multiple times and it will go through the code line by line.
  • At each point, you can hover your mouse over variables. You will notice that it shows you what the value in each variable is.
  • When you think you’ve found the bug and you need to edit the code, press Shift+F5. This will kill the program. You can then edit your code and press F5 again to run the edited version.
  • If you don’t need a breakpoint anymore, just press F9 again on the same line, and it will go away.

Also have a look at the “Debug” menu and its “Windows” submenu, while you are stepping through the program (i.e. when there is a yellow line somewhere). If you like to experiment and play, try these out, especially “Autos”, “Locals” and “Call Stack”. Have fun!

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

Comments

0

Here are a few suggestions that might help...

  1. Are you running this in a Console App? If so, replace the email sending with Console.WriteLine("Some Text"), and that can help you see where the code is getting without using a debugger.

  2. You have select top 1 * from Subscribers... Are you sure the query is returning results?

2 Comments

1. no i'm running this as an ASPX on a webpage 2. yes i'm sure it's returning results
Since your on a web app, you could replace my recommendation of using "Console.WriteLine" with "Response.Write" to output debug info. It is a poor man's way of debugging. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.