Abhi's den...

The piece of code which nobody cracked...
DataTable to Dictionary using LINQ

Sometimes we have to create generic dictionary from the values in datatable. Usually we will loop through all the rows in the datatable and add the relevant keys to dictionary object

 

for (int _iExRowCnt = 0; _iExRowCnt < dsReturn.Tables[0].Rows.Count; _iExRowCnt++)

{

//add some code to chek null values & other validations if any

_dictObj.Add(dsReturn.Tables[0].Rows[iExRowCnt][0].dsReturn.Tables[0].Rows[iExRowCnt][1]);

}

 

After LINQ was introduced there better way of doing the same addition.
In the following code i am creating a datatable and populaing few dummy records.

DataTable dtTable = new DataTable();dtTable.Columns.Add(new DataColumn("ColumnNo", typeof(System.String)));
dtTable.Columns.Add(
new DataColumn("controlType", typeof(System.String)));
dtTable.Columns.Add(
new DataColumn("showVal", typeof(System.Boolean)));

 

DataRow dr;

for (int i = 0; i < 10; i++)
{
dr = dtTable.NewRow();
dr[0] = dtTable.Rows.Count + 1;
dr[1] = i.ToString() +
"Value";
dr[2] =
false;
dtTable.Rows.Add(dr);
}

//In the following code I am using LINQ to create a new Dictionary<string,string>

//You can filter the data and checf for the conditions in data which you dont want in dictionary

 

var dic = (from order in dtTable.AsEnumerable()

where order.Field<Boolean>("showVal") == false

select new

{

myColumnNo = order.Field<
String>("ColumnNo"),myControlType = order.Field<String>("controlType")

}).AsEnumerable().ToDictionary(k => k.myColumnNo, v => v.myControlType);

 

This code snippet uses Enumerable.ToDictionary Method
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.todictionary.aspx

var dic will hold Dictionary<string,string> object. In the sample code it will have 10 elements

 For further reading

Posted: Dec 28 2009, 02:35 PM by cabhilash | with 1 comment(s) |
Filed under: , ,
Anonymous types for dummies
Anonymous types are used to define strong types without defining the type. Anonymous types are strongly typed and checked at compile time. It provides a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. Anonymous types are reference types that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object. This type is widely used by LINQ, because LINQ returns dynamically-shaped data, whose type is determined by the LINQ query



To understand better look at the following code
static void Main(string[] args)
        {
               string[] names = { "Abhi1", "abhi", "abhi11", 
                       "george", "bush", "britney", 
                       "gandhi", "David" };

            
             IEnumerable<string> varAnnoyType = from str in names 
                                         orderby str
                                         select str;

            //following code also valid
          //var varAnnoyType = from str in names 
          //                               orderby str
          //                               select str;

             foreach (string strname in varAnnoyType)
                 Console.WriteLine(strname);


                    Console.ReadLine();
                                      
        }

anonymous type is an object which supports IEnumerable interface. 

In the above example an array of names is created (took the array collection from msdn) , anIEnumerable<string> type is created, see the keyword like from used to iterate eact item in collection, orderby clause orders (without LINQ imagine the dictionaries , temp variables and other techniqies used to sort items now this is really cool). You can add a Where clause also before the select keyword. If any conditions are specified then select will retrieve only those items which satisfies the where clause.

  var varAnnoyType = from str in names
                                where str.StartsWith("abhi")
                                orderby str
                                select str;

The for each iteration can be written like the following also.
foreach (var strname in varAnnoyType)
                 Console.WriteLine(strname);

If two or more anonymous types have the same number and type of properties in the same order, the compiler treats them as the same type and they share the same compiler-generated type information.

An anonymous type has method scope. To pass an anonymous type, or a collection that contains anonymous types, outside a method boundary, you must first cast the type to object. However, this defeats the strong typing of the anonymous type. If you must store your query results or pass them outside the method boundary, consider using an ordinary named struct or class instead of an anonymous type.

Anonymous types cannot contain unsafe types as properties.

Because the Equals and GetHashCode methods on anonymous types are defined in terms of the Equals and GetHashcode of the properties, two instances of the same anonymous type are equal only if all their properties are equal.
LINQ for dummies - an overview

Why do we need LINQ?


Most of us would have wrote code to access data from different data sources a database, in memory objects , XML files or from other formats. We have different guidelines, architectures and methods to process and retrieve these data collection. For a data control in form it is immaterial whether the data is from XML or any other data sources. We have many relational OO databases but there always the gap between the data and its processing in Objects in any modern languages.



Is LINQ the Holy Grail?
I can’t decide on that. But LINQ tries to fill the vacuum between the datasources and their successful interpretation in Objects. With LINQ, Microsoft’s intention was to provide a solution for the problem of object-relational mapping, as well as to simplify the interaction between objects and data sources. LINQ eventually evolved into a general-purpose language-integrated querying toolset. This toolset can be used to access data coming from in-memory objects (LINQ to Objects), databases (LINQ to SQL), XML
documents (LINQ to XML), a file-system, or any other source.


