a developer's notes – a semi-technical web development BLOG

September 29, 2011

Why can’t I run my .NET 4.0 site on Windows 2003 with IIS 6?

Filed under: ASP.NET — Duy Nguyen @ 10:44 pm
Tags: , , , , , , , , , , , , , , , , , ,

IIS 6 (that comes with Windows 2003) does not let you have more than one framework at a time running in the same instance like IIS7 that can create Application PoolS targeting different frameworks. This just means a single application pool can’t be used for two different frameworks like IIS7.

You have a two options:

  1. Downgrade the .NET application to 3.5 with Visual Studio 2010. Just go to the Website project properties, Application tab and there is a drop down with all the framework releases, select 3.5.

    VS2010 will reload the project and modify the web.config. If you added A web service reference, thEN you may have to delete them and re-add them under 3.5

  2. Configure IIS6 with a second application pool and a second web site.

    • Create a new application pool…

      and new web site…

      Then assign this application pool to the Framework 4 web application that will be hosted by your new site. To assign the application pool, in the IIS console open the properties section of the web application, and click on the “Home directory” tab and select the application pool which you have created earlier.

    • Then you need to change the ASP.NET version of your application. Right click your virtual directory that hosts your application and click the property and check the ASP.Net. Select Framework 4.

    • Select ASP.NET 4.0 on IIS6 under Web Service Extension. Go to the IIS6 console open “web service extension node” which is right below the “default website” node. You will see the entire ASP.Net framework list that is installed. By default these frameworks might be prohibited so please select ASP.Net Framework 4 and click allow button.

      *If you don’t see ASP.NET 4.0, make sure the framework is installed on the server! If it is, then open a command prompt go to this directory: C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319
      Then run this command: aspnet_regiis -i

      You will now see ASP.NET 4.0. If you don’t set ASP.NET 4.0 to ALLOW, You will get a “404 Page is not found.”

Check out this great article:

http://stackoverflow.com/questions/2821514/default-aspx-with-iis-6-0-and-net-4

Advertisements

“No symbols have been loaded for this document” when trying to debug on a ASP.NET MVC .aspx View page

Filed under: General Tips — Duy Nguyen @ 5:50 pm
Tags: , , , , , , , , , , , , , ,

Problem:
I was always able to set a break point on any server side code within an MVC .aspx View page.

Server side code like this:

<a href="url">Link text1</a> 

//(Sever side code is code that is between <% and %>
<%                
	string x = "";
	if (ViewData["status"] != null)
	{
		x = ViewData["status"].ToString();
	}
%>

<a href="url">Link text2</a> 

Then while coding, all of a sudden I lost that ability. When you hover over the breakpoint, it would say “The breakpoint will not currently be hit. No symbols have been loaded for this document.” The strangest part is that I was able to set a break point on the controller of the view!

Resolution:
There is some server side code that is throwing an error. Remove all code and put in a simple statement such as int x = 0;

Then set a break point on that. When I did that, I was able to hit the break point, then use the immediate window to make sure the code I was trying to use wasn’t throwing any errors.

Missing Modules Window in Visual Studio 2010

Filed under: General Tips — Duy Nguyen @ 1:44 pm
Tags: , , , ,

If the Modules Window in Visual Studio 2010 is not under Debug, Windows, Modules, you can get it back by pressing Ctrl-Alt-U.

September 14, 2011

Ambiguous Reference Between Two NameSpaces

Filed under: ASP.NET,C# — Duy Nguyen @ 11:25 pm
Tags: , , , , , , ,

If you get an ambiguous reference error when using two different namespaces that contain the same object, you will need to use a type alias.

For example: Say you want to use the System.Drawing.Image type in your ASP.NET project. So you decide to add the System.Drawing namespace like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

You will get an ambiguous reference if you have that namespace along with the System.Web.UI.WebControls namespace.

Why? Because both namespaces contain an “Image” object.

Resolution: Use a type alias. In your using statement write using Image = System.Drawing.Image;.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Image = System.Drawing.Image;

Now when you hover over your Image type, you will see that you are now referencing the Image from System.Drawing.Image.

Navigating in Visual Studio

Filed under: General Tips — Duy Nguyen @ 9:30 am
Tags: , , , , , , , , , ,

Have you ever jump around from code to code only to find yourself not knowing what class file you were at or what method you were navigating from? I get lost all the time when I am digging around in code.

I used to use bookmarks to place where I want to remember. They are great but what if you forgot to create one? Is there a “Back” button like you have on your windows browser?

In Visual Studio there is! Check out the Navigate Backwards and Navigate Forwards button!

You can simply click backwards to see where your last mouse cursor was. I usually hit F12 on a method or class to see its definition. And sometimes I find myself down a rabbit hold. Just click on the Navigate backwards to get back where you were!

If they are missing, customize the Standard toolbar. The Navigate Forward and Navigate Backwards is on the “View” Category.

September 5, 2011

Two ways to get an Enum with a string

Filed under: C# — Duy Nguyen @ 2:33 pm
Tags: , , ,

Two ways to get an Enum. Consider the following Enum:

 public enum MyEnumType
    {
        Default = 0,
        First,
        Second,
        Third,
    }

How to get an Enum type from a string equivalent to the Enum value?

(MyEnumType)Enum.ToObject(typeof(MyEnumType), Convert.ToInt16('1'));

