March 11, 2015 at 9:24 am
So I am being forced to code in C#.net today because I need to add something to an existing solution. Unfortunately, my code is not working. What I'd like to do is troubleshoot the way I do in VB.
In VB, I add MsgBox to my code to popup with the values of variables in various parts of my code while debugging. This lets me know if the code is reaching certain places as well as telling me if the code is coming up with the data I expect or not. Unfortunately, C# doesn't seem to have similar functionality. I tried using MessageBox.Show, but VS 2012 underlines it (doesn't auto complete) and tells me I can't use it as a statement.
Does anyone have any suggestions on how I can accomplish this objective in C#.net?
March 11, 2015 at 9:28 am
So something like
MessageBox.Show("Here is the message");
is highlighted as an error? It should work. Could there be an error on the preceding line?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
March 11, 2015 at 9:34 am
There is no indication of an error on the previous line.
I've tried to put it into a void method that calls a SQL proc.
void findstatus ()
{
int returnjobstat = 5;
foreach (string name in JobNames)
{
SqlConnection sqlC = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "EXECUTE dbo.spJobStatusCheck '" + name + "';";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlC;
sqlC.Open();
returnjobstat = (int)cmd.ExecuteScalar();
string imagename = string.Empty;
if (returnjobstat == 0 )
imagename = "images/small_red.jpg";
else if (returnjobstat == 1 )
imagename = "images/small_green.jpg";
else if (returnjobstat == 4)
imagename = "images/small_yellow.jpg";
else if (returnjobstat == 5)
imagename = "images/small_gray.jpg";
sqlC.Close();
switch (name) {
case "Job1": this.DTDQC.Src = Page.ResolveClientUrl(imagename);
break;
case "Job2": this.XMEDPTX.Src = Page.ResolveClientUrl(imagename);
break;
MessageBox.Show("hello, how are you");
}
}
EDIT: Apparently the forums don't have a code box for C#.
March 11, 2015 at 9:41 am
Try adding a couple more statements:
switch (name) {
case "Job1": this.DTDQC.Src = Page.ResolveClientUrl(imagename);
break;
case "Job2": this.XMEDPTX.Src = Page.ResolveClientUrl(imagename);
break;
default:
MessageBox.Show("hello, how are you");
break;
...
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
March 11, 2015 at 9:46 am
Phil Parkin (3/11/2015)
Try adding a couple more statements:
switch (name) {
case "Job1": this.DTDQC.Src = Page.ResolveClientUrl(imagename);
break;
case "Job2": this.XMEDPTX.Src = Page.ResolveClientUrl(imagename);
break;
default:
MessageBox.Show("hello, how are you");
break;
...
Nope. "The name 'MessageBox' does not exist in the current context"
March 11, 2015 at 9:48 am
I've even tried putting it outside the switch and outside the foreach, and I'm still getting the error.
This is frustrating.
March 11, 2015 at 9:50 am
What sort of C# app is this? Windows forms, console, web etc?
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
March 11, 2015 at 9:52 am
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?
Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
March 11, 2015 at 9:56 am
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Which, in turn, is also a good question – I'm not sure.
If you check your References, is System.Windows.Forms included? This is where MessageBox lives.
You could also try the longhand version:
System.Windows.Forms.MessageBox.Show("Dodgy message");
The absence of evidence is not evidence of absence.
Martin Rees
You can lead a horse to water, but a pencil must be lead.
Stan Laurel
March 11, 2015 at 10:10 am
Phil Parkin (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Which, in turn, is also a good question – I'm not sure.
It's using the ASP.Net debugger, if that helps. I think it might be an aspx project, if there is such a thing. I'm coding in aspx.cs pages and this is going to be a webpage when I'm done with it.
If you check your References, is System.Windows.Forms included? This is where MessageBox lives.
You could also try the longhand version:
System.Windows.Forms.MessageBox.Show("Dodgy message");
No, the Systems.Windows.Forms is not included and the long version also gives me the error "The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
March 11, 2015 at 10:14 am
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Well...does it run in a web browser? If so, it is likely a web app. Does it run in a DOS box? If so, it is a console app.
What do you see for the Output Type under Project -> Properties?
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 11, 2015 at 10:17 am
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Which, in turn, is also a good question – I'm not sure.
It's using the ASP.Net debugger, if that helps. I think it might be an aspx project, if there is such a thing. I'm coding in aspx.cs pages and this is going to be a webpage when I'm done with it.
If you check your References, is System.Windows.Forms included? This is where MessageBox lives.
You could also try the longhand version:
System.Windows.Forms.MessageBox.Show("Dodgy message");
No, the Systems.Windows.Forms is not included and the long version also gives me the error "The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
Aha!!! I suspected as much. You can't pop a message box in a web application. A message box is part of the windows operating system. You can do this a couple of ways.
You can use Response.Write to output some info to the browser. This is not my preferred methodology as it harkens back to classic asp when modern type of debugging wasn't available.
You could also use a ClientStartupScript to add a javascript alert to your page but again this is extremely rudimentary debugging.
I would just add a breakpoint (F9) and run your site in debug mode.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 11, 2015 at 10:22 am
Sean Lange (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Well...does it run in a web browser? If so, it is likely a web app. Does it run in a DOS box? If so, it is a console app.
What do you see for the Output Type under Project -> Properties?
Output type: Class Library
It does run in a web browser.
March 11, 2015 at 10:25 am
Sean Lange (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Which, in turn, is also a good question – I'm not sure.
It's using the ASP.Net debugger, if that helps. I think it might be an aspx project, if there is such a thing. I'm coding in aspx.cs pages and this is going to be a webpage when I'm done with it.
If you check your References, is System.Windows.Forms included? This is where MessageBox lives.
You could also try the longhand version:
System.Windows.Forms.MessageBox.Show("Dodgy message");
No, the Systems.Windows.Forms is not included and the long version also gives me the error "The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
Aha!!! I suspected as much. You can't pop a message box in a web application. A message box is part of the windows operating system. You can do this a couple of ways.
You can use Response.Write to output some info to the browser. This is not my preferred methodology as it harkens back to classic asp when modern type of debugging wasn't available.
You could also use a ClientStartupScript to add a javascript alert to your page but again this is extremely rudimentary debugging.
I would just add a breakpoint (F9) and run your site in debug mode.
WELL!
I suppose that would explain the error. @=)
Thank you for the info. I will try both options 1 and 3 (I don't know javascript yet) to see what isn't functioning the way I expect it to.
March 11, 2015 at 10:29 am
Brandie Tarvin (3/11/2015)
Sean Lange (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
Brandie Tarvin (3/11/2015)
Phil Parkin (3/11/2015)
What sort of C# app is this? Windows forms, console, web etc?Ummm... That's a good question. Where would I look to find that information? (Someone else created it).
Which, in turn, is also a good question – I'm not sure.
It's using the ASP.Net debugger, if that helps. I think it might be an aspx project, if there is such a thing. I'm coding in aspx.cs pages and this is going to be a webpage when I'm done with it.
If you check your References, is System.Windows.Forms included? This is where MessageBox lives.
You could also try the longhand version:
System.Windows.Forms.MessageBox.Show("Dodgy message");
No, the Systems.Windows.Forms is not included and the long version also gives me the error "The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
Aha!!! I suspected as much. You can't pop a message box in a web application. A message box is part of the windows operating system. You can do this a couple of ways.
You can use Response.Write to output some info to the browser. This is not my preferred methodology as it harkens back to classic asp when modern type of debugging wasn't available.
You could also use a ClientStartupScript to add a javascript alert to your page but again this is extremely rudimentary debugging.
I would just add a breakpoint (F9) and run your site in debug mode.
WELL!
I suppose that would explain the error. @=)
Thank you for the info. I will try both options 1 and 3 (I don't know javascript yet) to see what isn't functioning the way I expect it to.
😀
The worst part of using Response.write to debug is that you have to compile your code, reload the page and then find the text on the screen somewhere. It is just awful to deal with. If you use the debug methodology you just set your break point and your entire site can run. It will stop when it encounters a break point. Then you can evaluate variables, run statements in the immediate window etc. Probably much like what you remember from VB. Web development is very different from windows applications because everything is stateless and there is a very clear demarcation between application code and client code.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 15 posts - 1 through 15 (of 34 total)
You must be logged in to reply to this topic. Login to reply