Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, October 12, 2009

Simple way to check time consumed by a piece of code using stopwatch class

TimeSpan ts;
string elapsedTime;
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
//DO SOME LENGTHY OPERATION
ts = stopWatch.Elapsed;
elapsedTime =
                    String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                    ts.Hours, ts.Minutes, ts.Seconds,
                    ts.Milliseconds / 10);
 stopWatch.Reset();
 stopWatch.Stop();

Thursday, July 23, 2009

Executing a stored procedure with output parameter doesnt return any value.

When you use a DataReader object to read data from SqlServer, the parameter passed in as output parameter doesnot get assigned even after excuting the reader.

Solution:- Close the DataReader and your value will get returned.

Reason:- The output parameter is returned at the end of the data stream when you use a DataRader.

Reference:- http://support.microsoft.com/kb/308621

Friday, February 27, 2009

LINQ ERROR

I was working with a LINQ sample and stumbled upon the following error.
"The non-generic type 'System.Collections.IEnumerable' cannot be used with type arguments"

XML file I was using :-


Code used :-


//The application is a ASP.net page
XDocument xDoc = XDocument.Load(HttpContext.Current.Server.MapPath("Persons.xml"));

IEnumerable queryNew = from e in xDoc.Descendants("Person")

select new Person{
FirstName = e.Element("FirstName").Value,
LastName = e.Element("LastName").Value,
Salary = Convert.ToInt32(e.Element("Salary").Value)
};


Solution :- IEnumerable is present in both System.Collections.Generic and System.Collections name spaces. The query returns a generic collection. So in this case We should use System.Collections.Generic. So when I provided a "using System.Collections.Generic;" on top of the page, it did the trick for me.