Sunday, 7 April 2013

How to Insert table data through Gridview

Step 1: Create a table:
Create a table test
(
sno int not null identity(1,1),
sfname NvarChar(50),
slname NvarChar(50)
)
SnoFirstNameLastName
1Srinivas AmruthaMunagala
2SreenishIndian

Step 2:
GridViewExample.aspx
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView runat="server" Width="800px" HorizontalAlign="Center"  
ShowFooter="true" HeaderStyle-BackColor="#8FBC8F" ID="gd"  
AutoGenerateColumns="false" OnRowCommand="inserte"   OnRowCancelingEdit="cancle"
 OnRowDeleting="delete" OnRowUpdating="update" OnRowEditing="edit" >
    <Columns>

    <asp:TemplateField>
    <EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" CommandName="Update" runat="server"  
ImageUrl="~/images/imagesupdate.jpeg" ToolTip="Update" Height="20px"  
Width="20px" />
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel"  
ImageUrl="~/images/imagescancel.jpeg" ToolTip="Cancel" Height="20px"  
Width="20px" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnEdit" CommandName="Edit" runat="server"  
ImageUrl="~/images/imagesedit.jpeg" ToolTip="Edit" Height="20px"  
Width="20px" />
<asp:ImageButton ID="imgbtnDelete" CommandName="Delete" Text="Edit"  
runat="server" ImageUrl="~/images/imagesdelete.jpeg" ToolTip="Delete"
Height="20px" Width="20px" />
</ItemTemplate>

<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" ImageUrl="~/images/images.jpeg"  
CommandName="insert" Width="30px" Height="30px" ToolTip="Add new User"
ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="S NO"  >
    <ItemTemplate>
    <asp:Label runat="server" ID="lblsno" Text='<%#Eval("sno") %>' ></asp:Label>
    </ItemTemplate>


    </asp:TemplateField>
    <asp:TemplateField HeaderText="First Name" >
    <ItemTemplate>
    <asp:Label runat="server" ID="lblfname" Text='<%#Eval("sfname") %>' >
</asp:Label>
    </ItemTemplate>
    <EditItemTemplate>


    <asp:TextBox runat="server" ID="txtfname" Text='<%#Eval("sfname") %>'  >
</asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>

      <asp:TextBox runat="server" ID="txtftr" ></asp:TextBox>
      <asp:RequiredFieldValidator ID="rfvdesignatdfgion" runat="server"  
ControlToValidate="txtftr" Text="*" ValidationGroup="validaiton"/>

    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Last Name" >
    <ItemTemplate>
    <asp:Label runat="server" ID="lbllastname" Text='<%#Eval("slname") %>' >
</asp:Label>
    </ItemTemplate>
    <EditItemTemplate >
    <asp:TextBox runat="server" ID="txtlastname" Text='<%#Eval("slname") %>' >
</asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
     <asp:TextBox runat="server" ID="txtinlastname"   ></asp:TextBox>
     <asp:RequiredFieldValidator ID="rfvdesignation" runat="server"  
ControlToValidate="txtinlastname" Text="*" ValidationGroup="validaiton"/>

    </FooterTemplate>
    </asp:TemplateField>

    </Columns>

    </asp:GridView>
    </div>
    </form>
</body>

Step 3:
GridViewExample.cs
using System;
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("Data Source=DOTNET19;Initial 
Catalog=srinivas;User ID=sa;Password=admin123");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            filldata();
        }
   
    }
    protected void filldata()
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select * from test",con);
        DataSet dt = new DataSet();
        da.Fill(dt);
        gd.DataSource = dt;
        gd.DataBind();
        con.Close();
    }
    protected void cancle(object sender, GridViewCancelEditEventArgs e)

    {
        e.Cancel = true;
        gd.EditIndex = -1;
        filldata();
    }
    protected void edit(object sender, GridViewEditEventArgs e)
    {
        gd.EditIndex = e.NewEditIndex;
        filldata();
   
    }
    protected void update(object sender, GridViewUpdateEventArgs e)
    {
        con.Open();
        GridViewRow row = gd.Rows[e.RowIndex];
        Label lblid = (Label)row.FindControl("lblsno");
        int id = Convert.ToInt32(lblid.Text);
        TextBox txtbfname = (TextBox)row.FindControl("txtfname");
        TextBox txtblname = (TextBox)row.FindControl("txtlastname");
        string fname = txtbfname.Text.ToString();
        string lname = txtblname.Text.ToString();
        update(id, fname, lname);
    }
    public void update(int id, string fname, string lastname)
    {

        SqlCommand cmm = new SqlCommand("update test set sfname='" + fname + "',
 slname='" + lastname + "' where sno="+ id +"", con);
        cmm.ExecuteNonQuery();
        con.Close();
        gd.EditIndex = -1;
        filldata();


    }
    protected void delete(object sender, GridViewDeleteEventArgs e)
    {
   
        Label lblid1 = (Label)gd.Rows[e.RowIndex].FindControl("lblsno");
        int id1 = Convert.ToInt32(lblid1.Text);
        con.Open();
        SqlCommand cmm = new SqlCommand("delete from test where sno=" + id1 +
"", con);
        cmm.ExecuteNonQuery();
        con.Close();
        filldata();
    }
    protected void inserte(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "insert")
        {
            TextBox txtfname = (TextBox)gd.FooterRow.FindControl("txtftr");
            TextBox txtlname = (TextBox)gd.FooterRow.FindControl("txtinlastname");
            con.Open();
            SqlCommand cmm = new SqlCommand("insert into test values('" +
txtfname.Text + "', '" + txtlname.Text + "')", con);
            cmm.ExecuteNonQuery();
            con.Close();
            filldata();


        }
    }
}

No comments:

Post a Comment