Using block defines a scope, outside of which an object or objects will be disposed.
The using statement takes an object parameter, this object is destroyed when the statement finish's, or rather disposed of (IDisposable). The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources.
A "using" statement can be exited either when the end of the "using" statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement.
In the below example font1 object is disposed of after the using statement has been executed.
using (Font font1 = new Font("Arial", 10.0f)){}
Multiple objects can be used in with a "using" statement, but they must be declared inside the "using" statement, like this:
using (Font font3 = new Font("Arial", 10.0f),
font4 = new Font("Arial", 10.0f)){
// Use font3 and font4.
}
The problem is that SqlConnection and SqlCommand implement IDisposable, which means they could have unmanaged resources to cleanup and it is our job, the developers, to make sure Dispose() gets called on these classes after we are finished with them. And, because an exception could be raised if the database is unavailable ( a very real possibility on WebHost4Life :) ), we need to make sure Dispose() gets called even in the case of an exception.
Personally, I like the “using” keyword in C#. Internally, this bad boy generates a try / finally around the object being allocated and calls Dispose() for you. It saves you the hassle of manually creating the try / finally block and calling Dispose().
The new code would looking something like this:
using (SqlConnection cn = new SqlConnection(connectionString))
{
using (SqlCommand cm = new SqlCommand(commandString, cn))
{
cn.Open();
cm.ExecuteNonQuery();
}
}
This is essentially equivalent to the following, although my guess is that C# will internally generate two try / finally blocks (one for the SqlConnection and one for the SqlCommand), but you get the idea:
SqlConnection cn = null;
SqlCommand cm = null;
try
{
cn = new SqlConnection(connectionString);
cm = new SqlCommand(commandString, cn);
cn.Open();
cm.ExecuteNonQuery();
}
finally
{
if (null != cm);
cm.Dispose();
if (null != cn)
cn.Dispose();
}
Tuesday, March 15, 2011
Friday, March 4, 2011
Cache object use for dataBase Access(Application Level Caching/Programmatic and Data Caching)
//First Page where we store dataset values into caching and use that value another page...
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select Id,Name from emp", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
Cache.Insert("user", ds);
/////second page access caching value..........
if (Cache["user"] != "")
{
ds = (DataSet)Cache["user"];
GridView1.DataSource = ds;
GridView1.DataBind();
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand("select Id,Name from emp", con);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
Cache.Insert("user", ds);
/////second page access caching value..........
if (Cache["user"] != "")
{
ds = (DataSet)Cache["user"];
GridView1.DataSource = ds;
GridView1.DataBind();
}
Caching in ASP.NET
This is the link where we can understand easily for Caching.......
http://www.c-sharpcorner.com/uploadfile/vishnuprasad2005/implementingcachinginasp.net11302005072210am/implementingcachinginasp.net.aspx
http://www.c-sharpcorner.com/uploadfile/vishnuprasad2005/implementingcachinginasp.net11302005072210am/implementingcachinginasp.net.aspx
Labels:
Caching in Asp.Net
Thursday, March 3, 2011
Ajax tutorial Link
http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_suggest
Labels:
Ajax tutorial Link
Tuesday, March 1, 2011
LINQ Bind Gridview and Insert Data
SqlConnection con=new SqlConnection (ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataClassesDataContext db = new DataClassesDataContext(con);
//linq Table emps
var query = from emp1 in db.emps select new { emp1.id, emp1.Name };
GridView1.DataSource = query;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext(con);
db.Connection.Open();
//emp Name of the Table
emp empob = new emp();
empob.Name = TextBox1.Text.Trim();
db.emps.InsertOnSubmit(empob);
db.SubmitChanges();
db.Connection.Close();
TextBox1.Text = "";
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataClassesDataContext db = new DataClassesDataContext(con);
//linq Table emps
var query = from emp1 in db.emps select new { emp1.id, emp1.Name };
GridView1.DataSource = query;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext(con);
db.Connection.Open();
//emp Name of the Table
emp empob = new emp();
empob.Name = TextBox1.Text.Trim();
db.emps.InsertOnSubmit(empob);
db.SubmitChanges();
db.Connection.Close();
TextBox1.Text = "";
}
Monday, February 28, 2011
Read Data from Excel and Export to Sql server
SqlBulkCopy Use for Insert Bulk Data into sql server.You have to create one table inside the sql server and create only structure of the table which we have in Excel .and Use the code Below.
//File upload pick the file name and Details is a sheet name Inside the .Xls file
DataTable dt = new DataTable();
protected void Button2_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileUpload1.PostedFile.FileName +";Extended Properties=Excel 8.0; ");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Details$] ", con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
InsertBulkDATA();
}
private void InsertBulkDATA()
{
SqlBulkCopy sbc = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
sbc.DestinationTableName = "tblData";
sbc.WriteToServer(dt);
}
//File upload pick the file name and Details is a sheet name Inside the .Xls file
DataTable dt = new DataTable();
protected void Button2_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+FileUpload1.PostedFile.FileName +";Extended Properties=Excel 8.0; ");
OleDbDataAdapter da = new OleDbDataAdapter("select * from [Details$] ", con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
InsertBulkDATA();
}
private void InsertBulkDATA()
{
SqlBulkCopy sbc = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["kumarConnectionString"].ConnectionString);
sbc.DestinationTableName = "tblData";
sbc.WriteToServer(dt);
}
Saturday, February 26, 2011
Hosting WCF Services in IIS
This is the link which will describe how to Host wcf services in IIS
http://www.youtube.com/watch?v=mX8quq7MoeI&feature=related
http://www.youtube.com/watch?v=mX8quq7MoeI&feature=related
Labels:
Hosting WCF Services in IIS
Subscribe to:
Posts (Atom)