Default.aspx Sayfası
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs Sayfası
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.MobileControls;
public partial class _Default : System.Web.UI.Page
{
DataTable table = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
//sayfa ilk defa yüklendiğinde çalıştır.
if (!Page.IsPostBack)
{
//GridView'i data ile dolduran metodumuz.
GridShippersLoad();
}
}
//Nortwind Database'inde Shippers tablosunun için proportisi
public class Shippers
{
private int _ShipperID;
public int ShipperID
{
get { return _ShipperID; }
set { _ShipperID = value; }
}
private string _CompanyName;
public string CompanyName
{
get { return _CompanyName; }
set { _CompanyName = value; }
}
private string _Phone;
public string Phone
{
get { return _Phone; }
set { _Phone = value; }
}
}
//integer tipindeki verilerin null kontrolünü yapıp
//convert işlemi yapan metodumuz
private int ConvertNullInteger(object field)
{
if (field == null)
return 0;
else return
Convert.ToInt32(field);
}
//string tipindeki verilerin null kontrolünü yapıp
//convert işlemi yapan metodumuz
private string ConvertNullString(object field)
{
if (field == null)
return "";
else return
Convert.ToString(field);
}
//GridView'i DataTable ile dolduran metodumuz
private void GridShippersLoad()
{
table = GetAllShippers();
GridView1.DataSource = table;
GridView1.DataBind();
}
//Veriyi DataTable olarak hazırlayan ve döndüren metodumuz
private DataTable GetAllShippers()
{
DataTable newdt = new DataTable();
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM Shippers";
cmd.Connection = conn;
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader())
{
//DataTable'in column'larının datatype lerini ayarla
newdt.Columns.Add("ShipperID", Type.GetType("System.Int32"));
newdt.Columns.Add("CompanyName", Type.GetType("System.String"));
newdt.Columns.Add("Phone", Type.GetType("System.String"));
while (dr.Read())
{
//veritabanından okunan verileri class'a aktaracak nesneyi tanımla
Shippers sp = new Shippers();
sp.ShipperID = ConvertNullInteger(dr.GetValue(0));
sp.CompanyName = ConvertNullString(dr.GetValue(1));
sp.Phone = ConvertNullString(dr.GetValue(2));
//propertiye aktaran verileri Add metodu ile datatable'ye ekle
newdt.Rows.Add(new object[] { sp.ShipperID,sp.CompanyName,sp.Phone });
}
//eğer datatable dolu ise metodu çağırana dolu olarak yolla
if (newdt != null && newdt.Rows.Count > 0)
return newdt;
else
return null;
}
}
}
}
//veritabanına bağlantıyı sağlayan metodumuz.
private string ConnectionString
{
get
{
return @"Data Source=localhost; Trusted_Connection=true;Database=Northwind";
}
}
}