Yanesh Tyagi writes …

October 21, 2008

Getting Browser Information and Capabilities in ASP.Net

Filed under: .Net, Programming, Web Technology — yaneshtyagi @ 7:40 am
Tags:

While developing a web site or web application, one need to keep in mind the features supported by different browsers. This is very unfortunate that there is no industry standard in the browser market. Fortunately ASP.net provides HttpBroserCapability class to determine the browser type and features supported by the browser.

The browser property of request object returns the object of HttpBrowserCapability object. This object has several properties which provide the information about the browser type, version and features supported by this.

Following C# code explains how you can use HttpBrowserCapability class to fetch browser information.

private void btnBrowserInfo_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = “Browser Capabilities\n”
+ “Type = “                    + browser.Type + “\n”
+ “Name = “                    + browser.Browser + “\n”
+ “Version = “                 + browser.Version + “\n”
+ “Major Version = “           + browser.MajorVersion + “\n”
+ “Minor Version = “           + browser.MinorVersion + “\n”
+ “Platform = “                + browser.Platform + “\n”
+ “Is Beta = “                 + browser.Beta + “\n”
+ “Is Crawler = “              + browser.Crawler + “\n”
+ “Is AOL = “                  + browser.AOL + “\n”
+ “Is Win16 = “                + browser.Win16 + “\n”
+ “Is Win32 = “                + browser.Win32 + “\n”
+ “Supports Frames = “         + browser.Frames + “\n”
+ “Supports Tables = “         + browser.Tables + “\n”
+ “Supports Cookies = “        + browser.Cookies + “\n”
+ “Supports VBScript = “       + browser.VBScript + “\n”
+ “Supports JavaScript = “     +
browser.EcmaScriptVersion.ToString() + “\n”
+ “Supports Java Applets = “   + browser.JavaApplets + “\n”
+ “Supports ActiveX Controls = ” + browser.ActiveXControls
+ “\n”;
txtBrowserInfo.Text = s;
}

I have placed one button on the aspx page and named it as Browser Information. The id of the button is btnBrowserInfo. On the click event of the button, I wrote the above code.

I also placed a TextBox on the page and named it as txtBrowserInfo.

First I initialized an HttpBrowserCapability object named browser with the Request object’s browser property. I fetched the different property of browser and concatenate these into a string variable. Finally I displayed the concatenated string into the text box.

kick it on DotNetKicks.com

September 27, 2008

My First ASP.Net MVC Appliacation

Filed under: .Net, Programming, Technology, Web Technology — yaneshtyagi @ 4:50 pm
Tags: ,

I was thinking about MVC since I read first post on ScottGu’s blog. Yesterday, on Friday night, I decided to create my first MVC applications. I downloaded Visual Web Developer 2008 Express Edition 2008 last night. That 128 MB download took quite a long time to download. Then I downloaded MVC preview 5. After successful installation of MVC, I was ready to fly…

Finding a suitable example on the web:
Before starting develop a new MVC project,  I needed a good example. After searching the web for a while, I found two links that seems to be useful.

First was a 4 part tutorial written by ScottGu. I was a detailed introduction to MVC framework and how it works. I was like a step by step DIY guide. Scott demonstrated how to create a complete e-commerce site using MVC framework.

The second choice was a simple To Do List application. This application allowed one to enter a list of tasks, save these into database and mark them complete. Both applications used LINQ to SQL for accessing database.

I decided to go with the Task List application as this was simple and small application. This app is like the typical Hello World example with database support. I took only one hour to complete this app and run it properly.

