views:

112

answers:

2

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);
        }
    }
}
A: 

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!

Timwi
A: 

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?

Matthew
1. no i'm running this as an ASPX on a webpage2. yes i'm sure it's returning results
korben
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. :)
Matthew