Default.aspx
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function GetAmount()
{
var qty = document.getElementById('<%= txtQuantity.ClientID %>').value;
var Price = document.getElementById('<%= txtPrice.ClientID %>').value;
var Amount = (qty * Price);
document.getElementById('<%= txtAmount.ClientID %>').value = Amount;
}
</script>
<div>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
<div>
<div>
Bill No:
</div>
<div>
<asp:TextBox ID="txtBillNo" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBillNo" runat="server" ValidationGroup="Customer"
ControlToValidate="txtBillNo" ErrorMessage="<b>please enter bill no</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Customer Name:
</div>
<div>
<asp:TextBox ID="txtCustomerName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCustomerName" runat="server" ValidationGroup="Customer"
ControlToValidate="txtCustomerName" ErrorMessage="<b>please enter CustomerName</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Address:
</div>
<div>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddress" runat="server" ValidationGroup="Customer"
ControlToValidate="txtAddress" ErrorMessage="<b>please enter Address</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Gender:
</div>
<div>
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:RadioButtonList>
</div>
</div>
<div style="border: 1px solid gray; height: 200px; padding: 5px">
<div style="width: 155px; float: left;">
<div>
Product Name:
</div>
<div>
<asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvProductName" runat="server" ValidationGroup="Product"
ControlToValidate="txtProductName" ErrorMessage="<b>please enter product</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Quantity:
</div>
<div>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" runat="server" ValidationGroup="Product"
ControlToValidate="txtQuantity" ErrorMessage="<b>please enter Quantity</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Price:
</div>
<div>
<asp:TextBox ID="txtPrice" runat="server" onblur = "GetAmount()"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPrice" runat="server" ValidationGroup="Product"
ControlToValidate="txtPrice" ErrorMessage="<b>please enter Price</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Amount:
</div>
<div>
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
</div>
</div>
<div style="margin: 15px;">
<asp:Button ID="btnAdd" runat="server" ValidationGroup="Product" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</div>
<asp:GridView ID="GVProduct" runat="server" OnRowCancelingEdit="GVProduct_RowCancelingEdit"
OnRowDeleting="GVProduct_RowDeleting" OnRowEditing="GVProduct_RowEditing" >
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="true" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</div>
</div>
<div style="padding: 5px">
<asp:Button ID="btnSave" runat="server" ValidationGroup="Customer" Text="Save" OnClick="btnSave_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</div>
</asp:Content>
Default.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.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
CreateConnection();
}
public void CreateConnection()
{
Conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = null;
if (ViewState["dtProduct"] != null)
dt = (DataTable)ViewState["dtProduct"];
else
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Product", typeof(string)),
new DataColumn("Quantity", typeof(double)),
new DataColumn("Price",typeof(double)),
new DataColumn("Amount",typeof(double))});
}
if (btnAdd.Text == "Add")
{
dt.Rows.Add(txtProductName.Text, txtQuantity.Text, txtPrice.Text, txtAmount.Text);
}
else
{
int i = Convert.ToInt32(ViewState["Index"]);
dt.Rows[i]["Product"] = txtProductName.Text;
dt.Rows[i]["Quantity"] = txtQuantity.Text;
dt.Rows[i]["Price"] = txtPrice.Text;
dt.Rows[i]["Amount"] = txtAmount.Text;
}
GVProduct.EditIndex = -1;
GVProduct.DataSource = dt;
GVProduct.DataBind();
ViewState["dtProduct"] = dt;
ViewState["Index"] = null;
btnAdd.Text = "Add";
GVProduct.Enabled = true;
ClearProduct();
}
public void ClearProduct()
{
txtProductName.Text = "";
txtQuantity.Text = "";
txtPrice.Text = "";
txtAmount.Text = "";
}
public void Clear()
{
txtBillNo.Text = "";
txtCustomerName.Text = "";
txtAddress.Text = "";
txtProductName.Text = "";
txtQuantity.Text = "";
txtPrice.Text = "";
txtAmount.Text = "";
ViewState["dtProduct"] = null;
}
protected void btnClear_Click(object sender, EventArgs e)
{
ClearProduct();
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (ViewState["dtProduct"] != null)
{
CreateConnection();
SqlCommand cmd = null;
object obj = null;
cmd = Conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into Customerdetail(BillNo,CustomerName,Address,Gender) values (" + txtBillNo.Text + "," + "'" + txtCustomerName.Text + "'" + "," + "'" + txtAddress.Text + "'" + "," + "'" + (rblGender.SelectedItem.Text == "Male" ? "Male" : "Female") + "'" + ");SELECT SCOPE_IDENTITY()";
try
{
Conn.Open();
//obj = cmd.ExecuteScalar();
int c_id = Convert.ToInt32(cmd.ExecuteScalar());
if (c_id > 0)
{
InsertProduct(c_id);
lblMsg.Text = "Insert Successfull";
}
else
lblMsg.Text = "Customer insert fail";
Clear();
ClearProduct();
ViewState["dtProduct"] = null;
GVProduct.DataSource = null;
GVProduct.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
cmd = null;
Conn.Close();
}
}
else
{
lblMsg.Text = "Please Enter Product details";
}
}
public void InsertProduct(int CustomerId)
{
DataTable dt = (DataTable)ViewState["dtProduct"];
StringBuilder sbSql = new StringBuilder();
sbSql.Append(@"INSERT INTO ProductDetails(CustomerId,ProductName,Quantity,Price,Amount) VALUES");
// Response.Write("Outer Looop" + i.ToString() + "<br/><br/>");
for (int i = 0; i < dt.Rows.Count; i++)
{
// Response.Write("Inner Looop" + j.ToString() + "<br/>");
string values = string.Format("({0},{1},{2},{3},{4}),",
new string[] { Convert.ToString(CustomerId), "'" + dt.Rows[i]["Product"] + "'", "'" + dt.Rows[i]["Quantity"] + "'", "'" + dt.Rows[i]["Price"] + "'", "'" + dt.Rows[i]["Amount"] + "'" });
sbSql.Append(values);
}
string sql = sbSql.ToString();
sql = sql.TrimEnd(',');
//SqlConnection con = new SqlConnection();
//Conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
cmd.CommandText = sql;
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
sbSql.Clear();
}
protected void GVProduct_RowEditing(object sender, GridViewEditEventArgs e)
{
GVProduct.DataSource = (DataTable)ViewState["dtProduct"];
GVProduct.DataBind();
btnAdd.Text = "Update";
txtProductName.Text = GVProduct.Rows[e.NewEditIndex].Cells[2].Text;
txtQuantity.Text = GVProduct.Rows[e.NewEditIndex].Cells[3].Text;
txtPrice.Text = GVProduct.Rows[e.NewEditIndex].Cells[4].Text;
txtAmount.Text = GVProduct.Rows[e.NewEditIndex].Cells[5].Text;
GVProduct.Enabled = false;
ViewState["Index"] = e.NewEditIndex;
GVProduct.EditIndex = -1;
}
protected void GVProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVProduct.EditIndex = -1;
GVProduct.DataSource = ViewState["dtProduct"];
GVProduct.DataBind();
}
protected void GVProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = null;
if (ViewState["dtProduct"] != null)
dt = (DataTable)ViewState["dtProduct"];
dt.Rows.RemoveAt(e.RowIndex);
ViewState["dtProduct"] = dt;
GVProduct.DataSource = dt;
GVProduct.DataBind();
}
}
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script type="text/javascript">
function GetAmount()
{
var qty = document.getElementById('<%= txtQuantity.ClientID %>').value;
var Price = document.getElementById('<%= txtPrice.ClientID %>').value;
var Amount = (qty * Price);
document.getElementById('<%= txtAmount.ClientID %>').value = Amount;
}
</script>
<div>
<asp:Label ID="lblMsg" runat="server"></asp:Label>
<div>
<div>
Bill No:
</div>
<div>
<asp:TextBox ID="txtBillNo" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvBillNo" runat="server" ValidationGroup="Customer"
ControlToValidate="txtBillNo" ErrorMessage="<b>please enter bill no</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Customer Name:
</div>
<div>
<asp:TextBox ID="txtCustomerName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvCustomerName" runat="server" ValidationGroup="Customer"
ControlToValidate="txtCustomerName" ErrorMessage="<b>please enter CustomerName</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Address:
</div>
<div>
<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvAddress" runat="server" ValidationGroup="Customer"
ControlToValidate="txtAddress" ErrorMessage="<b>please enter Address</b>">
</asp:RequiredFieldValidator>
</div>
<div>
Gender:
</div>
<div>
<asp:RadioButtonList ID="rblGender" runat="server">
<asp:ListItem Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp:RadioButtonList>
</div>
</div>
<div style="border: 1px solid gray; height: 200px; padding: 5px">
<div style="width: 155px; float: left;">
<div>
Product Name:
</div>
<div>
<asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvProductName" runat="server" ValidationGroup="Product"
ControlToValidate="txtProductName" ErrorMessage="<b>please enter product</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Quantity:
</div>
<div>
<asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvQuantity" runat="server" ValidationGroup="Product"
ControlToValidate="txtQuantity" ErrorMessage="<b>please enter Quantity</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Price:
</div>
<div>
<asp:TextBox ID="txtPrice" runat="server" onblur = "GetAmount()"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPrice" runat="server" ValidationGroup="Product"
ControlToValidate="txtPrice" ErrorMessage="<b>please enter Price</b>">
</asp:RequiredFieldValidator>
</div>
</div>
<div style="width: 155px; float: left;">
<div>
Amount:
</div>
<div>
<asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
</div>
</div>
<div style="margin: 15px;">
<asp:Button ID="btnAdd" runat="server" ValidationGroup="Product" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" OnClick="btnClear_Click" />
</div>
<asp:GridView ID="GVProduct" runat="server" OnRowCancelingEdit="GVProduct_RowCancelingEdit"
OnRowDeleting="GVProduct_RowDeleting" OnRowEditing="GVProduct_RowEditing" >
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="true" />
<asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</div>
</div>
<div style="padding: 5px">
<asp:Button ID="btnSave" runat="server" ValidationGroup="Customer" Text="Save" OnClick="btnSave_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</div>
</asp:Content>
Default.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.SqlClient;
using System.Data;
using System.Configuration;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
SqlConnection Conn = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
CreateConnection();
}
public void CreateConnection()
{
Conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnString"].ToString();
}
protected void btnAdd_Click(object sender, EventArgs e)
{
DataTable dt = null;
if (ViewState["dtProduct"] != null)
dt = (DataTable)ViewState["dtProduct"];
else
{
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Product", typeof(string)),
new DataColumn("Quantity", typeof(double)),
new DataColumn("Price",typeof(double)),
new DataColumn("Amount",typeof(double))});
}
if (btnAdd.Text == "Add")
{
dt.Rows.Add(txtProductName.Text, txtQuantity.Text, txtPrice.Text, txtAmount.Text);
}
else
{
int i = Convert.ToInt32(ViewState["Index"]);
dt.Rows[i]["Product"] = txtProductName.Text;
dt.Rows[i]["Quantity"] = txtQuantity.Text;
dt.Rows[i]["Price"] = txtPrice.Text;
dt.Rows[i]["Amount"] = txtAmount.Text;
}
GVProduct.EditIndex = -1;
GVProduct.DataSource = dt;
GVProduct.DataBind();
ViewState["dtProduct"] = dt;
ViewState["Index"] = null;
btnAdd.Text = "Add";
GVProduct.Enabled = true;
ClearProduct();
}
public void ClearProduct()
{
txtProductName.Text = "";
txtQuantity.Text = "";
txtPrice.Text = "";
txtAmount.Text = "";
}
public void Clear()
{
txtBillNo.Text = "";
txtCustomerName.Text = "";
txtAddress.Text = "";
txtProductName.Text = "";
txtQuantity.Text = "";
txtPrice.Text = "";
txtAmount.Text = "";
ViewState["dtProduct"] = null;
}
protected void btnClear_Click(object sender, EventArgs e)
{
ClearProduct();
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (ViewState["dtProduct"] != null)
{
CreateConnection();
SqlCommand cmd = null;
object obj = null;
cmd = Conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into Customerdetail(BillNo,CustomerName,Address,Gender) values (" + txtBillNo.Text + "," + "'" + txtCustomerName.Text + "'" + "," + "'" + txtAddress.Text + "'" + "," + "'" + (rblGender.SelectedItem.Text == "Male" ? "Male" : "Female") + "'" + ");SELECT SCOPE_IDENTITY()";
try
{
Conn.Open();
//obj = cmd.ExecuteScalar();
int c_id = Convert.ToInt32(cmd.ExecuteScalar());
if (c_id > 0)
{
InsertProduct(c_id);
lblMsg.Text = "Insert Successfull";
}
else
lblMsg.Text = "Customer insert fail";
Clear();
ClearProduct();
ViewState["dtProduct"] = null;
GVProduct.DataSource = null;
GVProduct.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
cmd.Dispose();
cmd = null;
Conn.Close();
}
}
else
{
lblMsg.Text = "Please Enter Product details";
}
}
public void InsertProduct(int CustomerId)
{
DataTable dt = (DataTable)ViewState["dtProduct"];
StringBuilder sbSql = new StringBuilder();
sbSql.Append(@"INSERT INTO ProductDetails(CustomerId,ProductName,Quantity,Price,Amount) VALUES");
// Response.Write("Outer Looop" + i.ToString() + "<br/><br/>");
for (int i = 0; i < dt.Rows.Count; i++)
{
// Response.Write("Inner Looop" + j.ToString() + "<br/>");
string values = string.Format("({0},{1},{2},{3},{4}),",
new string[] { Convert.ToString(CustomerId), "'" + dt.Rows[i]["Product"] + "'", "'" + dt.Rows[i]["Quantity"] + "'", "'" + dt.Rows[i]["Price"] + "'", "'" + dt.Rows[i]["Amount"] + "'" });
sbSql.Append(values);
}
string sql = sbSql.ToString();
sql = sql.TrimEnd(',');
//SqlConnection con = new SqlConnection();
//Conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = Conn;
cmd.CommandText = sql;
cmd.CommandType = System.Data.CommandType.Text;
cmd.ExecuteNonQuery();
sbSql.Clear();
}
protected void GVProduct_RowEditing(object sender, GridViewEditEventArgs e)
{
GVProduct.DataSource = (DataTable)ViewState["dtProduct"];
GVProduct.DataBind();
btnAdd.Text = "Update";
txtProductName.Text = GVProduct.Rows[e.NewEditIndex].Cells[2].Text;
txtQuantity.Text = GVProduct.Rows[e.NewEditIndex].Cells[3].Text;
txtPrice.Text = GVProduct.Rows[e.NewEditIndex].Cells[4].Text;
txtAmount.Text = GVProduct.Rows[e.NewEditIndex].Cells[5].Text;
GVProduct.Enabled = false;
ViewState["Index"] = e.NewEditIndex;
GVProduct.EditIndex = -1;
}
protected void GVProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GVProduct.EditIndex = -1;
GVProduct.DataSource = ViewState["dtProduct"];
GVProduct.DataBind();
}
protected void GVProduct_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dt = null;
if (ViewState["dtProduct"] != null)
dt = (DataTable)ViewState["dtProduct"];
dt.Rows.RemoveAt(e.RowIndex);
ViewState["dtProduct"] = dt;
GVProduct.DataSource = dt;
GVProduct.DataBind();
}
}
No comments:
Post a Comment