temporarygridview with insert update delete product item using asp.net with c#
insert product
Edit the product
Delete checked data
product.aspx
<%@ 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 bgcolor="#3399CC">
<form id="form1" runat="server">
<div>
<table border="1" bordercolor="white">
<tr>
<td>
<asp:Label ID="Label2" ForeColor="White" runat="server" Text="product_name"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="rfvFirstName"
runat="server"
ControlToValidate="TextBox2"
ErrorMessage="company Name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" ForeColor="White" Text="product_description"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox3"
ErrorMessage="description name can't be left blank"
Display="Dynamic"> </asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label4" runat="server" ForeColor="White" Text="product_quntity"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
<td>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="only digit is allowed" ControlToValidate="TextBox4"
Display="Dynamic"
Font-Bold="True" Type="Integer" Operator="DataTypeCheck"></asp:CompareValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="Please
enter quantity"
ControlToValidate="TextBox4" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label5" ForeColor="White" runat="server" Text="product_rate"></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ErrorMessage="Please
enter price"
ControlToValidate="TextBox5" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button3" runat="server" Text="update" onclick="Button3_Click" Visible="false"/>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="add" onclick="Button1_Click" />
<asp:Button ID="Button4" runat="server" Text="cleare" onclick="Button4_Click" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" ShowFooter="true" ForeColor="White"
BorderColor="White" BorderWidth="1" runat="server" AutoGenerateColumns="False"
onrowdatabound="GridView1_RowDataBound"
onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting"
>
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
AutoPostBack="true"
oncheckedchanged="CheckBox1_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="pro_nm">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%#Eval("pro_name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="pro_desc">
<ItemTemplate>
<asp:Label ID="Label9" runat="server"
Text='<%#Eval("pro_desc")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="quantity">
<ItemTemplate>
<asp:Label ID="Label10"
runat="server"
Text='<%#Eval("quantity")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="rate">
<ItemTemplate>
<asp:Label ID="Label11"
runat="server"
Text='<%#Eval("rate")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label13"
runat="server"
Text="grand
total"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="total">
<ItemTemplate>
<asp:Label ID="Label12"
runat="server"
Text='<%#Eval("total")
%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label6" runat="server"
></asp:Label>
</FooterTemplate>
<asp:TemplateField HeaderText="edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
runat="server"
CommandName="edit"
CausesValidation="False">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" CommandName="delete" runat="server" CausesValidation="False">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button2" runat="server" Text="submit"
onclick="Button2_Click"
Visible="false"
CausesValidation="False"
/>
<asp:Button ID="Button5" runat="server" Text="DELETE ALL"
onclick="Button5_Click" CausesValidation="False" />
</div>
</form>
</body>
</html>
product.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.Collections;
public partial class _Default : System.Web.UI.Page
{
static double m = 0;
DataTable
dt;
DataTable
dt1;
// int xxx=0;
double
total = 0;
static double t = 0;
static int chk=0;
protected void Page_Load(object
sender, EventArgs e)
{
m = 0;
if
(Page.IsPostBack)
{
dt = Session["data_table"] as
DataTable;
}
if
(!Page.IsPostBack)
{
Session["data_table"]
= null;
chk = 0;
}
if
(Request.QueryString["pid"] != null && chk==0)
{
chk++;
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "select
p.pro_name,p.pro_desc,pi.quantity,pi.rate,pi.total from pi_productex p INNER
JOIN pi_rate pi on p.pro_id=pi.pro_id
where pi.pro_id ='"+Request.QueryString["pid"].ToString()+"'";
SqlDataAdapter
da = new SqlDataAdapter();
DataTable
ds = new DataTable();
SqlCommand
cmd1 = new SqlCommand(q,
conn);
conn.Open();
da.SelectCommand = cmd1;
cmd1.ExecuteNonQuery();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
dt = ds;
Session["data_table"]
= dt;
conn.Close();
}
}
protected void Button1_Click(object
sender, EventArgs e)
{
if
(dt == null)
{
dt = new
DataTable();
//DataColumn
dc0 = new DataColumn("pro_id");
DataColumn
dc0 = new DataColumn("pro_name");
DataColumn
dc1 = new DataColumn("pro_desc");
DataColumn
dc2 = new DataColumn("quantity");
DataColumn
dc3 = new DataColumn("rate");
DataColumn
dc4 = new DataColumn("total");
//dt.Columns.Add(dc0);
dt.Columns.Add(dc0);
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
//dt.Columns.Add(dc5);
m = 0;
}
DataRow
dr1 = dt.NewRow();
// dr1[0] = TextBox1.Text;
dr1[0] = TextBox2.Text;
dr1[1] = TextBox3.Text;
dr1[2] = TextBox4.Text;
dr1[3] = TextBox5.Text;
Double price =Double.Parse(TextBox4.Text);
Double
quan = Double.Parse(TextBox5.Text);
double
total = price * quan;
dr1[4] = total;
dt.Rows.Add(dr1);
GridView1.DataSource = dt;
GridView1.DataBind();
Session["data_table"]
= dt;
TextBox4.Text = "";
TextBox5.Text = "";
Button2.Visible = true;
m = 0;
// t = 0;
}
protected void Button2_Click(object
sender, EventArgs e)
{
if
(Request.QueryString["pid"] == null)
{
string
q;
int
i = 0;
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
q = "insert
into pi_productex(pro_name,pro_desc,gtotal) values ('" +
dt.Rows[0][0].ToString() + "','" +
dt.Rows[0][1].ToString() + "','" +
t + "')";
conn.Open();
SqlCommand
cmd = new SqlCommand(q,
conn);
i = cmd.ExecuteNonQuery();
double
gp = 0;
for
(int r = 0; r < dt.Rows.Count; r++)
{
SqlDataAdapter
da = new SqlDataAdapter("select max(pro_id) as pro_id from
pi_productex", conn);
DataTable
dt11 = new DataTable();
da.Fill(dt11);
//Session["id"]
= dt11.Rows[0]["pro_id"].ToString();
string
q1 = "insert into
pi_rate(pro_id,quantity,rate,total) values ('" + dt11.Rows[0]["pro_id"].ToString() + "','" + dt.Rows[r][2].ToString() + "','" + dt.Rows[r][3].ToString() + "','" + dt.Rows[r][4].ToString() + "')";
SqlCommand
cmd1 = new SqlCommand(q1,
conn);
//
conn.Open();
i = cmd1.ExecuteNonQuery();
}
conn.Close();
if
(i > 0)
{
Response.Redirect("~/showdata.aspx");
}
}
else
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "delete from pi_rate where pro_id='"
+Request.QueryString["pid"].ToString()+
"'";
string
qs = "update pi_productex set gtotal='"
+ t + "' where pro_id='" +
Request.QueryString["pid"].ToString()
+ "'";
SqlCommand
cmd = new SqlCommand(q,
conn);
SqlCommand
cmd3 = new SqlCommand(qs,
conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd3.ExecuteNonQuery();
conn.Close();
for
(int r = 0; r < dt.Rows.Count; r++)
{
string
q1 = "insert into
pi_rate(pro_id,quantity,rate,total) values ('" +
Request.QueryString["pid"].ToString()
+ "','" + dt.Rows[r][2].ToString()
+ "','" + dt.Rows[r][3].ToString()
+ "','" + dt.Rows[r][4].ToString()
+ "')";
SqlCommand
cmd1 = new SqlCommand(q1,
conn);
conn.Open();
int i = cmd1.ExecuteNonQuery();
conn.Close();
}
Response.Redirect("showdata.aspx");
}
}
protected void Button3_Click(object
sender, EventArgs e)
{
if
(Request.QueryString["pid"] == null)
{
dt.Rows[count][0] = TextBox2.Text;
dt.Rows[count][1] = TextBox3.Text;
dt.Rows[count][2] = TextBox4.Text;
dt.Rows[count][3] = TextBox5.Text;
int
price = Int32.Parse(TextBox4.Text);
int
quan = Int32.Parse(TextBox5.Text);
int
total = price * quan;
dt.Rows[count][4] = total;
GridView1.DataSource = dt;
GridView1.DataBind();
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
Button4.Visible = true;
Button1.Visible = true;
Button3.Visible = false;
m = 0;
}
else
{
dt.Rows[count][0] = TextBox2.Text;
dt.Rows[count][1] = TextBox3.Text;
dt.Rows[count][2] = TextBox4.Text;
dt.Rows[count][3] = TextBox5.Text;
int
price = Int32.Parse(TextBox4.Text);
int
quan = Int32.Parse(TextBox5.Text);
int
total = price * quan;
dt.Rows[count][4] = total;
GridView1.DataSource = dt;
GridView1.DataBind();
Session["data_table"]
= dt;
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
Button4.Visible = true;
Button1.Visible = true;
Button3.Visible = false;
m = 0;
Button2.Visible = true;
}
}
protected void Button4_Click(object
sender, EventArgs e)
{
//TextBox1.Text="";
TextBox2.Text="";
TextBox3.Text="";
TextBox4.Text="";
TextBox5.Text="";
}
protected void GridView1_RowDataBound(object
sender, GridViewRowEventArgs e)
{
if
(e.Row.RowType == DataControlRowType.Footer)
{
double
d = 0;
Label
gt = (Label)e.Row.FindControl("Label6");
if
(dt != null)
{
for
(int i = 0; i < dt.Rows.Count; i++)
{
d += Double.Parse(dt.Rows[i][4].ToString());
//
t += Double.Parse(dt.Rows[i][4].ToString());
gt.Text = d.ToString();
t =double.Parse(
gt.Text);
}
}
else
{
//
Label tt = (Label)e.Row.FindControl("Label12");
gt.Text = total.ToString();
}
}
if
(e.Row.RowType == DataControlRowType.DataRow)
{
Label
tll = (Label)e.Row.FindControl("Label12");
total += Double.Parse(tll.Text);
}
}
static int count = 0;
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
Label
pnm = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label7");
TextBox2.Text = pnm.Text;
Label
pdesc = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label9");
TextBox3.Text = pdesc.Text;
Label
pq = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label10");
TextBox4.Text = pq.Text;
Label
ppr = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label11");
TextBox5.Text = ppr.Text;
count = e.NewEditIndex;
TextBox2.ReadOnly = true;
TextBox3.ReadOnly = true;
Button1.Visible = true;
Button4.Visible = false;
Button3.Visible = true;
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
dt.Rows.RemoveAt(e.RowIndex);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox
chk = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox1");
if
(chk.Checked == true)
{
foreach
(GridViewRow r in
GridView1.Rows)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[r.RowIndex].FindControl("CheckBox2");
chk1.Checked = true;
}
}
else
{
foreach
(GridViewRow r in
GridView1.Rows)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[r.RowIndex].FindControl("CheckBox2");
chk1.Checked = false;
}
}
}
protected void Button5_Click(object
sender, EventArgs e)
{
int j = 0;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox
chk1 = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox2");
if
(chk1 != null)
{
if
(chk1.Checked)
{
if
(i == 0)
{
dt.Rows.RemoveAt(i);
}
else
{
j++;
dt.Rows.RemoveAt(i-j);
}
}
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Show the data
Edit the Data
Show on gridview all two data
showdesign.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="showdata.aspx.cs"
Inherits="showdata"
%>
<!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 bgcolor="#3399CC">
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="pro_id" onrowediting="GridView1_RowEditing" ForeColor="White"
ShowFooter="true"
onrowdeleting="GridView1_RowDeleting"
>
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="Label8" runat="server"
Text='<%#Eval("pro_id")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="pro_nm">
<ItemTemplate>
<asp:Label ID="Label7" runat="server"
Text='<%#Eval("pro_name")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="pro_desc">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text='<%#Eval("pro_desc")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="total">
<ItemTemplate>
<asp:Label ID="Label12"
runat="server"
Text='<%#Eval("gtotal")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="edit">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandName="edit"
runat="server">edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="delete">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2"
CommandName="delete"
runat="server">delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
showdesign.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;
public partial class showdata : System.Web.UI.Page
{
protected void Page_Load(object
sender, EventArgs e)
{
grid();
}
public void grid()
{
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q = "select
p.pro_id,p.pro_name,p.pro_desc,p.gtotal from pi_productex p INNER JOIN pi_rate
pi on p.pro_id=pi.pro_id group by
p.pro_id,p.pro_name,p.pro_desc,p.gtotal ";
SqlDataAdapter
da = new SqlDataAdapter();
DataTable
ds = new DataTable();
SqlCommand
cmd1 = new SqlCommand(q,
conn);
conn.Open();
da.SelectCommand = cmd1;
cmd1.ExecuteNonQuery();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
conn.Close();
}
protected void GridView1_RowEditing(object
sender, GridViewEditEventArgs e)
{
Label
l = (Label)GridView1.Rows[e.NewEditIndex].FindControl("Label8");
Response.Redirect("~/Default.aspx?pid="+l.Text);
}
protected void GridView1_RowDeleting(object
sender, GridViewDeleteEventArgs e)
{
Label
pid = (Label)GridView1.Rows[e.RowIndex].FindControl("Label8");
string
con = @"Data Source=SQLDB;Initial
Catalog=Demo;User ID=Demoh;Password=Demo1@";
SqlConnection
conn = new SqlConnection(con);
string
q1 = "delete from pi_rate where pro_id='"
+ pid.Text + "'";
string
q = "delete
pi_productex where pi_productex.pro_id='" +pid.Text+"' ";
SqlCommand
cmd1 = new SqlCommand(q,
conn);
SqlCommand
cmd = new SqlCommand(q1,
conn);
conn.Open();
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
GridView1.EditIndex = -1;
grid();
conn.Close();
}
}
0 comments :