André Carrilho's Blog

<I'm>.NET C# 1.x / 2.0</I'm>

This site

Sponsors

  • MaximumASP
  • Packet Sniffer
    Home Loans
  • conference calls glossary
  • Featured ASP.NET Web Hosting
    3 MONTHS FREE & FREE SETUP on ASP.NET 3.5/2.0 Web Hosting! Windows 2008 & 2003 Servers Available, MS SQL 2008/2005, .NET 3.5 SP1, Entity Framework, LINQ, Silverlight 2.0, 30 Day Money Back Guarantee – Click Here!
Restoring templates in VS 2005

Hi

The other day I ran into a peculiar problem, all my vs 2005 templates were gone!
What I did? Reinstall VS again but later I found a solution for the this. It seems that it works for some people but one can try it (it's better than installing all over agaisn, BELIEVE ME).
Close vs, open it's command prompt and type in:

devenv /installvstemplates

I didn't test it but seems to work ok. You can refer to

http://geekswithblogs.net/ehammersley/archive/2005/11/08/59451.aspx

for more info

Cheers

Starter Kits

Sup people

I have a friend that asked me to create an e-commerce site, a very simple one. I remembered that I came accross a couple of starter kits for ASP.NET that might help anyone that wants to create very simple but powerful sites.

Check this: http://www.asp.net/community/projects/

Cheers

C# using statement

Hi

Have my hands full in current projects that's why I've been away :-(. I'm currently workin in a dynamic DAL in one of my projects. I have to build classes that dynamically create/alter and query tables. It's an interesting project and soon I'll be posting here an article about it since I'm trying to use has much resources and .NET 2.0 can provide (from providers to Sql permorfance testing). Useful things...

One thing I came across today was the lack of object disposal. A collegue of mine was accessing a DB using SqlClient classes (SqlConnections, SqlCommand) without disposing the objects. Some of these objects use unmanaged resources that must be freed from momery and not using dispose on them causes meory leaks.

When using SqlConnection or SqlCommand you could use:

DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(this._connectionString))
{
   string sqlComm = "SET DATEFORMAT DMY ";
   sqlComm += "SELECT * FROM someTable";
   conn.Open();
   using (SqlCommand comm = new SqlCommand(sqlComm, conn))
   {
      comm.CommandType = CommandType.Text;
      SqlDataAdapter da = new SqlDataAdapter(comm);
      da.Fill(ds, tableName);
   }
}

or use try/catch/finally

SqlConnection conn = null;
SqlCommand comm = null;
SqlDataAdapter da = null;
try
{
   conn = new SqlConnection(connectionString);
   sqlComm += "SELECT * FROM someTable";
   comm = new SqlCommand(sqlComm, conn);
   comm.CommandType = CommandType.Text;
   da = new SqlDataAdapter(comm);
   DataSet ds = new DataSet();
   da.Fill(ds, tableName);
}
catch (Exception ex)
{
   // manage exception
}
finally
{
   if (conn != null)
      conn.Dispose();
   if (comm != null)
      comm.Dispose();
   if (da != null)
      da.Dispose();
}

Remember that freeing memory is VERY important so... :-)

Cheers

Posted: Dec 04 2007, 01:04 AM by andrecarrilho | with no comments
Filed under: , ,
Implement a Collection of your own custom object in .Net C#

Hi

It was something I had to do for a class object of my own so I decided to post it here. The ideia is to create a Collection of a custom object. It's quite simple actually. Use the CollectionBase class that is the mother of all collection classes. Here's a snippet of a code that you can use...

public class MyCollecyions : CollectionBase
{
    public int Add(MyObject obj)
    {
        return List.Add(obj);
    }

    public void Insert(int index, MyObject obj)
    {
        List.Insert(index, obj);
    }

    public void Remove(MyObject obj)
    {
        List.Remove(obj);
    }

    public bool Contains(MyObject obj)
    {
        return List.Contains(obj);
    }

 
   public int IndexOf(MyObject obj)
    {
        return List.IndexOf(obj);
    }

    public void CopyTo(MyObject[] objs, int index)
    {
        List.CopyTo(objs, index);
    }

 
   public MyObject this[int index]
    {
        get { return (MyObject)List[index]; }
        set { List[index] = value; }
    }
}

As the code is self explanatory I won'r delve into what each part does but if any question arises please don't hesitate to ask. This code is posted AS-IS.

Cheers

Import data to Sql Server using SqlBulkCopy (performance boost!)

Recently I came across a situation that I had to upload an excel spreadsheet to a SQL Server DB (both with same schemas). No problem, just use SQL INSERT statements and I'm done right?! Not so fast, the INSERT statement is cool but in case of thousands and even millions of data some performance issues rise up! So, the first approach sucks... And then came the SqlBulkCopy, a major improvement in data import to Sql Server DB's.
It uses the same functionality the Bulk Copy Program utility provides. It allows you to copy large amounts of data from a variety or sources to your Sql Server Table without compromising much of the performance. One article on eggheadcafe by Peter Broomberg shows how long does it take to copy 43.000 records from a csv file to Sql Server (if I'm not mistaken, about 1250 milliseconds).

 

So I decided to try it myself and boom... Extreme successful result!!!

Here is a code snippet of my code:

DataTable dt = ReadFromExcel();
string connectionString = @"Server=QUIZER\SQLEXPRESS;Initial Catalog=SqlBulkCopyTest;Uid=submission;Pwd=submission";
using (SqlConnection conn = new SqlConnection(connectionString))
{
    using (SqlBulkCopy copy = new SqlBulkCopy(conn))
    {
        conn.Open();
        copy.DestinationTableName = "TestTable";
        try
        {
            copy.WriteToServer(dt);
        }
        catch (Exception ex)
        {
            logTime.WriteLine("Error occured : " + ex.Message);
        }
    }
}

To import 65535 excel rows it took about 4 seconds (including exporting excel data to a DataTable). I've tried importing using normal insert statements and it tool about 7 minutes and 30 seconds.
No match :).

Cheers

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.