// Gives you MyEnumType.First

How to get an Enum type from a string equivalent to the Enum name?

(MyEnumType)Enum.Parse(typeof(MyEnumType), 'Second', true); 

//passing in true as the third parameter makes it not case-sensitive
// Gives you MyEnumType.Second

How to get a value from an Enum?

int enumVal = (int)MyEnumType;

How to write and append to a text file (C# .NET)

Filed under: C# — Duy Nguyen @ 2:33 pm
Tags: , , , , ,

How write to a text file and append more text from another function.


namespace TextFileWriter
{
    public partial class _Default : System.Web.UI.Page
    {
        //Create the StreamWriter object to do the writing
        StreamWriter sw;

        protected void Page_Load(object sender, EventArgs e)
        {
            string driveLetter = "c";
            string folder = "DEV_LOGS";
            string filename = "myFile";
            string fileExtension = ".txt";

            string path = SetFilePath(driveLetter, folder);
            string fullFilePathName = path + "\\" + filename + fileExtension;

            sw = CreateTextFile(fullFilePathName);

            sw.WriteLine("My first line of text");

            function1();

            sw.WriteLine("My third line of text");

            //be sure to close the stream and file or else
            //the file will be locked
            sw.Close();
        }

        public void function1()
        {
            sw.WriteLine("Second line of text");
        }

        public StreamWriter CreateTextFile(string fullFilePathName)
        {
            StreamWriter sw;

            // checks if the file exists
            if (!File.Exists(fullFilePathName))
            {
                sw = File.CreateText(fullFilePathName);
            }
            else
            {
                // file exists, append text to file
                sw = File.AppendText(fullFilePathName);
            }

            return sw;
        }

        public string SetFilePath(string driveLetter, string folder)
        {
            // Specify a "currently active folder" 
            string activeDir = driveLetter + @":\";

            //Create a new subfolder under the current active folder 
            string newPath = System.IO.Path.Combine(activeDir, folder);

            // Create the subfolder
            System.IO.Directory.CreateDirectory(newPath);

            return newPath;
        }
    }
}

Output:

My first line of text
Second line of text
My third line of text

You can also append like this:

namespace TextFileWriter
{
    public partial class _Default : System.Web.UI.Page
    {       
        //Create your objects here for global scope
        //Create the file
        FileStream theFile;

        //Create the StreamWriter object to do the writing
        StreamWriter sw;

        int x;

        protected void Page_Load(object sender, EventArgs e)
        {
            x = 0;

            for (int i = 0; i < 5; i++)
            {
                function1();
            }
        }

        public void function1()
        {
            x++;

            theFile = new FileStream(@"C:\DEV_LOGS\test.txt", FileMode.Append);

            sw = new StreamWriter(theFile);
            sw.WriteLine(x.ToString() + " line of text");

            sw.Close();
            theFile.Close();
        }
    }
}

Output:
1 line of text
2 line of text
3 line of text
4 line of text
5 line of text

Check this site out for different ways to open a file.

How to search all columns of all tables in a sql database for a keyword

Filed under: SQL — Duy Nguyen @ 2:26 pm
Tags: , , , , , ,

How to search all columns of all tables in a database for a keyword? Check out this great blog entry: http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

You can also use these queries to find which table a particular column name resides in:

SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name = ‘MYCOLUMNNAME’ )

SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns WHERE name like ‘%PART_OF_NAME%’ )

There Is No Source Code Available For the Current Location

Filed under: ASP.NET — Duy Nguyen @ 2:25 pm
Tags: , , , , , , , , , , , , , ,

(Taken from:http://www.marvinpalmer.com/MarvinPalmer/post/There-Is-No-Source-Code-Available-For-the-Current-Location.aspx

When using trying to debug with Visual Studio and programming using .NET, you get a Visual Studio dialog that says “There is no source code available for the current location.”

1.The “Show Disassembly” button will be available. Click on it. You can step through the disassembly which does eventually take you into the code.
2. If you click “Show Disassembly” and then “Step Into” code (F11) just like you would have if the code window had loaded, you will see code mixed with other stuff.
3. When you see the code to debug, just right-click the window and choose “Go to Source Code” which opens up the code

Debugging into Classic ASP with Visual Studio

Filed under: Classic ASP — Duy Nguyen @ 2:22 pm
Tags: , , , , ,

1. First, navigate to the page your trying to debug into to load the IIS Worker Process.

2. Enable “Script source access” in IIS under the Home Directory tab. This allows VS to access the script files in ASP

3. In the Home Directory tab, click Configuration under application settings. Then click the Debugging tab. Check the box for Enable ASP server-side script debugging.

4. In Visual Studio, you need to enable Just-In-Time debugging for script files. In VS2008 go to Tools -> Options. Navigate to Debugging -> Just-In-Time. Click the check box to enable scrip debugging:

5. Add a “stop” statement to your VB code where you want the JIT debugger to break execution.

6. Finally, use the attach to process feature. In the Debug menu in VS2008. Ensure that “Attach to:” has “Script” selected and that your w3wp.exe has “Script” as one of the Types. If “Script” is not listed in the types, you have not configured IIS correctly for debugging.

7. You should now be able to navigate to the page you are trying to access. The debugger should load when the stop statement is reached and you can use the Debugging controls inside Visual Studio.

Next Page »

Blog at WordPress.com.