<%@ 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 background="green-windows-7-wallpapers_11839_1024x768.jpg">
<form id="form1" runat="server">
<div style="width: 1013px">
<asp:GridView ID="GridView1" runat="server"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
BackColor="#666699" BorderColor="#996600" BorderStyle="Double"
BorderWidth="5px" Font-Bold="True" Font-Italic="False" Font-Size="Medium"
ForeColor="Black" Height="215px" onrowdeleting="GridView1_RowDeleting"
Width="450px" AutoGenerateColumns="False" AutoGenerateDeleteButton="True"
AutoGenerateEditButton="True" AutoGenerateSelectButton="True">
<RowStyle BorderColor="Black" />
<Columns>
<asp:BoundField DataField="Roll" HeaderText="Roll No" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Fees" HeaderText="Fees" />
<asp:TemplateField HeaderText="SEX">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataTextField="status" Width="127px" DataSource="<%# DropDownValue() %>" DataValueField="status">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("SEX")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#6600CC" ForeColor="White" />
<AlternatingRowStyle BackColor="#FFCCFF" BorderColor="#CC3300" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="New Entry" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Roll Number:" Font-Bold="True"
Font-Italic="True" ForeColor="White"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" style="margin-left: 0px"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Name" Font-Bold="True"
Font-Italic="True" ForeColor="White"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="128px"></asp:TextBox>
<br />
<asp:Label ID="Label3" runat="server" Text="Fees" Font-Bold="True"
Font-Italic="True" ForeColor="White"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label4" runat="server" BackColor="#000228" Font-Bold="True"
Font-Italic="True" ForeColor="White" Text="SEX"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="Button2" runat="server" Height="26px" Text="Submit"
onclick="Button2_Click" />
</div>
</form>
</body>
</html>
Default.aspx.cs:
Default.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Visible = false;
TextBox2.Visible = false;
TextBox3.Visible = false;
Label1.Visible = false;
Label2.Visible = false;
Label3.Visible = false;
Label4.Visible = false;
Button2.Visible = false;
DropDownList1.Visible = false;
if (!IsPostBack)
bindGridView();
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
}
public void bindGridView()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
con.Open();
SqlCommand mySqlCommand = new SqlCommand("SELECT Roll,Name,Fees,SEX FROM fees1", con );
SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
DataSet myDataSet = new DataSet();
mySqlAdapter.Fill(myDataSet);
GridView1.DataSource = myDataSet;
GridView1.DataBind();
con.Close();
}
protected DataTable DropDownValue()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
string Sqlquery = "SELECT status FROM master_status";
SqlDataAdapter dtadapter = new SqlDataAdapter(Sqlquery, con);
DataTable dt = new DataTable();
dtadapter.Fill(dt);
return dt;
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindGridView();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
////Update the values.
int ro = int.Parse(((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
string na = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string fe = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList1");
string se = ddl.SelectedValue;
string update = "UPDATE fees1 SET Name = '" + na +"', Fees = '" +fe +"',SEX='"+se+"' WHERE Roll = '" + ro +"';";
SqlCommand command = new SqlCommand(update, con);
con.Open();
command.ExecuteNonQuery();
con.Close();
////Reset the edit index.
GridView1.EditIndex = -1;
////Bind data to the GridView control.
bindGridView();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
int ro = int.Parse(datatable().Rows[e.RowIndex][0].ToString());
string delete = "DELETE FROM fees1 WHERE Roll = '" + ro + "';";
SqlCommand command = new SqlCommand(delete, con);
con.Open();
command.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
bindGridView();
}
public DataTable datatable()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid; uid=sa;password=hi";
SqlCommand command = new SqlCommand("SELECT * FROM fees1", con);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = command;
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Visible = true;
TextBox1.Text = "";
TextBox2.Visible = true;
TextBox2.Text = "";
TextBox3.Visible = true;
TextBox3.Text = "";
Label1.Visible = true;
Label2.Visible = true;
Label3.Visible = true;
Label4.Visible = true;
Button2.Visible = true;
DropDownList1.Visible = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=.;Initial Catalog=datagrid;uid=sa;password=hi";
SqlCommand cmd = new SqlCommand();
con.Open();
cmd = new SqlCommand("INSERT INTO fees1(Name,Fees,SEX) VALUES ('"+TextBox2 .Text +"','"+TextBox3 .Text +"','"+DropDownList1.Text+"')",con );
cmd.ExecuteNonQuery();
con.Close();
bindGridView();
}
}
No comments:
Post a Comment