Microsoft Enterprises Library provide API where we dont need to open and close connection and they have sme predefind method .You dont need to create your own method .it has pedefined method for all that things for insert update delete retrieve and some more specific operation...........
You have to download the Entterprise lIbrary first and Add some Dll through AddReferences from a bin folder of Enterprises Library add
using Microsoft.Practices.EnterpriseLibrary.Data.dll
using Microsoft.Practices.EnterpriseLibrary.Common..dll
and Add the classes in code behind
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
//Retrieve and Insert value Using Entrprises library
protected void Page_Load(object sender, EventArgs e)
{
Database db = DatabaseFactory.CreateDatabase();
string sqlcommand = "select * from emp";
System.Data.Common.DbCommand dbCommand = db.GetSqlStringCommand(sqlcommand);
using (IDataReader reader = db.ExecuteReader(
dbCommand))
{
Gridview1.DataSource = reader;
Gridview1.DataBind();
}
}
public void save(string name)
{
DatabaseFactory.CreateDatabase().ExecuteNonQuery("inserts12",name);
}
protected void Button1_Click(object sender, EventArgs e)
{
save("subhash");
}
/////////web.config file
inside the ConfigSection
section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data" />
after closing configsection You have to add
/configSections>
dataConfiguration defaultDatabase="Master1"/>
//dataconfiguration same name as the connection string because DatabaseFactory.CreateDatabase(); using for connection
//connection string
add name="Master1" providerName="System.Data.SqlClient" connectionString="Data Source=subhash-9f7eec2;user id=;password=;database=Master;integrated security=sspi;"/>
Tuesday, March 22, 2011
Friday, March 18, 2011
Generic
using System.Collections.Generic;
In an Arraylist, As Each item is stored as an object, During Sorting and traversing the list, it must be typed at runtime, and then compared for sorting/searching wheareas Generic list has all items typed compile time, which saves a lot of workload during sorting and searching.
The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required.
Type safety : it gives error at compile time if a data type mismatch is found.
Code reusablity : Same code is reused for various data tyoe like int,string etc.
public class User
{
protected string name;
protected int age;
protected double salary;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
public double Salary { get { return salary; } set { salary = value; } }
static void Main(string[] args)
{
List users = new List();
for (int x = 0; x < 5; x++)
{
User user = new User();
user.Name ="Name : "+ "subhash" + x;
user.Age = x+20;
user.salary =x + 12500;
users.Add(user);
}
foreach (User user in users)
{
System.Console.WriteLine(System.String.Format("{0}:{1}:{2}", user.Name, user.Age,user.salary));
}
Console.ReadLine();
}
}
In an Arraylist, As Each item is stored as an object, During Sorting and traversing the list, it must be typed at runtime, and then compared for sorting/searching wheareas Generic list has all items typed compile time, which saves a lot of workload during sorting and searching.
The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required.
Type safety : it gives error at compile time if a data type mismatch is found.
Code reusablity : Same code is reused for various data tyoe like int,string etc.
public class User
{
protected string name;
protected int age;
protected double salary;
public string Name { get { return name; } set { name = value; } }
public int Age { get { return age; } set { age = value; } }
public double Salary { get { return salary; } set { salary = value; } }
static void Main(string[] args)
{
List
for (int x = 0; x < 5; x++)
{
User user = new User();
user.Name ="Name : "+ "subhash" + x;
user.Age = x+20;
user.salary =x + 12500;
users.Add(user);
}
foreach (User user in users)
{
System.Console.WriteLine(System.String.Format("{0}:{1}:{2}", user.Name, user.Age,user.salary));
}
Console.ReadLine();
}
}
Tuesday, March 15, 2011
Using Statement in C#
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();
}
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();
}
Labels:
Using Statement in C#
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 = "";
}
Subscribe to:
Posts (Atom)