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.

 
Best viewed in Internet Explorer 8.