Thursday, November 19, 2009

Office 2010 direct download link

Huh! Office 2010 Beta is out for public. I’d started downloading. What about you?

Here is the direct download link.

http://care.dlservice.microsoft.com/dl/download/office2010/2/4/7/247A463c-af98-4778-b5a3-934cb8d198d1/professionalplus.exe

Thanks.

Saturday, October 17, 2009

How to get time elapsed after loading a webpage

How to get the time elapsed time after loading a webpage, using Callback() in MS Ajax library. Here is a sample script;

[Ajax-Script]

   1: <script type="text/javascript">
   2:         // Page Load
   3:         function pageLoad() {
   4:             var loadTime = {date : new Date()};
   5:             var callback = Function.createCallback(onBtnClick, loadTime);
   6:             
   7:             $addHandler($get("Button1"), "click", callback);
   8:         }
   9:  
  10:         function onBtnClick(event, context) {
  11:             var loadTime = context.date;
  12:             var elapseTime = (new Date() - loadTime) / 1000;
  13:  
  14:             $get("Text1").value = elapseTime + " Seconds";
  15:         }
  16:     </script>

[HTML-Script]

   1: <form id="form1" runat="server">
   2:     <asp:ScriptManager ID="ScriptManager1" runat="server">
   3:     </asp:ScriptManager>
   4:     <div>
   5:         Time elapsed since PageLoad: 
   6:         <input id="Text1" type="text" />
   7:         <input id="Button1" type="button" value="Get Elapsed Time" />
   8:     </div>
   9:     </form>

Thanks.

Sys.ArgumentTypeException: Object of type 'String' cannot be converted to type 'Number'

Another careless mistake : when I used the setLocation() method in the DomElement class in Sys.UI namespace. I tried to set the location of a DIV element using the value which I read from a textbox and I’s thrown with the error:

Sys.ArgumentTypeException: Object of type 'String' cannot be converted to type 'Number'

After taking a close look, I found it. The setLocation() method clearly states that I’ve to enter the (x,y) coordinates as integer data type. Take a look:

msajax_setlocn

Aaahhh…!!! See how careless I’m. Soon, I fixed the small careless-bug using ParseInt() as

msajax_setlocn_2

Hope, you’ll be more careful.

Thanks.

Reposition a DOM element using Ajax


For handling the DOM elements, Microsoft Ajax library has a Class named DomElement in the Sys.UI namespace. The DomElement class has 2 methods as follows:

a) getLocation(element-name): returns the (x,y) coordinate of the DOM element.
b) setLocation(element, x-pos, y-pos): sets the DOM element to (x,y) position.

Here is a simple example on repositioning a DOM element using the Ajax library;

[Ajax Script]

   1: <script type="text/javascript">
   2:         //Page Load
   3:         function pageLoad() {
   4:             var btnGet = $get("btnGetPos");
   5:             var btnSet = $get("btnSetPos");
   6:  
   7:             $addHandler(btnGet, "click", getXYLocation);
   8:             $addHandler(btnSet, "click", setXYLocation);
   9:             
  10:             
  11:         }
  12:  
  13:         // Get (X,Y) Location
  14:         function getXYLocation() {
  15:             var location = Sys.UI.DomElement.getLocation(div1);
  16:  
  17:             var tbX = $get("tbXpos");
  18:             var tbY = $get("tbYpos");
  19:  
  20:             tbX.value = location.x;
  21:             tbY.value = location.y;
  22:         }
  23:         
  24:         // Set (X,Y) Location
  25:         function setXYLocation() {
  26:             var tbX = $get("tbXpos").value;
  27:             var tbY = $get("tbYpos").value;
  28:  
  29:             alert(tbX + tbY);
  30:  
  31:             Sys.UI.DomElement.setLocation(div1, parseInt(tbX), parseInt(tbY));
  32:         }
  33:  
  34:         // Unload Page
  35:         function pageUnload() {
  36:             var btnGet = $get("btnGetPos");
  37:             var btnSet = $get("btnSetPos");
  38:  
  39:             $removeHandler(btnGet, "click", getXYLocation);
  40:             $removeHandler(btnSet, "click", setXYLocation);
  41:         }
  42:     </script>

[HTML-Script]


   1: <form id="form1" runat="server">
   2:     <asp:ScriptManager ID="ScriptManager1" runat="server">
   3:     </asp:ScriptManager>
   4:     <div id="div1" style="border: ridge 1px Black; width: 250px; text-align: justify;">
   5:     This is a sample text inside an HTML for demonstrating CSS Positioning using ASP.NET Ajax Library.
   6:     </div>
   7:     <br />
   8:     <div>
   9:     X-Coord: <input id="tbXpos" type="text" /><br />
  10:     Y-Coord: <input id="tbYpos" type="text" /><br />
  11:         <input id="btnGetPos" type="button" value="Get (X,Y) Location" />&nbsp;&nbsp;&nbsp;
  12:         <input id="btnSetPos" type="button" value="Set (X,Y) Location" />
  13:     </div>
  14:     </form>

Thanks.

