Gridview Dynamic Binding the data:
In this example i am inserting data to database and using grid we are going to display the data in the Front-End .
here i am using fields as
sno, Firstname,Lastname,Emailid,Phoneno,Address
where i am using sno as auto generate column ..
2)when i am using Submit Button the data should be store in database and display in the Grid.
Code For Design Page Of the example Aspx code
<body style="background-color:Silver">
<form id="form1" runat="server">
<div align="center">
<h2>Registration Page</h2>
<table>
<tr>
<td>First Name</td>
<td>:</td>
<td><asp:TextBox ID="txtfirstname"runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Last Name</td>
<td>:</td>
<td><asp:TextBox ID="txtlastname"runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Email id</td>
<td>:</td>
<td><asp:TextBox ID="txtemailid"runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Phone No</td>
<td>:</td>
<td><asp:TextBox ID="txtphoneno"runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td>Address</td>
<td>:</td>
<td><asp:TextBox ID="txtaddress"runat="server"></asp:TextBox> </td>
</tr>
<tr>
<td></td>
<td></td>
<td><asp:Button ID="btnsubmit"runat="server"Text="Submit"
onclick="btnsubmit_Click"/></td>
</tr>
</table>
<asp:GridView ID="GridView1"runat="server"AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="username">
<ItemTemplate>
<asp:Label ID="lblfstname"runat="server"Text='<%#Bind("firstname") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lastname">
<ItemTemplate>
<asp:Label ID="lbllstname"runat="server"Text='<%#Bind("lastname") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Emailid">
<ItemTemplate>
<asp:Label ID="lblemail"runat="server"Text='<%#Bind("emailid") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phoneno">
<ItemTemplate>
<asp:Label ID="lblphno"runat="server"Text='<%#Bind("Phoneno") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<%-- <asp:Button ID="btndel" runat="server" Text="Delete" OnCommand="delete" CommandArgument='<%#Bind("sno")%>' />--%>
<asp:ImageButton ID="btnlink"runat="server"Text="Delete"ImageUrl="~/icon_button_close.gif"OnCommand="delete"CommandArgument='<%#Bind("sno")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
Next I am Using Aspx.cs page
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 samplegrid : System.Web.UI.Page
{
string conn = ConfigurationManager.ConnectionStrings["connent"].ToString();
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter dap;
DataSet ds;
codefile obj= new codefile();
protected voidPage_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gridbinddata();
}
}
protected voidbtnsubmit_Click(object sender, EventArgs e)
{
obj.databind1(txtfirstname.Text,txtlastname.Text,txtemailid.Text,txtphoneno.Text,txtaddress.Text);
gridbinddata();
}
private voidgridbinddata()
{
GridView1.DataSource = obj.gridbinddata();
GridView1.DataBind();
}
public void delete(object sender, CommandEventArgse)
{
string str = e.CommandArgument.ToString();
con = new SqlConnection(conn);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "spdeletedata";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@spsno", str);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
gridbinddata();
}
}
Class File of aspx file or Ado.net file
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
/// <summary>
/// Summary description for codefile
/// </summary>
public class codefile
{
string conn = ConfigurationManager.ConnectionStrings["connent"].ToString();
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter dap;
DataSet ds;
public codefile()
{
//
// TODO: Add constructor logic here
//
}
public voiddatabind1(string txtfirstname, string txtlastname, stringtxtemailid, string txtphoneno, string txtaddress)
{
con = new SqlConnection(conn);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "spregistrationfrom";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@spfirstname", txtfirstname);
cmd.Parameters.AddWithValue("@splastname", txtlastname);
cmd.Parameters.AddWithValue("@spemailid", txtemailid);
cmd.Parameters.AddWithValue("@spphoneno", txtphoneno);
cmd.Parameters.AddWithValue("@spaddress", txtaddress);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
public DataSetgridbinddata()
{
con = new SqlConnection(conn);
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "spgriddata";
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
dap = new SqlDataAdapter(cmd);
ds = new DataSet();
dap.Fill(ds);
return (ds);
}
0 comments:
Post a Comment