How to Bind Records Web Application Some Familiar Controls for Different Database Environment
In this Article, I am going to explain about how to bind the records using web application in Some Familiar Controls for Different Database Connectivity .I have implemented some controls for ASP.net web application .I have post source given below
1.How to Retrieve Records From Ms-Access
string mycon = @"PROVIDER=Microsoft.ACE.OLEDB.12;DATA SOURCE=" + Server.MapPath("\\DbPay.accdb");
DataTable Dt = new DataTable();
string Query = "SELECT * FROM Table1";
using (con = new OleDbConnection(mycon))
{
using (cmd = new OleDbCommand(Query, con))
{
OleDbDataAdapter Da = new OleDbDataAdapter(cmd);
Da.Fill(Dt);
}
}
Grd1.DataSource = Dt;
Grd1.DataBind();2.How to Retrieve Records From Sql Server
string Sqlcon2 = "Data Source=PC-NAME;Initial Catalog=Test;Integrated Security=True";
DataTable Dt = new DataTable();
string Query = "SELECT * FROM Employees";
using (Sqlcon1 = new SqlConnection(Sqlcon2))
{
using (Sqlcmd = new SqlCommand(Query, Sqlcon1))
{
SqlDataAdapter Da = new SqlDataAdapter(Sqlcmd);
Da.Fill(Dt);
}
}
Grd1.DataSource = Dt;
Grd1.DataBind();3.How to Retrieve Records From XML File
string Fpath = Server.MapPath("\\XmlFile1.xml");
DataSet ds = new DataSet();
ds.ReadXml(Fpath);
Grd1.DataSource = ds;
Grd1.DataBind();4.How to Retrieve Records From Text File
string value1 = File.ReadAllText(Server.MapPath("\\TextFile1.txt"));
List
lst.Add(value1);
Grd1.DataSource = lst;
Grd1.DataBind();3.How to Retrieve Records From Mysql
string Sqlcon2 = "Data Source=localhost;port=3306;Initial Catalog=Test;User Id=root;password=555";
using (MySqlConnection con = new MySqlConnection(Sqlcon2))
{
using (MySqlCommand cmd = new MySqlCommand("SELECT * FROM Employees"))
{
using (MySqlDataAdapter sda = new MySqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
Grd1.DataSource = dt;
Grd1.DataBind();
}
}
}
}3.How to Retrieve Records From List Object
List
_lstEmp.Add(new Emp() { ID = 1, BookCode = "150", BookName = "Dotnet" });
_lstEmp.Add(new Emp() { ID = 2, BookCode = "151", BookName = "Asp.net" });
Grd1.DataSource = _lstEmp;
Grd1.DataBind();3.How to Retrieve Records From Linq
var Query = from r in obj.UserInformations.AsEnumerable()
where (r.UserId == 1)
select r;
Grd1.DataSource = Query.CopyToDataTable();
Grd1.DataBind();3.How to Retrieve Records From Excel Sheet
DataTable dt;
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source="+ Server.MapPath("\\Test.xls") +"; Extended Properties=Excel 8.0;"
using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open()
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "SELECT * FROM $sheet1";
using(OleDbDataAdapter adapter =new OleDbDataAdapter())
{
adapter.SelectCommand = command;
adapter.Fill(dt);
}
Grd1.DataSource = dt;
Grd1.DataBind();
}
}