DomEvents in ASP.NET AJAX

In my previous article Avoid Numbers in Textbox using ASP.NET AJAX, I showed how you can prevent numbers from being entered into a Textbox, using ASP.NET AJAX Framework.

In that example, the code segment e.charCod  and preventDefault() designates a property belonging to Sys.UI.DomEvent class. The Sys.UI is the class provided by the AJAX Framework to work with the DOM components in a webpage. And you can say, Sys is the root namespace or mother-of-all classes, just like System is the root namespace in C#.

After a slight Binging, I’s able to get a lot more properties of the Sys.UI.DomEvent class. Please note, they are cross-browser objects. Here is the list:

Properties Descriptions
rawEvent event-data object built by the current browser
shiftKey Returns TRUE, if SHIFT key was pressed
ctrlKey Returns TRUE, if CTRL key was pressed
altKey Returns TRUE, if ALT key was pressed
button captures the mouse-button click and has values as (leftButton, middleButton and rightButton)
charCode character code for the typed characters
clientX returns the  x-coordinate of the mouse location relative to client area of the page
clientY returns the  y-coordinate of the mouse location relative to 
client area of the page
target represents the Element that raised the event
screenX x-coordinate of mouse-pointer relative to screen
screenY y-coordinate of mouse-pointer relative to screen
type Returns name/type of the event (click/mouse)
preventDefault() Prevents the default action associated with that event
stopPropagation() Prevents the event from propagation to  the element’s parent nodes

Hope this is handy.
Thanks.

Thursday, September 17, 2009

Avoid numbers in Textbox using ASP.NET AJAX

An old question which was recently asked for an interview.

How can you prevent numbers from being entered into a Textbox, using Microsoft ASP.NET AJAX Framework. Thinking about UpdatePanel’s? Be clever, I have some client-side codes, using the framework itself.

[Client-Side]

   1: <script type="text/javascript">
   2:     function pageLoad() {
   3:         var tbox = $get("Text1");
   4:         $addHandler(tbox, 'keypress', text1_keypress);
   5:     }
   6:  
   7:     function text1_keypress(e) {
   8:         var code = e.charCode;
   9:         if (code >= 48 && code <= 57)
  10:             e.preventDefault();
  11:     }
  12:  
  13:     function pageUnload() {
  14:         var tbox = $get("Text1");
  15:         $removeHandler(tbox, 'keypress', text1_keypress);
  16:     }
  17: </script>

[Markup-Code]

   1: <form id="form1" runat="server">
   2: <asp:ScriptManager ID="ScriptManager1" runat="server">
   3: </asp:ScriptManager>
   4: <div>
   5:     Enter text value:
   6:     <input id="Text1" type="text" />
   7: </div>
   8: </form>

Thanks.

What are Mashups?

It’s been a long time, I’ve been hearing about mashups in the web world. Surprisingly, I came across the same when I’s reading Manning’s ASP.NET AJAX in Action.

A mashup is a web application that consumes content from more than
one external source and aggregates it into a seamless, interactive experience
for the user.

If you ask me a live example, here it is PageFlakes.

Tuesday, September 15, 2009

Exiting an Application on Error

Error handling is always a nightmare for every programmer. Because, testing you application in your development lab and exposing the application to a real-world lies in two extremities. The very reason where everyone go for alpha/beta and finally the RC release. Hey…Hey…I’m not going into core-concepts.

Recently, I’s re-optimizing a 6-month old c#-code (written in .NET 2.0) to .NET 3.5. Re-optimizing the code is a tough task, specially when the code was written by someone else. It deals with studying the code, understanding the logic and also good, if you can preview the test-cases encountered while re-designing.

When digging deep into the logic, I came across code-blocks where, even if you catch an Exception, you can’t continue because that might cause an abnormal exception at some other point. Huh! and I’s forced to kill the application at that point, using one-line code.

   1:  
   2: Environment.Exit(1);
   3:  

Remember, use this code safely. Use it only when you came across a do-or-die situation. Killing an application unnecessarily is bad way of programming.

Happy Programming.

Friday, August 28, 2009

Simple Checksum Generation in .NET

.NET framework is a tremendous installment from MSFT. No doubt! Every day-to-day life methodologies are wrapped into a Namespace/Class form. Say, Mail Sending class (System.Net.Mail), and so on. With the release of .NET 3.0, the number of Namespace/Class simply grew and they’ll.

A major installment in .NET 3.0 was Lambda Expressions. That even reduced the Line of Codes(LOC). In a nutshell, Lambda expression converts a bunch of codes into a single line of code, which we(I) may call as one-liner codes. Hard to believe, wait!

How, it affected a programmer? Does it made their work easier or does it made them lazy? Anyway, it made me lazy a bit. Because, whenever I start to implement repeating/familiar codes, say Checking Odd/Even values, etc, I always look for one-liners. Because, I don't want to repeat the same LOC every time. But, its better to know, what’s the internal implementation of one-liner, for Performance and Optimization reasons.

Recently, I’s developing an application for one of my client, in which I’ve to implement a checksum feature for a 9-Digit UniqueID (generated from another code-block).

