0% found this document useful (0 votes)
178 views7 pages

GV Pro

The document contains code for an ASP.NET web application that allows users to view, add, edit and delete records in a SQL database table using a GridView control. The code connects to the database, retrieves data to populate the GridView, handles user input for inserting, updating and deleting records, and manages the GridView editing functionality. The database table structure and web.config connection string are also included.

Uploaded by

Mano Haran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views7 pages

GV Pro

The document contains code for an ASP.NET web application that allows users to view, add, edit and delete records in a SQL database table using a GridView control. The code connects to the database, retrieves data to populate the GridView, handles user input for inserting, updating and deleting records, and manages the GridView editing functionality. The database table structure and web.config connection string are also included.

Uploaded by

Mano Haran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Gridview Gv.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; using System.Data.SqlClient; using System.Web.UI.HtmlControls; using System.Configuration; using System.IO; using System.Text; namespace mano_sample { public partial class gv : System.Web.UI.Page { SqlConnection con = new SqlConnection(); SqlCommand cmd; DataTable dt; SqlDataAdapter da; protected void Page_Load(object sender, EventArgs e) { con.ConnectionString = ConfigurationManager.ConnectionStrings["mycon"].ConnectionString; if (con.State == ConnectionState.Closed) { con.Open(); }

if (IsPostBack == false) { grdview(); } con.Close(); } private void grdview() { if (con.State == ConnectionState.Closed) { con.Open(); } da = new SqlDataAdapter("select * from tbuser", con); dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count == 0) { //Response.Write("No record found"); } else { GridView1.DataSource = dt; GridView1.DataBind(); }

con.Close(); } protected void Button1_Click(object sender, EventArgs e) { if (con.State == ConnectionState.Closed) { con.Open(); } //da = new SqlDataAdapter("Select uid from tbuser where uid=@uid ", con); //da.SelectCommand.Parameters.Add("uid", Txt_uid.Text); //dt = new DataTable(); //da.Fill(dt); //da.Dispose(); //if (dt.Rows.Count == 0) //{ cmd = new SqlCommand("insert into tbuser values(@uid,@Name,@Address,@Salary)",con); cmd.Parameters.AddWithValue("@uid",Txt_uid.Text); cmd.Parameters.AddWithValue("@Name",Txt_Name.Text); cmd.Parameters.AddWithValue("@Address",Txt_Address.Text); cmd.Parameters.AddWithValue("@Salary", Txt_Salary.Text); cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); grdview(); clr(); //} //else //{ // Response.Write("user name is already exits"); //} } private void clr() { Txt_uid.Text = ""; Txt_Name.Text = ""; Txt_Address.Text = ""; Txt_Salary.Text = ""; } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; grdview(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; grdview(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; grdview(); }

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { Label id = ((Label)(GridView1.Rows[e.RowIndex].FindControl("lb_id"))); if (con.State == ConnectionState.Closed) { con.Open(); } cmd = new SqlCommand("delete from tbuser where id=@id", con); cmd.Parameters.AddWithValue("@id", id.Text); cmd.ExecuteNonQuery(); cmd.Dispose(); con.Close(); grdview(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Label id = ((Label)(GridView1.Rows[e.RowIndex].FindControl("lbl_edit"))); if (con.State == ConnectionState.Closed) { con.Open(); } cmd = new SqlCommand("update tbuser set name=@name,salary=@salary,address=@address where id=@id", con); cmd.Parameters.AddWithValue("@name",((TextBox)(GridView1.Rows[e.RowIndex].FindControl("tx t_ename"))).Text); cmd.Parameters.AddWithValue("@salary",Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowInde x].FindControl("txt_esal"))).Text)); cmd.Parameters.AddWithValue("@address",((TextBox)(GridView1.Rows[e.RowIndex].FindControl( "txt_eadd"))).Text); cmd.Parameters.AddWithValue("@id", id.Text); cmd.ExecuteNonQuery(); cmd.Dispose(); GridView1.EditIndex = -1; con.Close(); grdview(); } } } Gv.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gv.aspx.cs" Inherits="mano_sample.gv" %> <!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>

<table> <tr> <td> Uid </td> <td> <asp:TextBox ID="Txt_uid" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Name </td> <td> <asp:TextBox ID="Txt_Name" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Address </td> <td> <asp:TextBox ID="Txt_Address" runat="server" Height="22px"> </asp:TextBox> </td> </tr> <tr> <td> Salary </td> <td> <asp:TextBox ID="Txt_Salary" runat="server"></asp:TextBox> </td> </tr> <tr> <td> &nbsp; </td> <td> <asp:Button ID="Button1" runat="server" Text="Insert" Width="89px" OnClick="Button1_Click" /> </td> </tr> <tr> <td> &nbsp; </td> <td> &nbsp; </td> </tr> <tr> <td> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="1" ForeColor="#333333" GridLines="None" OnRowCancelingEdit="GridView1_RowCancelingEdit"

OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" CellSpacing="1"> <%-- OnSelectedIndexChanging="GridView1_SelectedIndexChanging"-%> <RowStyle BackColor="#EFF3FB" /> <Columns> <asp:TemplateField HeaderText="S.No"> <ItemTemplate> <%#Container.DataItemIndex + 1 %> <asp:Label ID="Label5" runat="server"></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Uid"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("uid") %>'></asp:Label> <asp:Label ID="lb_id" runat="server" Text='<%#Eval("id") %>' ></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lbl_edit" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txt_ename" runat="server" Text='<%#Eval("name") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Salary"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%#Eval("Salary") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txt_esal" runat="server" Text='<%#Eval("Salary") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address"> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%#Eval("Address") %>'></asp:Label> </ItemTemplate> <EditItemTemplate>

<asp:TextBox ID="txt_eadd" runat="server" Text='<%#Eval("address") %>' TextMode="MultiLine" Width="246px"></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Edit"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="edit">Edit</asp:LinkButton> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="update">Update</asp:LinkButton> <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" CommandName="cancel">Cancel</asp:LinkButton> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Delete"> <ItemTemplate> <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="false" CommandName="delete" OnClientClick="return confirm('are you sure you want to delet this column')">Delete</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> HorizontalAlign="Center" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#FF9999" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> </td> </tr> </table> </div> </form> </body> </html> <PagerStyle BackColor="#2461BF" ForeColor="White" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"

DB:
CREATE TABLE [dbo].[tbuser]( [id] [int] IDENTITY(1,1) NOT NULL, [uid] [varchar](50) NOT NULL, [Name] [varchar](50) NULL, [address] [varchar](50) NULL, [salary] [int] NULL,)on PRIMARY KEY

Web.config: <configuration> <connectionStrings> <add name="mycon" connectionString="Data Source=192.168.0.9;Initial Catalog=test;User ID=sa;Password=sa;"/> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </connectionStrings> Screen shot:

You might also like