LINQ for dummies – an overview
December 26, 2009 at 12:35 pm | Posted in .Net, ASP.net, C#, VB.NET | Leave a commentTags: ASP.NET, C#, LINQ, VB.NET
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
- LINQ to Objects,
- LINQ to SQL,
- 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 …
Difference between a constant and readonly variables/fields : (constant vs readonly)
December 26, 2009 at 12:34 pm | Posted in .Net, ASP.net, C#, VB.NET | 1 CommentTags: .Net, C#. VB.Net
- 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?
- 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 variables are created usually in the constructor of class. there fore it will have values before the constructor of the class exists
{
public readonly string _strReadonly;
public void MyClass(string strVal)
{ _strReadonly = strVal;
}
}
Return top N rows from datatable
December 26, 2009 at 12:32 pm | Posted in .Net, ASP.net, C#, VB.NET | Leave a commentTags: .Net, ADO.NET, C#, DATATABLE, LINQ, VB.NET
{
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;
}
{
Viewstate error : 12031
December 26, 2009 at 12:28 pm | Posted in .Net, ASP.net, C# | Leave a commentTags: .Net, AJAX, ASP.NET, C#, EXCEPTION, VIEWSTATE
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.
Add Web service reference- Components required to enumerate web references not installed
December 26, 2009 at 12:26 pm | Posted in .Net, C#, VB.NET | Leave a commentTags: .Net, ASP.NET, VISUAL STUDIO, WCF, WEB SERVICE
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.(0×80004002)
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 …
- Close all open Visual Studio instances
- Open the Command prompt available with Visual Studio and type devenv /resetskippkgs (You can directly type this to Run command or normal command prompt)
- Add reference to the Web service/WCF service
Now I am back in business
Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.