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

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;
Advertisements

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.

Restore failed for Server – RESTORE HEADERONLY is terminating abnormally

Filed under: General Tips — Duy Nguyen @ 2:21 pm
Tags: , , , , ,

I found a great post on this that helped me resolve my problem:

http://dotnetguts.blogspot.com/2010/06/restore-failed-for-server-restore.html

Can’t edit file that is locked on Server 2008 with Notepad++

Filed under: General Tips — Duy Nguyen @ 2:20 pm
Tags: , , , , , , ,

When trying to edit a file on Windows Server 2008 with Notepad++, you might get this error:

“Please check whether if this file is opened in another program”

Run Notepad++ as an administrator by right-clicking on the shortcut, and choose, Run As Administrator.

Strange.

IIS 7 Application Pool Identities

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

In Windows 7 and Windows 2008 R2 (IIS 7.5), the default identity for application pools is set to ApplicationPoolIdentiy.

ApplicationPoolIdentiy specifies the service account that is used when you create an application pool. The service account is the same name as the service account. So if you created MyNewAppPool, the ApplicationPoolIdentiy is IIS AppPool\MyNewAppPool. You won’t be able to find this service account if you just browse or search for “MyNewAppPool” alone. You MUST specify IIS AppPool\MyNewAppPool.

Previously in older versions of IIS, it was set to “NT Service\NetworkService.” You can now set the ACL for “IIS AppPool\<app pool name>” – the user account created for each new application pool.

This is a great article about Application Pool Identities in IIS7

Click Here

How to find SQL Instances installed and uninstall SQL Server 2005 Express

Filed under: General Tips — Duy Nguyen @ 2:17 pm
Tags: , , , , , ,

Run this script in Management Studio to find what instances are installed:

http://blogs.msdn.com/b/sqlserverfaq/archive/2009/03/20/how-to-list-all-the-installed-sql-server-on-the-server-using-tsql.aspx

1. In add/remove find Microsoft SQL Server 2005 Express
2. Uninstall
3. Choose Microsoft SQL Server 2005 Express instances
4. If prompted to do so, stop any Microsoft SQL Server 2005 Express services in services.msc

How to use Microsoft’s DebugView with ASP.NET for debugging

Filed under: General Tips — Duy Nguyen @ 2:17 pm
Tags: , , , , ,

This is a great article.

UPDATE: But this doesn’t seem to work with .NET 4 web apps. Only 3.5 or less.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=337

Next Page »

Create a free website or blog at WordPress.com.