Monday, November 19, 2007

Modal Browser Window Spawns in New Window When Submitting to Self

I had the problem with an aspx page that when I changed it to a modal window it would pop a second copy of itself into another non modal window when the submit button was clicked.

The solution ended up being to add <base target="_self"> between the <head> and </head> tags. Don't know why I had to do that, but so far it seems to have resolved the problem.

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:
case value2:
case value3:
  • 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:
      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)


str = var.ToString;

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


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)

//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)

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();}

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.

Friday, March 9, 2007

Creating a new XMLNode instance in .Net

The .Net XMLNode can not be instantiated with new since it is an abstract class. To create a new one you have to create one of the more specific types like XMLElement or XMLAttribute. Unfortunately it doesn't look like you can just do New XMLElement either. You must use an XMLDocument and it's CreateElement method.

Here's a situation I had. I had a method that was taking in a value and a node. I wanted to assign that value to the node. Then I had the problem of the node not existing in certain circumstance. I wanted to create a new node and just give it a name and a value. I decided to pass in the parent of the node and then append it to the parent. I couldn't do that because it wouldn't know how to deal with that node without knowing if it is an element or an attribute or whatever. So I needed an element, but couldn't just do a New XMLElement either. I used the XMLDocument, excuted CreateElement and then passed that to the AppendChild method of the parent node. Note that the XMLDocument that you are attaching the Element to much be the same document that was used to create.

private Sub MyMethod(ByVal Value As String, ByVal n As XmlNode, ByVal ParentNode As XmlNode, doc as XMLdocument)

dim ele as XMLElement

If IsNothing(n) Then

ele = doc.CreateElement("MyName")
ele.InnerText= Value

End If

End Sub

Friday, February 23, 2007

Server Side Variables in an Alert in ASP

If you need to see the value of a server side variable in ASP you can insert a script tag an inside the script tag you can put a server side variable of function call. It seems a little odd at first and if you are using the Visual Studio IDE the color coding even makes you think you are doing something wrong, but it seems to work.

Here's the situation. You have loaded something into a server side variable and you want to find out what that value is.

Note the single quotes around the server tags. Without the single quotes, the JavaScript evaluates the contents of the variable as code and not a string. Also note the = in front of the server side variable. This causes it to return the value of the variable.

In the image I attached I failed to insert the attributes for the script tag, so be sure you do that to tell it that it is JavaScript.