Wednesday, October 31, 2007

Switch Case C#

For some reason I find myself having to look this up every time I go to use it.

Switch is like Select in VB. The structure of it is as follows.

switch (variable)
{
case value1:
statements;
break;
case value2:
statements;
break;
case value3:
statements;
break;
default:
statements;
break;
}
  • Switch is followed by a variable in parenthesis. This the variable that you are checking the value of.
  • Each value that you want to check the variable against is in a case vale block.
    • Must have a colon after the value.
    • Each statement int he block must be terminated with a semicolon.
    • Each block must have a break at the end of it. C# does not allow falling through to one case to the other, but you can do several empty cases in a row that all fall through to one code block. The break tells it not to do this. For example:
      case 1:
      case 2:
      case 3:
      DoSomething();
      break;
      In this example if the variable is 1, 2, or 3 it runs do something.
  • There can be a default case that executes if the variable does not match any of the cases.
  • It appears as though one can include a goto statement in their case block also. For example "goto case 17" jumps you to case one. You could execute code in case 45 and then jump to case 17 and run its code as well, even though the variable is not equal to 17.
  • All of the case blocks are surrounded by {}. The opening { is after switch (variable). The closing } is after the last case block of code.

Tuesday, September 25, 2007

Debugging Classic ASP

This has been a pain to me in Visual Studio 2005. I finally Googled it and found that VS2005 has been crippled in many ways when it comes to debugging classic ASP pages. One recommendation I saw was to use VS2003, which had more or easier debugging for classic ASP. I can't say if this is true or not since I have never used VS2003 to debug classic ASP pages.

One thing I found that some say can make it easier in VS2005 is listed below:
  • IIS Manager -> Rt Click Web Sites -> Properties -> Home Directory -> Configuration -> Debugging -> Enable ASP Server-Side script
Another thing I found was that some talked of attaching to the IIS ASP process (w3wp.exe). I was not able to get this to work, but I only tried for a couple of minutes.

Wednesday, July 11, 2007

Cannot implicitly convert type 'string' to 'method group'

I run into this now and again in C#: Cannot implicitly convert type 'string' to 'method group'

This usually means that you have not included the parenthesis at the end of a method call. For example, ToString(). Many times I will do:

if (str == var.ToString)

or

str = var.ToString;

It throws the about error because I needed to do:
if (str == var.ToString())

or

str = var.ToString();

Tuesday, June 12, 2007

Using Autoincrement field Value After Insert

I had the need to insert one record into a SQL Server table that held a unique code and a description of what that code represents. That unique code was automatically generated by the database. Right after that insert I needed to insert several records in another table that used that newly inserted value from the previous table. I found two approaches to do this which I will share below.

Approach 1 - Use @@Identity
DECLARE @AutoIncCode int

INSERT INTO LookupTable (Description)
Values('DescriptionOfItem')

//The @@identity variable holds the last auto incremented value. Store this value or you will lose it after the next insert you do.
SET @@AutoIncCode = @@identity

INSERT INTO DependantTable
(Field1, Field2, LookupTableId)
Values('Val1', 'Val2', @AutoIncCode)

Approach 2 - Fetch from the table you just inserted to
DECLARE @AutoIncCode int

INSERT INTO LookupTable (Description)
Values('DescriptionOfItem')

SET @AutoIncCode = (SELECT LookupTableId
from LookupTable where Description = 'DescriptionOfItem')

INSERT INTO DependantTable
(Field1, Field2, LookupTableId)
Values('Val1', 'Val2', @AutoIncCode)

Wednesday, May 9, 2007

Changing ReportServerUrl and ReportPath

In SQL Reporting Services when using the web ReportViewer control I had the problem of my reporting not showing when changing ReportServerUrl and ReportPath. It turned out that I hadn't set ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote.

OK, so that part doesn't have anything to do with changing the properties, but after that I found that after I fixed that problem I could see data for the report, but it would not change the report display when changing these properties. It turned out that I needed to call the Refresh method. I had been looking for something similar to that, but was looking on the ReportViewer control itself. It turns out it is on the ReportView.ServerReport object.

Friday, April 6, 2007

Client Side Event Handlers Appear to Short Circuit RadioButton Server Side Handled

ASP.Net 2.0 RadioButton

I had a handler for the body tag's OnLoad event and that did not seem to conflict with the Page_Load server event. In that event I did something like this:

document.getElementById("rbDocView").onclick=function(){ RadioClick();}
document.getElementById("rbItemView").onclick=function(){ RadioClick();}
RadioClick();

Once I added this the server side CheckChanged event stopped firing.

Changing Value of RadioButton Does Not Change Form

ASP.Net 2.0 Server Side RadioButton
I had two RadioButtons in the same group on a form. I created a server side handler for the CheckChanged event. If a certain criteria was true then I needed to show an error and set the checked property back to its previous value. The problem I ran into was that it wasn't updating the form.

As I looked at the source of the page I found that both radiobuttons had their checked attribute set to "checked". It appears that it displays as checked whichever is the last radiobuton with its attribute set. The remedy was to manually set both radiobuttons' checked property on the server and not assume that setting the checked property to true of one that is in the same group that the other would automatically get set to false.