The checksum technique was simple. My checksum has 3-Digits;
a) 1st Digit = Total Sum (of 9 Digits)
b) 2nd Digit = Even Sum (sums up the digits in the even place)
c) 3rd Digit = Odd Sum (sums up digits in the odd place)

Please note, Chances are that the Total Sum, Even Sum or Odd Sum may have 2 Digits, which you’ve to sum up again to a single digit.

The traditional mode of generating Even Sum, Odd Sum and Total Sum deals with a lot of mathematical computations and all, which is quite tedious to repeat every time. Here comes Lambda Expression, as a handy feature in .NET 3.0.

Here is the code segment for :

a) Total Sum

   1: int totSum = uniqueId.ToString().Sum(c => c - '0');

b) Even Sum

   1: bool flag = false;
   2: int evenSum = uniqueId.ToString().Sum(c => (flag = !flag) ? 0 : c - '0');

c) Odd Sum

   1: bool flag = false;
   2: int oddSum = uniqueId.ToString().Sum(c => (flag = !flag) ? c - '0' : 0);

I bet, this is quite handy.

Saturday, August 8, 2009

Changing Multiple GridView Row values using RowUpdating event

This is an extended sample of my previous post titled Change GridView Row values using RowUpdating Event. Recently, I got comment from NobleMule asking how he can edit multiple column/cell values.

I have 4 DDL's in a gridview. The first one is updating (mostly thanks to your code) but I can't get the other three to update. I'm a fairly new to C# and have never used the DictionaryEntry before. I naively tried just adding more if's (one for each ddl) within the foreach loop but that didn't work. Any help would be greatly appreciated.

I hope the 4 DDL’s are in 4 different Cells of a GridView. And wants to update the new values selected in those DDL’s into the database. As he’d mentioned my previous post sample is only meant for updating the DDL present in one Cell of your GridView.

I’ll break-up the concept here, so that you can deal with any number of Cells in the GridView. These are the tips to be kept in mind before start coding.

#1. Identify the Row in the GridView for updating, obtained via e.RowIndex

#2. Identify the Cell (column) in the GridView for updating.
     This value will be entered manually. Keep in mind, Cell indexing starts from zero.

#3. Locate the control (DDL here) using FindControl() method and cast it to appropriate type (ie, DDL type here).

These tips help you to find a Control in a particular (row,cell) of a GridView.

Now, the real case. You want to update the DDL in 4 different Cells of a GridView. All you’ve to do is to:
(a) Identify each Cell of GridView
(b) Access the Control (DDL here), from each Cell
(c) Get the Selected Value from DDL
(d) Update with new value

Lets assume that we’ve a Table having cells as:

IdColumn1Column2Column3Column4

And, Column1 having DropDownList1, Column2 having DropDownList2, Column3 having DropDownList3 and Column4 having DropDownList4.

Now, the rough piece of code looks like:

   1:  
   2: protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   3:     {
   4:         foreach (DictionaryEntry entry in e.NewValues)
   5:         {
   6:             DropDownList ddl;
   7:  
   8:             if (entry.Key.ToString() == "Column1")
   9:             {
  10:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[1].FindControl("DropDownList1");
  11:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  12:             }
  13:             if (entry.Key.ToString() == "Column2")
  14:             {
  15:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[2].FindControl("DropDownList2");
  16:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  17:             }
  18:             if (entry.Key.ToString() == "Column3")
  19:             {
  20:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[3].FindControl("DropDownList3");
  21:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  22:             }
  23:             if (entry.Key.ToString() == "Column4")
  24:             {
  25:                 ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList4");
  26:                 e.NewValues[entry.Key] = Server.HtmlEncode(ddl.SelectedItem.Text);
  27:             }
  28:         }
  29:     }

Hope this makes some sense.


Thanks.

Friday, June 26, 2009

How to get Duplicate records in a Table

Last day, I seamlessly entered into a situation where I want to list out all the repeating/duplicate records from a table. Though it sounds simple, its bit a tricky approach.

As a small example: consider a table say RepeatTest having 2 fields: Id and Url. To figure out the repeating Url records, you can write the query as:

   1:  
   2: SELECT Url, count(Url) FROM RepeatTest
   3:     GROUP BY Url
   4:         HAVING (COUNT(Url)>1)
   5:  

Sometimes you may also jump into sudden simple queries, as what I’d jumped into.

Update: This simple question was asked for the Huwaei International's written exam, held at Bangalore on July 2009.

Thursday, June 25, 2009

Automatic Implemented Properties in .NET 3.5

One of the cool feature that I like in .NET 3.5 is the automatic implementation of Properties in .NET 3.5. Here is a sample:

   1:  
   2: private string _name
   3:  
   4: public string Name
   5: {
   6:     get
   7:     {
   8:         return _name;
   9:     }
  10:     set
  11:     {
  12:         _name = value;
  13:     }
  14: }

Can be re-written as:

   1:  
   2: public string Name {get; set;}
   3:  
 
Best viewed in Internet Explorer 8.