............................source..................
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 100%">
<tr>
<td>
<span class="title"> Centres Form</span>
<hr />
<asp:Panel ID="Panel2" runat="server">
<table bgcolor="#31CFF0" border="1" bordercolor="#E8E8E8" cellpadding="5"
cellspacing="0" class="texts" width="100%">
<tr>
<td style="width: 148px; height: 30px;">
<b>State</b></td>
<td style="height: 30px">
<asp:TextBox ID="txtstate" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
<asp:Button ID="btnstate" runat="server" class="resetbtn" TabIndex="32"
Text="Add State" Width="96px" onclick="btnstate_Click" />
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Choose State</b></td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
class="textbox" Height="23px" TabIndex="7" Width="175px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>City's</b></td>
<td>
<asp:TextBox ID="txt_addCity" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
<asp:Button ID="btn_AddCity" runat="server" class="resetbtn" TabIndex="32"
Text="Add City" Width="96px" onclick="btn_AddCity_Click" />
</td>
</tr>
<tr>
<td style="width: 148px">
</td>
<td>
</td>
</tr>
<caption>
<asp:HiddenField ID="HiddenField3" runat="server" />
</caption>
</table>
</asp:Panel>
<br />
<asp:Panel ID="Panel1" runat="server">
<table bgcolor="#31CFF0" border="1" bordercolor="#E8E8E8" cellpadding="5"
cellspacing="0" class="texts" width="100%">
<tr>
<td style="width: 148px" class"WHITETXT">
<b>State </b> </td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" class="textbox"
Width="175px" Height="23px"
TabIndex="7" AutoPostBack="True"
onselectedindexchanged="DropDownList3_SelectedIndexChanged" >
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>City</b></td>
<td>
<asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="True"
class="textbox" Height="23px" TabIndex="7" Width="175px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Area</b></td>
<td>
<asp:TextBox ID="txtArea" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Address</b></td>
<td>
<asp:TextBox ID="txtAddress" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Email ID's</b></td>
<td>
<asp:TextBox ID="txtEmail" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
</td>
<td>
</td>
</tr>
<caption>
<asp:HiddenField ID="HiddenField1" runat="server" />
</caption>
</table>
</asp:Panel>
<div >
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Button6" runat="server" CssClass="submitbtn"
onclick="Button1_Click" TabIndex="31" Text="Add Centres" Width="104px" />
<asp:Button ID="Button2" runat="server" class="resetbtn" Text="Update"
Width="96px" TabIndex="32" onclick="Button2_Click"></asp:Button>
<asp:Button ID="Button3" runat="server" class="resetbtn" Text="Delete"
Width="96px" TabIndex="32" ></asp:Button>
</div>
</td>
<td>
</td>
</tr>
<tr>
<td>
<center>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BorderStyle="None" DataKeyNames="areaid,area"
BorderWidth="1px" CellPadding="3" Font-Bold="True"
PagerSettings-Mode="Numeric" RowStyle-CssClass="Descriptiongv2"
ShowFooter="True" Width="100%" BackColor="White" BorderColor="#CCCCCC"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting"
onrowupdating="GridView1_RowUpdating" onrowcommand="GridView1_RowCommand"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" AutoGenerateEditButton="True"
>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle CssClass="Descriptiongv2" BackColor="White" ForeColor="#330099"></RowStyle>
<Columns>
<asp:TemplateField HeaderText="State " ItemStyle-Height="30px">
<ItemTemplate>
<center>
<asp:LinkButton ID="lnkitem" runat="server" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="cmdBind" Text='<%# Eval("state") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="City" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbcity" runat="server" Text='<%# Eval("city") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Area" ItemStyle-Height="30px">
<ItemTemplate>
<center><asp:Label ID="lbarea" runat="server" Text='<%# Eval("area") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbaddress" runat="server" Text='<%# Eval("address") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbemail" runat="server" Text='<%# Eval("email")%>' ></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle Font-Bold="True" ForeColor="White" BackColor="#669999" />
<PagerStyle HorizontalAlign="Left" BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
Font-Size="Smaller" />
</asp:GridView>
<asp:HiddenField ID="HiddenField2" runat="server" />
</center>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</asp:Content>
.......................code behind.........................
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlCommand cmd;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindState();
bindState1();
bindCentres();
bindCity();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
InsertArea();
bindCentres();
Literal1.Text="Successfully Inserted";
}
protected int getCityId(string id)
{
int id1 = 0;
cmd = new SqlCommand("select cityid from city where stateid='" + id + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
id1 = Convert.ToInt32(dr[0]);
}
con.Close();
return id1;
}
protected void InsertArea()
{
string dt = DateTime.Now.ToString();
int sid = stateId(DropDownList3.SelectedValue);
int cid = getCityId(DropDownList3.SelectedValue);
string str = "insert into area values ("+cid+","+sid+",'" + txtArea.Text + "','"+txtAddress.Text+"','" + txtEmail.Text + "','"+dt+"')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
protected void bindCentres()
{
con.Open();
string str = "select a.areaid, s.state,c.city,a.area,a.address,a.email,s.date from state as s inner join city as c on s.stateid=c.stateid inner join area as a on a.cityid=c.cityid order by s.date desc";
da = new SqlDataAdapter(str, con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected int stateId(string id)
{
int id1 = 0;
cmd = new SqlCommand("select stateId from state where stateid='" + id + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
id1 = Convert.ToInt32(dr[0]);
}
con.Close();
return id1;
}
protected void insertstate()
{
try
{
string date = DateTime.Now.ToString();
string str = "insert into state(state,date) values ('" + txtstate.Text + "','"+date+"')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
catch(Exception ex)
{
}
}
protected void insertCity()
{
try
{
string datectiy = DateTime.Now.ToString();
int sid = stateId(ddlState.SelectedValue);
string str = "insert into city values (" + sid + ",'" + txt_addCity.Text + "','" + datectiy + "')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txt_addCity.Text = "";
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
catch(Exception ex)
{
}
}
public void bindState()
{
try
{
da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
da.Fill(dt);
ddlState.DataSource = dt;
ddlState.DataTextField = "state";
ddlState.DataValueField = "stateid";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select State--", "0"));
}
catch(Exception ex)
{
}
}
public void bindState1()
{
da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "state";
DropDownList3.DataValueField = "stateId";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, new ListItem("--Select State--", "0"));
}
public void bindCity()
{
da = new SqlDataAdapter("select * from city where stateid='" + DropDownList3.SelectedValue + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
ddlcity.DataSource = dt;
ddlcity.DataTextField = "city";
ddlcity.DataValueField = "cityid";
ddlcity.DataBind();
ddlcity.Items.Insert(0, new ListItem("--Select City--", "0"));
}
protected void btnstate_Click(object sender, EventArgs e)
{
insertstate();
bindState();
Literal1.Text = "Successfully Inserted";
}
protected void btn_AddCity_Click(object sender, EventArgs e)
{
insertCity();
bindCity();
Literal1.Text = "Successfully Inserted";
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
bindCity();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["areaid"].ToString());
string username = GridView1.DataKeys[e.RowIndex].Values["area"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from area where areaid=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
bindCentres();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string area = GridView1.DataKeys[e.RowIndex].Values["area"].ToString();
TextBox txtarea = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtarea");
TextBox txtaddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress");
TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemail");
con.Open();
SqlCommand cmd = new SqlCommand("update area set area='" + txtarea.Text + "',address='" + txtaddress.Text + "',email='"+txtemail.Text+"' where areaid=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bindCentres();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindCentres();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//if(e.CommandName=="delete")
//{
//GridViewRow row=(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//Label ld = (Label)GridView1.Rows[row.RowIndex].FindControl("lnkitem");
//cmd = new SqlCommand("delete from area where state=" + ld + "", con);
//con.Open();
//cmd.ExecuteNonQuery();
//con.Close();
//bindCentres();
//}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindCentres();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindCentres();
}
}
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style="width: 100%">
<tr>
<td>
<span class="title"> Centres Form</span>
<hr />
<asp:Panel ID="Panel2" runat="server">
<table bgcolor="#31CFF0" border="1" bordercolor="#E8E8E8" cellpadding="5"
cellspacing="0" class="texts" width="100%">
<tr>
<td style="width: 148px; height: 30px;">
<b>State</b></td>
<td style="height: 30px">
<asp:TextBox ID="txtstate" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
<asp:Button ID="btnstate" runat="server" class="resetbtn" TabIndex="32"
Text="Add State" Width="96px" onclick="btnstate_Click" />
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Choose State</b></td>
<td>
<asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True"
class="textbox" Height="23px" TabIndex="7" Width="175px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>City's</b></td>
<td>
<asp:TextBox ID="txt_addCity" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
<asp:Button ID="btn_AddCity" runat="server" class="resetbtn" TabIndex="32"
Text="Add City" Width="96px" onclick="btn_AddCity_Click" />
</td>
</tr>
<tr>
<td style="width: 148px">
</td>
<td>
</td>
</tr>
<caption>
<asp:HiddenField ID="HiddenField3" runat="server" />
</caption>
</table>
</asp:Panel>
<br />
<asp:Panel ID="Panel1" runat="server">
<table bgcolor="#31CFF0" border="1" bordercolor="#E8E8E8" cellpadding="5"
cellspacing="0" class="texts" width="100%">
<tr>
<td style="width: 148px" class"WHITETXT">
<b>State </b> </td>
<td>
<asp:DropDownList ID="DropDownList3" runat="server" class="textbox"
Width="175px" Height="23px"
TabIndex="7" AutoPostBack="True"
onselectedindexchanged="DropDownList3_SelectedIndexChanged" >
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>City</b></td>
<td>
<asp:DropDownList ID="ddlcity" runat="server" AutoPostBack="True"
class="textbox" Height="23px" TabIndex="7" Width="175px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Area</b></td>
<td>
<asp:TextBox ID="txtArea" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Address</b></td>
<td>
<asp:TextBox ID="txtAddress" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
<b>Email ID's</b></td>
<td>
<asp:TextBox ID="txtEmail" runat="server" Class="textbox" TabIndex="6"
Width="175px"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 148px">
</td>
<td>
</td>
</tr>
<caption>
<asp:HiddenField ID="HiddenField1" runat="server" />
</caption>
</table>
</asp:Panel>
<div >
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
<asp:Button ID="Button6" runat="server" CssClass="submitbtn"
onclick="Button1_Click" TabIndex="31" Text="Add Centres" Width="104px" />
<asp:Button ID="Button2" runat="server" class="resetbtn" Text="Update"
Width="96px" TabIndex="32" onclick="Button2_Click"></asp:Button>
<asp:Button ID="Button3" runat="server" class="resetbtn" Text="Delete"
Width="96px" TabIndex="32" ></asp:Button>
</div>
</td>
<td>
</td>
</tr>
<tr>
<td>
<center>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" BorderStyle="None" DataKeyNames="areaid,area"
BorderWidth="1px" CellPadding="3" Font-Bold="True"
PagerSettings-Mode="Numeric" RowStyle-CssClass="Descriptiongv2"
ShowFooter="True" Width="100%" BackColor="White" BorderColor="#CCCCCC"
onpageindexchanging="GridView1_PageIndexChanging"
onrowdeleting="GridView1_RowDeleting"
onrowupdating="GridView1_RowUpdating" onrowcommand="GridView1_RowCommand"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" AutoGenerateEditButton="True"
>
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle CssClass="Descriptiongv2" BackColor="White" ForeColor="#330099"></RowStyle>
<Columns>
<asp:TemplateField HeaderText="State " ItemStyle-Height="30px">
<ItemTemplate>
<center>
<asp:LinkButton ID="lnkitem" runat="server" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="cmdBind" Text='<%# Eval("state") %>'></asp:LinkButton>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="City" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbcity" runat="server" Text='<%# Eval("city") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Area" ItemStyle-Height="30px">
<ItemTemplate>
<center><asp:Label ID="lbarea" runat="server" Text='<%# Eval("area") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbaddress" runat="server" Text='<%# Eval("address") %>'></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" ItemStyle-Height="30px">
<ItemTemplate>
<center> <asp:Label ID="lbemail" runat="server" Text='<%# Eval("email")%>' ></asp:Label> </center>
</ItemTemplate>
<ItemStyle Height="30px" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowSelectButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
<SelectedRowStyle Font-Bold="True" ForeColor="White" BackColor="#669999" />
<PagerStyle HorizontalAlign="Left" BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White"
Font-Size="Smaller" />
</asp:GridView>
<asp:HiddenField ID="HiddenField2" runat="server" />
</center>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</asp:Content>
.......................code behind.........................
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlCommand cmd;
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindState();
bindState1();
bindCentres();
bindCity();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
InsertArea();
bindCentres();
Literal1.Text="Successfully Inserted";
}
protected int getCityId(string id)
{
int id1 = 0;
cmd = new SqlCommand("select cityid from city where stateid='" + id + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
id1 = Convert.ToInt32(dr[0]);
}
con.Close();
return id1;
}
protected void InsertArea()
{
string dt = DateTime.Now.ToString();
int sid = stateId(DropDownList3.SelectedValue);
int cid = getCityId(DropDownList3.SelectedValue);
string str = "insert into area values ("+cid+","+sid+",'" + txtArea.Text + "','"+txtAddress.Text+"','" + txtEmail.Text + "','"+dt+"')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
protected void bindCentres()
{
con.Open();
string str = "select a.areaid, s.state,c.city,a.area,a.address,a.email,s.date from state as s inner join city as c on s.stateid=c.stateid inner join area as a on a.cityid=c.cityid order by s.date desc";
da = new SqlDataAdapter(str, con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected int stateId(string id)
{
int id1 = 0;
cmd = new SqlCommand("select stateId from state where stateid='" + id + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
id1 = Convert.ToInt32(dr[0]);
}
con.Close();
return id1;
}
protected void insertstate()
{
try
{
string date = DateTime.Now.ToString();
string str = "insert into state(state,date) values ('" + txtstate.Text + "','"+date+"')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
catch(Exception ex)
{
}
}
protected void insertCity()
{
try
{
string datectiy = DateTime.Now.ToString();
int sid = stateId(ddlState.SelectedValue);
string str = "insert into city values (" + sid + ",'" + txt_addCity.Text + "','" + datectiy + "')";
cmd = new SqlCommand(str, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
txt_addCity.Text = "";
txtArea.Text = "";
txtAddress.Text = "";
txtEmail.Text = "";
}
catch(Exception ex)
{
}
}
public void bindState()
{
try
{
da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
da.Fill(dt);
ddlState.DataSource = dt;
ddlState.DataTextField = "state";
ddlState.DataValueField = "stateid";
ddlState.DataBind();
ddlState.Items.Insert(0, new ListItem("--Select State--", "0"));
}
catch(Exception ex)
{
}
}
public void bindState1()
{
da = new SqlDataAdapter("select * from state", con);
DataTable dt = new DataTable();
da.Fill(dt);
DropDownList3.DataSource = dt;
DropDownList3.DataTextField = "state";
DropDownList3.DataValueField = "stateId";
DropDownList3.DataBind();
DropDownList3.Items.Insert(0, new ListItem("--Select State--", "0"));
}
public void bindCity()
{
da = new SqlDataAdapter("select * from city where stateid='" + DropDownList3.SelectedValue + "'", con);
DataTable dt = new DataTable();
da.Fill(dt);
ddlcity.DataSource = dt;
ddlcity.DataTextField = "city";
ddlcity.DataValueField = "cityid";
ddlcity.DataBind();
ddlcity.Items.Insert(0, new ListItem("--Select City--", "0"));
}
protected void btnstate_Click(object sender, EventArgs e)
{
insertstate();
bindState();
Literal1.Text = "Successfully Inserted";
}
protected void btn_AddCity_Click(object sender, EventArgs e)
{
insertCity();
bindCity();
Literal1.Text = "Successfully Inserted";
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
bindCity();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["areaid"].ToString());
string username = GridView1.DataKeys[e.RowIndex].Values["area"].ToString();
con.Open();
SqlCommand cmd = new SqlCommand("delete from area where areaid=" + userid, con);
int result = cmd.ExecuteNonQuery();
con.Close();
bindCentres();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string area = GridView1.DataKeys[e.RowIndex].Values["area"].ToString();
TextBox txtarea = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtarea");
TextBox txtaddress = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtaddress");
TextBox txtemail = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtemail");
con.Open();
SqlCommand cmd = new SqlCommand("update area set area='" + txtarea.Text + "',address='" + txtaddress.Text + "',email='"+txtemail.Text+"' where areaid=" + userid, con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bindCentres();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindCentres();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//if(e.CommandName=="delete")
//{
//GridViewRow row=(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
//Label ld = (Label)GridView1.Rows[row.RowIndex].FindControl("lnkitem");
//cmd = new SqlCommand("delete from area where state=" + ld + "", con);
//con.Open();
//cmd.ExecuteNonQuery();
//con.Close();
//bindCentres();
//}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindCentres();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindCentres();
}
}
.....................................................