A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Posts Tagged ‘.NET’

DataSet Vs Custom Entities

Posted by kiranpatils on June 1, 2012

Challenge:

Dear Readers, Apologize for not sharing anything with you since so long. As was bit busy with projects. (But as I told in my earlier posts as well, It’s good to be busy. Because during your busy time you will learn a lot, and needless to say once you get time you will have a lot to share!)

This week, We were discussing what to use for our new project for passing data from one layer to another layer. DataSet Or Entities.

I’m big fan of Entities and Generics. But thought to do some research on it, before concluding anything. And my research revealed that I’m (I know you as well) not only one who is searching on this topic. Lot of people shared their views on web. But I filtered out few links, which I liked most (along-with the Excerpt which is important to read from that link)  and thought to share with you! So, here you go

http://forums.asp.net/t/1129497.aspx/1

“The problem with DataSets is that they tend to be pretty memory hungry. They also get a bit difficult to manage as your application gets larger. When you need more complex solutions, the initial effort that you put into getting custom classes up and running can pay off with shorter development cycles if you do it right. “

Going with custom classes is certainly better in many ways.

i) easier to add logic

ii) business validation is easier. This is tougher when using DataSet

iii) if you are using DataSet, even when the number of rows returned are small, the whole dataset has to be constructed which is quite an overhead. Custom classes are more light-weight

http://codebetter.com/jefferypalermo/2005/05/20/displaying-aggregates-dataset-vs-domain-object-performance-level-300/

“The page that used an array of custom domain objects to present a read-only display was 3.7% FASTER than the same functionality using a DataSet. “

http://blogs.msdn.com/b/reinouth/archive/2006/03/08/546510.aspx

“Now, amazingly, this has sparked somewhat of a religious war between various parties within the project. Just to be clear – I prefer custom objects – partly for their simplicity and readability”

“I agree with you on custom objects, you will have more control and better performance.”

http://blogs.msdn.com/b/irenak/archive/2006/02/27/539832.aspx

  • Memory consumption using Dataset = 290,436 (3.5 times larger than using Products class)
  • Memory consumption using array of objects = 140,028 (1.7 times larger than using Products class)
  • Memory consumption using Product class = 82,656 (BEST)

So, no matter how you turn it, strongly typed custom classes are your best bet in terms of memory consumption!

http://weblogs.asp.net/paolopia/articles/dsvscustomentities.aspx

2) BIZ and DAL are written by people different from the presentation layer group, so they have different knowledge about the data structures

http://www.4guysfromrolla.com/articles/051805-1.aspx

While DataSets are an easy way to return data, I think they are less than ideal for a couple of reasons. First, they add a lot of bloat to the returned XML payload since DataSets return not only the data they contain but also the data’s schema.

In my original article one of my main thrusts for not using DataSets was the performance disparity between DataSets and DataReaders. As I cited from A Speed Freak’s Guide to Retrieving Data in ADO.NET, when bringing in several hundred or thousands of records, a DataSet can take several seconds to be populated, where as a DataReader still boasts sub-second response times. Of course, these comparisons against several hundred to several thousand records are moot if you are working with a much smaller set of data, say just 10 to 50 records in total. For these smaller sized result sets, the DataSet will only cost you less than a second (although the DataReader still is, percentage-wise, much more efficient).

http://swap.wordpress.com/2007/07/10/dataset-vs-custom-entity/

Benefit with Custom Entity Classes

- Take advantage of OO techniques such as inheritance and encapsulation
- You can add custom behavior
- Object-Relational Mapping
- Mapping Custom Collections
- Managing Relationships between two entities in Object oriented way

http://stackoverflow.com/questions/1679064/should-i-use-entity-framework-dataset-or-custom-classes

I would agree with Marc G. 100% – DataSets suck, especially in a WCF scenario (they add a lot of overhead for handling in-memory data manipulation) – don’t use those. They’re okay for beginners and two-tier desktop apps on a small-scale maybe – but I wouldn’t use them in a serious, professional app.

http://www.rosscode.com/blog/index.php?title=datasets_vs_custom_objects&more=1&c=1&tb=1&pb=1

Solution:

So, in summary, as per my understanding, performance, maintainability, and serialization point of view entities looks more promising than DataSet.

Few of my guidelines:

You should use DataSet when:

1. Project is small.
2. Only Single person is going to work on all layers.
3. Data size is small.
4. Application is not going to get changed in future. (Mainly Database structure).
5. You are not worried about Maintainability.

You should use Custom Entities when:

1. Project is big.
2. Different people will be working on different layers.
3. Data size is big.
4. Application is going to get changed in future. (Mainly Database structure).
5. You are worried about Maintainability.
6. If you love to follow OOPs concepts.

These all are my views, and they might look biased toward Entities. Because as i told earlier, I’m big fan of custom entities. But if you would like to use DataSet and have good reasons to use it. Don’t worry go for it!

Happy Coding! :-)

Posted in .NET, ASP.NET, GoodToKnow | Tagged: , , | Leave a Comment »

Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occured because all pooled connections were in use and max pool size was reached.

Posted by kiranpatils on June 21, 2011

Challenge:

Have you seen following error?