LINQ can be used to access any type of object or datasource. The syntax remains the same. Previously we had to use different methods like ADO.Net.  XPath, IO packages etc to retrieve data ( ok still we can use these methods and in many cases I still prefer them over LINQ)


Broadly classifying we have three major categories of LINQ
  1. LINQ to Objects,
  2. LINQ to SQL,
  3. LINQ to XML


Don’t worry there are other categories like LINQ to datasets. LINQ to Entities ( with ADO.net entity framework). In Visual Studio you can write LINQ queries in Visual Basic or C# with SQL Server databases, XML documents, ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable(T) interface. In short .NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. Third parties are also free to replace the standard query operators with their own implementations that provide additional services such as remote evaluation, query translation, optimization, and so on. By adhering to the conventions of the LINQ pattern, such implementations enjoy the same language integration and tool support as the standard query operators.


Next – LINQ in action ... 

Posted: Dec 26 2009, 08:50 PM by cabhilash | with 1 comment(s)
Filed under: , ,
Difference between a constant and readonly variables/fields : (constant vs readonly)
Most of us would have encountered these questions 
  • What is the difference between constant and readonly fields?
  • When to use constant and when to use readonly fields?
  • What is the advantage/disadvantage of each?
 Constants
  • In C# you can declare a constant like this "const" is a keyword.
    public const string _constStrVar = "I am a static const str val";
  • A constant variable should have value at design time. 
  • All the constant variables are static ie they are shared across all the instances of the class. You dont have to add the keyword "static".
  • Constants are copied to all the dlls where is refereeing the parent class ie even if you change the the original constant value and recompile the parent dll , the other dll will still use the old value. The size of the executable goes up since all these constants are copied to the respective dlls
Read Only  
  • Read only variables are created usually in the constructor of class. there fore it will have values before the constructor of the class exists
    class MyClass
    {
        public readonly string _strReadonly;
        public void MyClass(string strVal)        {            _strReadonly = strVal;        }           } a read only variable will have different values for each object whereas a constant variable will have only one. 

http://thetechjungle.blogspot.com/

Posted: Dec 26 2009, 08:32 PM by cabhilash | with no comments |
Filed under: , ,
using LINQ retrieve top N rows
Normally to return top N rows we use an SQL statement similar to the one belowSelect top N * from table
 but How can we achieve the same thing in DataTableI have seen many examples, using different methods. Most of the methods centered around the idea of creating new columns with automatic values increment and using them as index 
 There is better method using LINQ 
 public  DataTable GetTopNFromDataTable(int TopRowCount, DataTable dtSource)
        {

            
var dtTrec = from item in 
dtSource.AsEnumerable()
                         
select 
item;
            
var 
topN = dtTrec.Take(TopRowCount);
            
DataTable dtNew = new DataTable
();
            dtNew = dtSource.Clone();

            
foreach (DataRow drrow in 
topN.ToArray())
            {

                dtNew.ImportRow(drrow);
            }

            
return 
dtNew;
        }
  var dtTrec - stores the item in datatable, Using the Take function of Linq the first N rows is filtered

            
var topN = dtTrec.Take(TopRowCount);
 

Now how to retrieve the rows between N1 &amp; N2, just use the skip function along with Take as shown below

  public DataTable GetTopBetweenFromDataTable(int intFrom, int intTo, DataTable dtSource)
        {
......   var topN = dtTrec.Skip(intFrom).Take(intFrom); ......} 

Viewstate error : 12031

Few days ago I was asked to look into an issue. In our application we have created dynamic grids to show data from database. This ASPX page was Ajax enabled. Moreover all the rows of the grid were in edit mode ie. the normal controls like textbox,dropdowns etc  were displayed in all the rows. This grid was Paginated. But for the past few days the paging was not working.

I executed the page and found that the page was generating an error 12031  with the following message

Sys.webForms.PageRequestManagerServerErrorException:An unknown error occurred while processing the request on the server .the status code returned from the server was:12031.

On my first round of analysis I found the issue with Viewstate. If the viewstate is large then connection is reset (ERROR_INTERNET_CONNECTION_RESET ). In local machine with less load this problem will not occur but as the load & network latency increases this error will come. Once this error is generated the general events of grid is not triggered. So advised my team to minimize the use of viewstate. It will help in to load the page faster & reduce the network traffic. I can increase the maxRequestlength value to allow more data but ideally i shouldn't increase that.

In the page tested by me a page with grid  with 372 rows generated a viewstate of 4.2 mb. you can disable viewstate using EnableViewState="false" for the individual controls and for the entire page also.

With every post back this much of data is transferred back & fro & this will result in low response time.

The developer was saving all the data into viewstate in page load and from that the data was populated to the grid.