Starting the new project
I opened VWD (Visual Web Developer0 and choose to create new project. MVC applications are developed as web applications. They are different from web sites. In the New Project dialog, I chose ASP.NET MVC Web Application. This automatically created a sample application in the solution explorer with three special meaning folders – Controller, Model and View. I deleted the classes and pages auto-generated by the WVD. The I proceed step by step as instructed in the example application.

Step By Step Progress Through Solving Issues:
Once I completed typing (yes, I preferred to type instead of copy-pasting it) the code and creating all the classes and pages, I was time to see the code in action. But as you know, no code runs for the very first time. Although VWD (and VS as well) minimizes the  chances of syntax errors to zero, many other types of error always occurs in the code. I face following problems for which I again need to do some Googling.

The ViewData Problem:

“The name ‘ViewData’ does not exist in the current context”

Even before compiling the code, my Index.aspx view warned me that ViewData is undefined by placing a red line below this. After some unsuccessful silly adjustment to the code, I go for Googling. I found a post in ASP.net forums asking for the same question. ScottGu answered the post by saying – “Have you tried compiling the project?  It could be that you haven’t done a compile since you added the page, and so intellisense with C# hasn’t picked up that you are deriving from a base-class yet”. This was very strange for me but I decided to go with Scott and compiled the code successfully. But when I run the code, the same problem reappeared.

After 20 minutes of unsuccessful Googling, I decided to use trial-and-error method. And finally I got the solution.

Solution:
The root cause of the problem was that I added a Web Form in my Views folder instead of MVC View Content Page. The normal web form is inherited from System.Web.UI.Page class  while an MVC View Content Page is inherited from System.Web.Mvc.ViewPage class. I change the parent class to ViewPage and it worked fine.

Primary key error in inserting the record:
Now I rum my application without any error. The Index page that displays a list of all tasks appears on the browser. I opened create page to add a new task. On pressing submit button, it gave primary key related error. The error was cause by the InserOnSubmit method of the LINQ generated class. The error was quite obvious that I forgot to define primary key on the table. I had an Id column with its Identity attribute set to on. So, ideally any insert statement should not have issue with the primary key. But to fix the error, I modified the table structure and declare Id column as primary key. But the problem still persisted. Like the above issue, Googling was unsuccessful in this case as well and I had to go for trial-and-error method.

Solution:
The solution was quite simple. I had to open the dbml file which is used for LINQ-to-SQL. I deleted the table from this file and again dragged that table into dbml designer from Server Explorer. Pressed F5 and the page was opened successfully in the browser. Opened the create page, added a task and pressed submit button. No error…
I checked into database table and the record was successfully inserted.

So this was the experience of developing first MVC application. The overall experience was good and exciting. I liked the simple and easy-to-read URLs that were displayed in the browser’s address bar.

Keep watching for more in ASP.Net MVC

Technorati Tags: ,

kick it on DotNetKicks.com

Scott Guthrie’s ASP.net MVC Tutorial Links

Filed under: .Net, Programming, Technology, Web Technology — yaneshtyagi @ 12:13 pm
Tags: ,

ASP.NET MVC Framework
One of the things that many people have asked for over the years with ASP.NET is built-in support for developing web applications using a model-view-controller (MVC) based architecture. Last weekend at the Alt.NET conference in Austin I gave the first…

ASP.NET MVC Framework (Part 1)
Two weeks ago I blogged about a new MVC (Model View Controller) framework for ASP.NET that we are going to be supporting as an optional feature soon. It provides a structured model that enforces a clear separation of concerns within applications, and…

ASP.NET MVC Framework (Part 2): URL Routing
Last month I blogged the first in a series of posts I’m going to write that cover the new ASP.NET MVC Framework we are working on.  The first post in this series built a simple e-commerce product listing/browsing scenario.  It covered the high…

ASP.NET MVC Framework (Part 3): Passing ViewData from Controllers to Views
The last few weeks I have been working on a series of blog posts that cover the new ASP.NET MVC Framework we are working on.  The ASP.NET MVC Framework is an optional approach you can use to structure your ASP.NET web applications to have a clear…

ASP.NET MVC Framework (Part 4): Handling Form Edit and Post Scenarios
The last few weeks I have been working on a series of blog posts that cover the new ASP.NET MVC Framework we are working on.  The ASP.NET MVC Framework is an optional approach you can use to structure your ASP.NET web applications to have a clear…

ASP.NET MVC Preview 5 and Form Posting Scenarios
This past Thursday the ASP.NET MVC feature team published a new “Preview 5″ release of the ASP.NET MVC framework.  You can download the new release here .  This “Preview 5″ release works with both .NET 3.5 and the recently…

Technorati Tags: ,

kick it on DotNetKicks.com

June 3, 2008

True / False in classic VB

Filed under: Programming, Technology — yaneshtyagi @ 10:03 am
Tags: ,

In classic VB (and classic ASP as well), zero is considered as false and any other non-zero integer is considered as true.

While debugging an asp page, I come across following line:

If Not Request.Form.Count Then

Now form count was 2 (true) so the condition should evaluate as false (Not true). But this was always evaluating as true. It took me half an hour to find the catch.

What actually happening here was that Not is a bit-wise operator in VB. It flips the bits. So,

2 is 0000 0010 in binary.
NOT 2 is 1111 1101 in binary.

Considering that the last bit is sign bit, this becomes -3.

Now our condition If NOT (2) becomes If (-3), which always evaluates to true (non-zero integers are always true in VB).

Technorati Tags: ,,,

May 26, 2008

Automatic Formatting of Markup (HTML) in ASP.NET

Daniel at dimebrain did a really fantastic work writing a plug-in for VS that can format the markup in asp.net. When you build a web page using the VS designer, it auto generates the markup. But the auto generated code so clustered and impossibly hard to read.  You have to manually format the markup (HTML) code by hand. This is always very tiresome and boring job. Also this is wastage of time.

Daniel also felt the same. After being inspired by the Joe Stagner, he developed this plug-in. It provides an additional Edit menu item and hotkey (Ctrl+K, Ctrl+Z) to automatically line up attributes in a selection of text, or format the entire document if no text is selected. The meat of the add-in is a handful of regular expressions that parse tags (XAML, HTML, and ASP.NET directives) and a few IDE tools to line them up according to their indent level.

Download Links:

Screen Shots:

December 4, 2007

Effective Use of Visual Studio 2005 – Keyboard Shortcuts

Filed under: .Net, Programming — yaneshtyagi @ 11:18 am
Tags: , ,
As a hardcore developer I found that typing is easy to me then using mouse. Mouse is an obstacle. Taking hands off the keyboard is time consuning. It takes me out of comfort zone. I like to use keyboard where ever possible and efficient. Thanks to the developers of Visual studio 2005, they provided keyboard shortcuts to the most frequest tasks.
Though the use of named commands is always recommended instead of keyboard shortcuts. But I found that these key strokes are most common and no one reassign these to custome actions. Its safe to get used to these shortcuts.

Here is a list of my favourite shortcusts.

Tool Boxes And Windows
  • CTRL+ALT+L: View Solution Explorer. I use Auto Hide for all of my tool windows to maximize screen real estate. Whenever I need to open the Solution Explorer, it’s just a shortcut away.
  • CTRL+ALT+X: Toolbox Window
  • CTRL+ALT+O: Output Window
  • CTRL+\, E: Error List Window
  • CTRL+\, T: Task List Window
  • F7: Toggle between Designer and Source views.
  • CTRL+PgDn: Toggle between Design and Source View in HTML editor.
  • SHIFT+ALT+Enter: Toggle full screen mode. This is especially useful if you have a small monitor. Since I upgraded to dual 17? monitors, I no longer needed to use full screen mode.
  • CTRL+SHIFT+A: Add New Item Window
  • ALT+F+F: Recent Files List

Building, Running And Debugging

  • F10: Debug – Step over.
  • F5: debug – Start
  • F11: debug – Step into
  • SHIFT+F11: Debug – step out
  • CTRL+F10: Debug – run to cursor
  • F9: Toggle Breakpoint
  • CTRL+SHIF+B: Build Solution. Related shortcuts:
  • ALT+B, U: Build selected Project
  • ALT+B, R: Rebuild Solution

Code Window

  • CTRL+D or CTRL+/: Find combo
  • CTRL+M, O: Collapse to Definitions.
  • CTRL+M, M: Toggle Outline Expension. Collapsed will become expanded and expanded will become collapsed.
  • CTRL+K, CTRL+C: Comment block.
  • CTRL+K, CTRL-U: Uncomment selected block
  • CTRL+-: Go back to the previous location in the navigation history.
  • CTRL+SHIFT+-: Go to the next location in the navigation history.
  • CTRL++: Select from last mouse pointer position to current mouse pointer position.
  • CTRL+ALT+Down Arrow: Show dropdown of currently open files. Type the first few letters of the file you want to select.
  • CTRL+K, D: Format code.
  • CTRL+L: Delete entire line.
  • CTRL+G: Go to line number. This is useful when you are looking at an exception stack trace and want to go to the offending line number.
  • CTRL+K, X: Insert “surrounds with” code snippet.
  • CTRL+K, K: Toggle bookmark.
  • CTRL+K, N: Next bookmark.
  • CTRL+K, P: Previous bookmark.
  • CTRL+K, L: Clear bookmark.
    CTRL+K, I: Quick Info
  • F12: Go to definition of a variable, object, or function.
  • SHIFT+F12: Find all references of a function or variable.

November 29, 2007

Effective Use of Visual Studio 2005 – Named Commands

Filed under: .Net, Programming, Technology — yaneshtyagi @ 5:43 pm
Tags: , ,

Command Window: Ctrl+Alt+A

Named commands are used to access VS features from Command Window. Visual Studio 2005 provides full intellisense support in the command window. I experienced that using named commands is much faster than accessing these features with mouse.

Named commands allow you to access and use menu commands in the command window. To use named commands, simply type the name of menu. Press ‘.’ and VS will provide intellisense. Type the name of the menu option or select it from intellisense.

Example:

To access Find Results 1 window you have to

1. Click on the ‘View’ menu and then
2. Click on the ‘Find Results’ and
3. Finally choose ‘Find Results 1′.

Now if you want to use named commands,
1. Press Ctrl+Alt+A to activate command window.
2. Type View.findresults1. You don’t need to type full text. Intellisense provides yoy write text. Just press enter.

Some Examples of Named Copmmands:

  • Window.NewWindow: Open a new window on your code.
  • Tools.Options: Bring up Options dialog box.
  • Edit.ViewWhiteSpace: (Ctrl+R, Ctrl+W) Replace white spaces with a dot.
  • Convert tab to spaces: Ctrl+K, Ctrl+D
  • Window.ActivateDocumentWindow: Switch to document window.
  • Window.AutoHide: Hide command window. (Use Ctrl+Alt+A to show command window)
  • Window.AutoHideAll: Hide all windows except document windows. It will hide even pinned-up windows too.
  • Window.ShowEzMDIFileList: Show Easy MDI File List.
  • Window.Split: Split code window into two. Both windows show same files but you can scroll them independently. Issue same command to close splitting.
  • Window.Windows: Open ‘Windows’  window. More Info
  • Edit.ClearAll: Clear Command Window.

Named Command and Keyboard Shortcuts

Usually VS features can be accessed in two ways: using keyboard shortcusts or  using named commands. But using named commands is easier than using keyboard shortcuts:

  1. You need to learn keyboard shortcuts. But named commands are not required to learn, as they are based of the menu navigation which is most commanlyt used.
  2. Keyboard shortcuts are customizable. You (or anybody else) can change them. If you are working on some other machine, you may not have same keyboard shortcuts.
  3. There is no help (intellisense) provided for keyboard shortcuts.

Images:

1. Easy MDI File List

EzMdiFileList

2. Split code window
Untitled

November 28, 2007

Effective Use Of Visual Studio 2005 – Handling Open Documents

Filed under: .Net, Programming, Technology — yaneshtyagi @ 12:48 pm
Tags: , , ,
Visual Studio 2005 provides some excellent document management capabilities. Most of the we are not aware of these features. One of the feature is open document tab groups. Unlike VS 2003, it shows only as much tabs as fits in the window width. There is no scrolling. Instead, VS 2005 provides dropdown menu to select from all open documents. Inseated of selecting open documents, you can also do some other functions.
I am planning to write a series on useful-but-less-known features of VS 2005. This is second post in the series. (read first post)

In continuation to my previous post on managing open documents, here are some more tricks.

Right click on the open document tab group. A context menu will appear with following options:
  1. Save default.aspx.vb
  2. Close
  3. Close all but this
  4. Copy full path
  5. Open containing folder
  6. New horizontal tab group
  7. New vertical tab group

Save default.aspx.vb

Allows you to save currently selected document. Currently selected document is the one which is open in the window.
Close
Close currently selected document.
Close all but this
Close all open documents except the currently selected document.
Copy full path
Copy full path of the currently selected document to the clipboard.
Open containing folder
Open the folder that contains the currently selected document. The folder is opened in the windows explorer.

New horizontal tab group
Open another document window. The two windows are tiled horizontally. Both window will have document tab group. This is useful when you want to open on two group of documents simultaneously.

New vertical tab group

Same as above option. But this makes the windows tiles vertically instead of horizontally.

November 27, 2007

Effective Use of Visual Studio 2005 – Closing Selected Window

Filed under: .Net, Programming, Technology — yaneshtyagi @ 10:20 am
Tags: , , ,
To close open documents selectively:
1. Go to Window -> Windows.

2. This will show list of all open windows (not just those which you see on the tabs).

3. Select the windows you want to close. Use shift (or ctrl) key for multiple selection. You can also use ctrl+A to select all windows.

4. Click “Close window(s)” button to close selected windows.

I experienced that the speed of loading a project into visual studio is directly proportional to the number of open documents. Generally, we open the document as we need it. And then we don’t close it. Visual studio 2005 displays only a few number of documents on the tabs. Rest remains open but are hidden. You can click on the down arrow near the ‘X’ button of the document window to select the open document. To keep you project loading fast, always keep minimum number of open documents.

November 20, 2007

Visual Studio 2008 and .NET 3.5 Released

Filed under: .Net, Programming, Technology — yaneshtyagi @ 6:44 am
Tags: , , ,

Microsoft shipped Visual Studio 2008 and .NET 3.5 today. Here are some resources for VS 2008:

1. ScottGu’s weblog is the primary and authentic source of information।
२. Somasegar’s Weblog
3. Paul Andrew blogged about a downloadable poster of commonly used types and namespace in .NET 3.5 (in pdf format), which is worth checking out.
4. View ASP.NET 3.5-specific videos at Microsoft’s official asp.net website.
5. Brad Abrams wrote about Design Guidelines, Managed code and the .NET Framework 3.5 in his blog.
6. Here is a 31 minute video at Channel 9.
7. VS 2008 is not shipped with MSDN. You can Download MSDN Library for VS 2008 from here.
8. The details about VS 2008 express edition (free) are available here.

Next Page »

Blog at WordPress.com.