timeout-expired-the-timeout-period-elapsed-prior-to-obtaining-a-connection-from-the-pool-this-may-have-occured-because-all-pooled-connections-were-in-use-and-max-pool-size-was-reached

Then this post is to solve it!

Solution:

As per the error your code has not closed the opened SqlConnection properly. For example

SqlConnection conn = new SqlConnection(

myConnectionString);
conn.Open();
doSomething(); /*  If some error occurs here — Next line will not get called and it will leave connection open */
conn.Close();

Solution:

1.
SqlConnection conn = new SqlConnection(myConnectionString);
try
{
conn.Open();
doSomething(conn);
}
finally
{
conn.Close();    // This line will get called in any case — success/failure
}

So, open your solution in Visual Studio and search in entire solution for all open connections and for all the code implement above suggested solution.

Just a note : If you have written Data Access layer code in code behind file then you are in trouble here. You have to do changes at N number of places. If you would have created separate Data Access layer (Technically Class Library) and Method to do DB operation then your task would have been easy enough!
2) You can raise the connection pool size in the connection string.  For example, you can add “Max Pool Size=100″ to your connection string to increase the pool size to 100.

Implement above solutions. You should not see any issues any more.

Good to read :
http://blogs.msdn.com/b/tolong/archive/2006/11/21/max-pool-size-was-reached.aspx
Happy DB Access! :-)

Posted in .NET, ASP.NET | Tagged: , , | 2 Comments »

Converting Hexadecimal Value to Int or VB6 Hexadecimal value to C# Int

Posted by kiranpatils on January 16, 2008

Somewhat busy from last few days and can’t able to write something new here…

Today i have faced new problem i want to convert a hexadecimal value [which i have stored in string and got from VB 6.0] to Int..But i have faced so many problems as under:

1. “Could not find any recognizable digits”.

stack trace:

——————————————————————————————————–

System.FormatException was unhandled
Message=”Could not find any recognizable digits.”
Source=”mscorlib”
StackTrace:
at System.ParseNumbers.StringToInt(String s, Int32 radix, Int32 flags, Int32* currPos)
at System.Convert.ToUInt32(String value, Int32 fromBase)
at HextoInt.Program.HexToInt(String hexString) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 27
at HextoInt.Program.Main(String[] args) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

——————————————————————————————————–

Root Cause:

string Hexvalue = “&H69″; //My Hex value is creating problem Here.

Solution

I have copied this string ["&H69"] from VB 6 Output. But when you need to pass a hex value in C#. you have to keep following thing in Mind.

1. It Must starts with 0[zero]x means for my problem solution is here:

string Hexvalue = “&H69″; //ERROR PRONE CODE

string Hexvalue = “0×69″; //NO ERROR HERE

i got help from: http://www.thescripts.com/forum/thread261083.html

2. “Input string was not in a correct format.”

stack trace:

——————————————————————————————————–

System.FormatException was unhandled
Message=”Input string was not in a correct format.”
Source=”mscorlib”
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s, NumberStyles style)
at HextoInt.Program.HexToInt(String hexString) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 26
at HextoInt.Program.Main(String[] args) in F:\Consensus\Appns My\Testharness\HextoInt\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
——————————————————————————————————–

Root Cause:

private static int HexToInt(string hexString)
{
return int.Parse(hexString, NumberStyles.HexNumber); // ERROR PRONE CODE
}

Solution

I have created one function for converting Hexa to Int. But it is throwing error.

Frankly speaking, I don’t know why it is not working but i tried other one and now its working..so i am providing you a working one here.

private static int HexToInt(string hexString)
{
int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}

//The above dude is working fine.

3. How to Convert Hexadecimal to Int???

I have tried so many codes,my applications and blogs and all that things which i usually do for Killing the problem. I have found this thing from so many spaces and i am putting it here for centralization purpose. and making your task easy one.

for converting Hexadecimal to Int or Vice versa use functions:

—————————————————————————————————–

1. for Hexadecimal to integer conversion

private static int HexToInt(string hexString)
{
int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}

2. for integer to Hexadecimal conversion

public static string IntToHex(int number)
{
return String.Format(“{0:x}”, number);
}

and ya don’t forget to Import System.Globalization namespace

—————————————————————————————————–

I Got Help from : http://mark.michaelis.net/Blog/HexadecimalConversionsInCNET.aspx

[OPTIONAL] And at last i am pasting my TestHarness Console application here:


using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
namespace HextoInt
{
class Program
{

static void Main(string[] args)
{

string Hexvalue = “0×69″;//”0×100000″;//”&H69″;
//Console.Write(“{0:X}”, Hexvalue);//To Print any/String value in Hexa decimal
Console.WriteLine(“HEX [which is in string] TO INT:–”+HexToInt(Hexvalue));
Console.WriteLine(“INT TO Hex[Output will be in string]:–” + IntToHex(HexToInt(Hexvalue)));

}
private static int HexToInt(string hexString)
{

int iHex1 = (int)System.Convert.ToUInt32(hexString, 16);
return iHex1;
}
public static string IntToHex(int number)
{
return String.Format(“{0:x}”, number);
}

}
}


Have a Happy Conversion!!!

Posted in .NET | Tagged: , | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 102 other followers