Better solution is to retrieve only the required data from database, minimize the use of  viewstate, Viewstate can be compressed also. About all these I will update in another post.

Posted: Oct 13 2009, 12:41 PM by cabhilash | with no comments |
Filed under: ,
Add Web service reference- Components required to enumerate web references not installed

After playing with Web services for so many years this was a tricky error which kept me thinking for few minutes.

 Today when i tried to add WCF reference to my application it gave the following error,

Error

---------------------------
Microsoft Visual Studio
---------------------------
Failed to update Service Reference 'XXXBusiness.Reference'.
Error:The components required to enumerate Web references are not installed on this computer. Please re-install Visual Studio.(0x80004002)


What could have caused this error? Then i remembered there was a alert box from VS few hours ago which informed that some package was not loaded successfully and whether i wanted  VS to stop loading that package in future.Without reading the full message I accidentally pressed "YES" I know mea cupla.

 But i have to move forward ...

Now the solution

  1. Close all open Visual Studio instances
  2. Open the Command prompt available with Visual Studio and type devenv /resetskippkgs (You can directly type this to Run command or normal command prompt) 
  3. Add reference to the Web service/WCF service
Now I am back in business :)
WCF 8192 issue

This was a simple issue which baffled few brains in my project. For the past few weeks i was seeing the mails about this. What was the exact problem?

 Whenever a large xml stream is passed through WCF it generated the following error. Everything was fins at the ASP.net client side. Almost all the known properties of

 Problem: 
The maximum string content length quota (8192) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. 

Cause: 
By default WCF allows a string content of size 8192 (8K) to pass through without any problems. If the size increases above this set limit WCF treats the incoming message as bad message & hence throws an exception. This level was set considering the security aspect of distributed system. 

If we have to pass more data we will have to manually override this default setting. 

Now here the trick. Everyone looked at the client side but there is a server part also :)

When a WCF client is created  automatically all the properties required to run that service is added by default by visual studio.


Changes made in WCF server applications web.config : 
Added a new HTTPbinding 
    <bindings> 
      <wsHttpBinding> 
        <binding name="newHTTPBinding" maxBufferPoolSize="2147483647"maxReceivedMessageSize="2147483647"> 
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" 
            maxArrayLength="16384" maxBytesPerRead="4096"maxNameTableCharCount="16384" /> 
        </binding> 
      </wsHttpBinding> 
    </bindings> 

set the value for maxBufferPoolSize, maxReceivedMessageSize,maxStringContentLength 

for the HTTPEndpoint i created a new binding configuration and mapped it 

<endpoint bindingConfiguration="newHTTPBinding" address="" binding="wsHttpBinding"contract="Abhi.AbhiBiz.IContractBusiness"> 

After this binding the WCF endpoint will take the new properties and allow more characters through the endpoint 

Stringcontent was given a higher value at client- presentation layer but the same configuration was not given in WCF service-endpoint at the business layer. 

Security issue : 

2147483647 is the magical figure but most of the applications doesn't need this much data.If not tested properly this configuration can lead to DOS attacks.
We need a realistic figure which matches the datatransfer between the endpoints. BufferSize also should be monitored to check the memory consumption
 


WCF de-serializes the object passed between endpoints, so if an object of 500 KB is passed the de-serilized data will be much higher. Further complexity can arise if an array of object is passed. So a realistic value for maxBufferPoolSize, maxReceivedMessageSize,maxStringContentLength should be given.

Posted: Aug 06 2009, 10:08 PM by cabhilash | with 4 comment(s) |
Filed under:
SQL Server Management Studio query template
Last day I was working on SQL server and I got this strange error
---------------------------
Microsoft SQL Server Management Studio
---------------------------
Cannot find template file for the new query ('C:\Program Files\Microsoft SQL
Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWorkbenchProjectItems\SQL\SQLFile.sql').
---------------------------
OK

At first I thought my SQl server installation was corrupt. I changed few settings, tried to repair but no use.

Solution:
Actually the solution was very simple. I just created an empty file SQLFile.sql in the designated folder and then it started working. Now this gave me a new idea. I could add my own template and every time I open SSMS I will get this template. Phew
Posted: Nov 23 2008, 09:36 PM by cabhilash | with no comments
Filed under:
.NET Framework V2.0 Obsolete API List

With the release of .Net framework 3.0 & 3.5 a lot API in .Net 2.0 has become obsolete.

I found two excellent links in MSDN which gives a comphrensive list of all the obsolete API's of .Net 2.0

  • The Obsoleted Types count is the number of obsoleted types found in the assembly/namespace.
  • The Obsoleted Members count is the number of obsoleted members not contained in an obsoleted type. If a type is obsolete, it's members are not counted in this value.

Obsolete List: By Assembly

Obsolete List: By Namespace

 

 

Posted: Jan 25 2008, 08:18 AM by cabhilash | with no comments |
Filed under:
More Posts