Code for the Configuration file.....
<connectionStrings> <add name="HelloConnectionString" connectionString="Data Source=HIMANSHU-PC\HIMANSHU;Initial Catalog=Hello;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings>
Coding for the .aspx file.....
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<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" AutoGenerateColumns="false" DataKeyNames="Empid"
AllowPaging="true" AllowSorting="true" PageSize="5" OnPageIndexChanging="GridView1_PageIndexChanging"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" ShowFooter="True">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Empid
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Empid") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt1" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Empname
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Empname") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="t1" runat="server" Text='<%# Eval("Empname") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt2" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
City
</HeaderTemplate>
<ItemTemplate>
<%# Eval("City") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="t2" runat="server" Text='<%# Eval("City") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt3" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Salary
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Salary") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="t3" runat="server" Text='<%# Eval("Salary") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt4" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Deptid
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Deptid") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="t4" runat="server" Text='<%# Eval("Deptid") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt5" runat="server" Width="100"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="Btn1" runat="server" Text="Insert" OnClick="Insert_Data" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Coding for the aspx.cs file....
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.Configuration;
public partial class Default4 : System.Web.UI.Page
{
string s = ConfigurationManager.ConnectionStrings["HelloConnectionString"].ToString();
SqlDataAdapter da;
DataSet ds = new DataSet();
SqlCommand cmd;
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(s);
if (!Page.IsPostBack)
ShowData();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
ShowData();
}
public void ShowData()
{
da = new SqlDataAdapter("SELECT * From Emp", s);
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
ShowData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
ShowData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;
string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t1")).Text;
string city = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("t2")).Text;
int sal = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("t3")).Text);
int did = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].FindControl("t4")).Text);
cmd = new SqlCommand("UPDATE Emp SET Empname=@p1,City=@p2,Salary=@p3,Deptid=@p4 WHERE Empid=@id", con);
con.Open();
cmd.Parameters.AddWithValue("@id", SqlDbType.Int).Value = id;
cmd.Parameters.AddWithValue("@p1", SqlDbType.VarChar).Value = name;
cmd.Parameters.AddWithValue("@p2", SqlDbType.VarChar).Value = city;
cmd.Parameters.AddWithValue("@p3", SqlDbType.Int).Value = sal;
cmd.Parameters.AddWithValue("@p4", SqlDbType.Int).Value = did;
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
ShowData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
TextBox t1, t2, t3, t4, t5;
protected void Insert_Data(object sender, EventArgs e)
{
con.Open();
t1 = (TextBox)GridView1.FooterRow.FindControl("txt1");
t2 = (TextBox)GridView1.FooterRow.FindControl("txt2");
t3 = (TextBox)GridView1.FooterRow.FindControl("txt3");
t4 = (TextBox)GridView1.FooterRow.FindControl("txt4");
t5 = (TextBox)GridView1.FooterRow.FindControl("txt5");
cmd = new SqlCommand("INSERT INTO Emp VALUES(@p1,@p2,@p3,@p4,@p5)", con);
cmd.Parameters.AddWithValue("@p1", t1.Text);
cmd.Parameters.AddWithValue("@p2", t2.Text);
cmd.Parameters.AddWithValue("@p3", t3.Text);
cmd.Parameters.AddWithValue("@p4", t4.Text);
cmd.Parameters.AddWithValue("@p5", t5.Text);
cmd.ExecuteNonQuery();
con.Close();
t1.Text = t2.Text = t3.Text = t4.Text = t5.Text = "";
//GridView1.EditIndex = -1;
ShowData();
}
}
For Deletion as a homework..........
No comments:
Post a Comment