Thursday, February 25, 2010

Efficient best Syntax to Open a SqlConnection in Asp.Net 2.0 3.5

Please visit my new Web Site

To describe the best way to open sql server connection in here i am choosing to bind the gridview because in most of the cases the major task is to bind GridView data. You can make more generous method to collect sql server data but here my intension is to show you how you can open sql server connection efficiently.

Please have a look at the code sample:
protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            DataTable dt;
            String SQL= "SELECT B.Name [Brand Name],C.Name [Category Name], " +
                    "P.Name [Product Name] FROM " +
                    "Brand B, Category C, Product P " +
                    "WHERE B.ID=P.BrandID AND C.ID=P.CategoryID Order BY 1,2,3";

            string sConstr = ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(sConstr))
                using (SqlCommand comm = new SqlCommand(SQL, conn))
                    using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        dt = new DataTable("tbl");
            GridView1.DataSource = dt;


The best practice is to wrap up all code under using statement. If you look at the code you will find that i have wrapped up all code under connection object as well as sql command. Keep in mind that when corresponding "using" statement reached at the end then automatically clear all variables immediately within the scope. You do not need to dispose those manually. Such as here i don't close the connection, sqlcommand. For ease understanding here i am using datatable. You can use any component whichever you like. But keep in mind to wrap up the connection object within "Using" statement.
This is my message to you.


Anonymous said...

Thanks for the great info.

Just one thing, what about if I want to wrap the on a cftry as well as the SqlDataReader on my case. Where should i mount it. Or do i still need it? ofcourse i do

Or can i just wrap the whole thing starting from using (SqlConnection conn = new SqlConnection(sConstr))

Your recommendation, what's the best way.

Shawpnendu said...

You can use try catch out side "Using" statement but no need to use within the "Using" since "Using" automatically rolledback for transactions if you follow the below way:

using (SqlConnection sqlCn = new SqlConnection())
using (SqlTransaction yourTrans = sqlCn.BeginTransaction())

Yusuf Ndola said...

i always under estimate the power of using statement now i cant do withoout it. thank alot

Want to say something?

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash