The contents of the post has been moved to here. Please follow the link to see the contents.
Sorry for the inconvenience.
Thanks!
All Day, I Dream about Codes
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.
.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.
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:
| Id | Column1 | Column2 | Column3 | Column4 |
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.
The contents of this post has been moved to http://blogs.cametoofar.com/post/Adding-Controls-to-Visual-Studio-Toolbox.aspx.
Sorry for inconvenience.
Thanks
While programming, I use the comment/un-comment menu in the Visual Studio toolbar. The commented portion looks like, for example
<%-- Some Comment/Code here --%>
Later on only, I paid attention to it; as what makes it different from our normal HTML comments like;
<!-- Some Comment/Code here -->The difference is simple. The former is called server-side comment & the latter is client-side comment. Serer-Side comments are not returned to the browser, while the client-side comments are returned to the browser.
Realizing, how I strained/managed to learn complex codes, without paying attention to such a silly thing. We all need Big without realizing what is Small.
Suppose that the Web Server from which a Web-Service client is requesting Web-Service supports compression; then the web-service client will receive respone as compressed data.
We can disable the decompression by setting the EnableDecompression property to true.
// on client web-applicationService srvc1 = new Service();srvc1.EnableDecompression = true;// call your web-service method here (example)string dtime = srvc1.ReturnDateTime();:
:
: