Thursday, November 7, 2013

How to import Data from Excel files to Dataset in .Net

Hi,

I want to share a path to import Excel files in .Net.

It is easy to export data to excel from our Grids in .Net(at least we know it very well) but importing data from Excel,it is troublesome task for us.

Naturally we do read through OLEDB, but is slow and troublesome.
To get rid off all these difficulties, we can access(/download) a Codeplex dll,it will read all the data(huge data also) within no time from  excel files into Dataset.

I am just mentioning link below

https://exceldatareader.codeplex.com/


go there and download that package and follow simple instructions(/simple code) to access the excel data,

that will works.

kudos to codeplex and people of Excel Data Reader


Thanks,
Hari



Tuesday, November 5, 2013

How to include FileUpload Control into Update Panel in Asp.Net

Hi,

We all encounter this problem in our applications. we might get into a situation like whole content of page should be in update panels and a FileUpload control is required in that page.

Here,we should sacrifice a update panel to work with FileUpload control.

To get rid off this mess, we can follow below solution.

Observe below code:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:FileUpload ID="FileUpload1" runat="server" />
         <asp:Button ID="btnUpload" runat="server" Text="Upload"
           OnClick = "btnUpload_Click" />               
    </ContentTemplate>
    <Triggers>
           <asp:PostBackTrigger ControlID = "btnUpload" />
    </Triggers>
</asp:UpdatePanel>

Here, FileUpload control and upload button are in a update panel

Everything is same except a Trigger.

This Trigger(PostBackTrigger) makes difference for us.Even though Fileupload control is in update panel,on clicking upload button post back occurs(so that our fileupload will work and rest of the controls will be in update panel as we desired.)

Thanks and Regards,
Hari

Monday, June 17, 2013

Stack Overflow exception Stack sizes in IIS

Hi,

We encountered a problem with decreased stack size of  IIS 6.0.

Problem:
As Stack size of IIS 6.0 is decreased to 256kb,we may get problem with executing recursive functionality up to some level or executing a method that processing heavy data.

We will get Stack OverFlow exception, when stack size of server(IIS) is not sufficient to serve the request.

Info: All the IIS versions below IIS 5.1, Stack size of a process is limited to 1MB.


Solution:
It is better to run a thread exclusively for the method which is causing exception like below.

protected void Button1_Click(Object Sender,EventArgs e)
{
Thread thread = new Thread(() => download(filename),4194304);
         thread .Start();
          thread .Join();
}



Explanation:
in Above thread,download is method name and filename is parameter.
4194304, 4 MB stack size is allotted to serve this request.



Thanks and Regards,
Hari


Friday, May 10, 2013

How to Register Controls in Web.config

In web.config, we can register User controls,custom controls and Third party controls.
just like below


<pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </controls>
    </pages>


Above lines are responsible to register asp.net controls in web.config,so that we are using those controls in all pages across the application without registering them in each individual Page.

if we developed a custom control MyControl

we have to register just like below

<pages>
  <controls>
   <add tagPrefix="Me" namespace="Me.Sample" assembly="Myassembly">
  </controls>
</pages>



Thursday, May 9, 2013

TryParse() in C#

in c#,we often convert data from one type to another.
Sometimes conversions may fail,to manage we will put conversion code block in try catch.
we can get rid off this by using TryParse().

Advantage:
No need to handle exception handling in conversions.

see below example


int quantity=0;
if (int.TryParse(txtQuantity.Text, out quantity) == false)
{
 quantity = 0;
}



Monday, April 16, 2012

DistinctBy in Linq

Hi Today i just created extension methods for TimeSpan object and Linq object.

we can add Extension methods  to existing methods of c#.

all extension methods are  made from Static  classes.
all extension methods use 'this' keyword.
there is nothing to pass to these methods.
these methods are invoked by the types for which we have generated.



namespace MyExtensions
{
  //Extension methods for TimeSpan object to get number of months and years
    public static class TimeSpanExtensions
    {
        public static int GetYears(this TimeSpan timespan)
        {
            return (int)((double)timespan.Days / 365.2425);
        }
        public static int GetMonths(this TimeSpan timespan)
        {
            return (int)((double)timespan.Days / 30.436875);
        }
    }

//Extension methods for Linq to make DistinctBy()
    public static class LinqExtensions
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>(
                            this IEnumerable<TSource> source, Func<TSource, TKey> selector)
        {
            var set = new HashSet<TKey>();
            return source.Where(element => set.Add(selector(element)));
        }
    }
}


you can create a class library with this class and use wherever you want.