Yanesh Tyagi writes …

November 27, 2009

No return statement in the finally clause, please.

Filed under: .Net, Programming, Technology — yaneshtyagi @ 1:05 am
Tags:

You cannot put return statement in the finally clause because Control cannot leave the body of a finally clause (Compiler error code CS0157)…Why? MSDN says that all statement in the finally clause must execute. MADN also states that

The purpose of a finally statement is to ensure that the necessary cleanup of objects, usually objects that are holding external resources, happens immediately, even if an exception is thrown.

Thus for releasing all locks and hold objects or in other words control cannot leave the finally block before finishing the cleanup task. However, an exception can be occurred in the finally block. And if it is not handled properly, code execution will stop and error is thrown.

Technorati Tags: ,,
Digg This

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

September 23, 2008

Project Rosetta

Filed under: .Net, Programming, Technology, Web Technology — yaneshtyagi @ 7:15 pm
Tags:

ASP.net programmers, are you ready for the Silverlight? Still wondering what Silverlight is?

Silverlight is the Microsoft’s answer to the Adobe’s Flesh. And considering the Microsoft’s marketing strategy and market share, I can bet this is going to be the next big thing in the Web 2.0 age.

So does that mean learning a new technology? The answer is – Yes. As an asp.net programmer you have to learn this Silver Light thing. Soon those static (including DHTML) websites will be history. This is the RIA age. RIA – Rich Internet Application – are getting more and more popular following Moory’s law.

But here is a good news from Microsoft. Microsoft has started a new project that will help one to learn this Silverlight stuff.  Below is the excerp from today’s MSDN Flash:

Project Rosetta
Still wondering what all the buzz around Silverlight is about? Check out Project Rosetta, a site dedicated to helping designers and developers build applications in Silverlight while taking advantage of skills they already know from their experience with Flash programming.

So if you know flash (or don’t. doesn’t matter), it is really easy to learn silverlight. But as with any other design skills, learning and being creative are two different things.

So get your hands started on Silverlight ASAP and get experienced before it’s too late.

Related Links:

August 10, 2008

VB.net to c# converter

Filed under: .Net, Programming, Technology — yaneshtyagi @ 9:58 am
Tags: , ,

Many times we need to convert VB.net code to c#. There are many utilities to do this. but none of these is perfect as these have one or other shortcomings. Most of these utilities are the windows apps written in c# language. The draw back of windows app is that you need to upgrade the app at times.

I was looking for an online app for the conversion. And after some googling I found a cool online tool from the Developer Fusion Lab. This conversion tool is quite accurate in conversion and if it could not convert any code then it inserts the error message into the code. So one can easily identify the error message and do the manual conversion.

Link:
Convert VB.NET to C# – A free code conversion tool from Developer Fusion

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:

February 18, 2008

Unicode Tool tip

Filed under: .Net, Programming, Web Technology — yaneshtyagi @ 1:39 pm
Tags: ,

While using windows XP, I was facing a problem. I was working on a multilingual web site. we were showing tool tips to help user to understand what a particular button will do or where a link points. The tool tips were working fine for language which uses English language characters. But in case of other characters (read unicode) such as Chinese or Arabic, it showed small boxes instead of letters.

I had to do lot of research on the internet (googling is my first habit) and here is what I found:

To set your tooltip font to be able to display Unicode characters:
Right click on the desktop, pick Properties -> Appearance -> Advanced ->Item: ToolTip, then set the font to Arial Unicode MS or other large font.

This will show unicode characters in the tool tip.

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

Next Page »

Blog at WordPress.com.