Asked by:
want to update old data and add new data with its guid id and no repeated entry in the database?

Question
-
User-1026236167 posted
hello in this code i have few error 1) in demolistings there are listings of entry when clicked on one entry (edit)
button then its data and its related (guid) entry is filled to example page textbox and gridview at this stage if we
can add some new data in the textbox and click on addinlist button then new data after old entry data is display at
that time we clicked on save button look the code in the save button my requirement is i want to update this old entry
and add new entry data in the database on save button click
Note - the data should not apply twice old data should be update and new will be added with the related guid id on both
demolistings cs using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography; using System.Text; 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 Panel_demolistings : System.Web.UI.Page { SqlCommand cmd = new SqlCommand(); SqlConnection con = new SqlConnection(); string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString(); public void EstablishConnection(string storeprocedure) { con.ConnectionString = connection; cmd.Connection = con; cmd.Connection.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storeprocedure; } public void CloseConnection() { cmd.Connection.Close(); cmd.Connection.Dispose(); con.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GetSaleListings(); } } public void GetSaleListings() { SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_demo1", connection); adp.SelectCommand.CommandType = CommandType.Text; DataTable DT = new DataTable(); adp.Fill(DT); Gv1.DataSource = DT; Gv1.DataBind(); } } demolistings aspx <%@ Page Title="" Language="C#" MasterPageFile="~/Panel/StudentMaster.master" AutoEventWireup="true" CodeFile="demolistings.aspx.cs" Inherits="Panel_demolistings" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div class="row"> <div class="col-lg-12"> <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false" Width="100%" CssClass="table table-striped table-bordered table-hover" Font-Size="Small" HeaderStyle-BackColor="#343a40" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true"> <Columns> <asp:TemplateField HeaderText="Sr No"> <ItemTemplate> <%# Container.DataItemIndex+1 %> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="name" HeaderText="Name" /> <asp:BoundField DataField="class" HeaderText="Class" /> <asp:BoundField DataField="roll_no" HeaderText="Roll No" /> <asp:TemplateField HeaderText="Edit" > <ItemTemplate> <a href="example.aspx?id=<%# Eval("invoice") %>" >Edit</a> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </div> </asp:Content> example 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 Panel_example : System.Web.UI.Page { SqlCommand cmd = new SqlCommand(); SqlConnection con = new SqlConnection(); string connection = System.Configuration.ConfigurationManager.AppSettings["con"].ToString(); public void EstablishConnection(string storeprocedure) { con.ConnectionString = connection; cmd.Connection = con; cmd.Connection.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = storeprocedure; } public void CloseConnection() { cmd.Connection.Close(); cmd.Connection.Dispose(); con.Close(); } public void FillInformation(Guid invoice_id) { using (Entities ctx = new Entities()) { var cust = (from a in ctx.tbl_demo1 where a.invoice == invoice_id select a).FirstOrDefault(); if (cust != null) { TB_name.Text = cust.name; TB_age.Text = cust.@class; TB_father_name.Text = cust.roll_no; } } } public void FillViewState() { DataTable DT = new DataTable(); DT.Columns.Add("tbl_id"); DT.Columns.Add("name"); DT.Columns.Add("class"); DT.Columns.Add("roll_no"); // DT.Columns.Add("invoice_id"); ViewState["DT"] = DT; } public DataTable AddNewRow(DataTable DT) { DT.Rows.Add(DT.Rows.Count + 1, TB_name.Text, TB_age.Text, TB_father_name.Text); return DT; } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { FillViewState(); GetRecordID(); if (Request.QueryString["id"] != null) { Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); FillInformation(invoice_id); //BT_add.Text = "Update Record"; SqlDataAdapter adp = new SqlDataAdapter("select * from tbl_demo1 where tbl_demo1.invoice='" + invoice_id.ToString() + "'", connection); DataTable DT = new DataTable(); adp.Fill(DT); Gv1.DataSource = DT; Gv1.DataBind(); DataTable stu = ViewState["DT"] as DataTable; stu = DT; ViewState["DT"] = stu; } } } protected void BT_add_Click(object sender, EventArgs e) { DataTable DT = ViewState["DT"] as DataTable; if (Request.QueryString["id"] == null) { if (BT_add.Text == "Update Record") { int tbl_id = Convert.ToInt16(TB_ID.Text) - 1; DT.Rows[tbl_id]["name"] = TB_name.Text; DT.Rows[tbl_id]["class"] = TB_age.Text; DT.Rows[tbl_id]["roll_no"] = TB_father_name.Text; //DT.Rows[tbl_id]["invoice_id"] = TB_invoice_id.Text; BT_add.Text = "Add In List"; } else { DT = AddNewRow(DT); } Gv1.DataSource = DT; Gv1.DataBind(); ViewState["DT"] = DT; GetRecordID(); } if (Request.QueryString["id"] != null) { if (BT_add.Text == "Update Record") { foreach (GridViewRow row in Gv1.Rows) { //decimal invoice_id = Convert.ToDecimal(Gv1.DataKeys[row.RowIndex].Values["tbl_id"].ToString()); Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); using (Entities ctx = new Entities()) { var package = (from c in ctx.tbl_demo1 where c.invoice == invoice_id select c).FirstOrDefault(); package.name = TB_name.Text; package.@class = TB_age.Text; package.roll_no = TB_father_name.Text; ctx.SaveChanges(); } } } else { DT = AddNewRow(DT); } Gv1.DataSource = DT; Gv1.DataBind(); ViewState["DT"] = DT; GetRecordID(); GetRecordID(); } } protected void BT_save_Click(object sender, EventArgs e) { if (Request.QueryString["id"] == null) { int status = 0; Guid invoice_id = Guid.NewGuid(); foreach (GridViewRow row in Gv1.Rows) { using (Entities ctx = new Entities()) { ctx.tbl_demo1.Add(new tbl_demo1() { name = (Gv1.DataKeys[row.RowIndex].Values["name"].ToString()), @class = (Gv1.DataKeys[row.RowIndex].Values["class"].ToString()), roll_no = (Gv1.DataKeys[row.RowIndex].Values["roll_no"].ToString()), invoice = invoice_id, }); status = ctx.SaveChanges(); } } } if (Request.QueryString["id"] != null) { int status = 0; Guid invoice_id = new Guid(Request.QueryString["id"].ToString()); foreach (GridViewRow row in Gv1.Rows) { decimal tbl_id = Convert.ToDecimal(Gv1.DataKeys[row.RowIndex].Values["tbl_id"].ToString()); using (Entities ctx = new Entities()) { var result = ctx.tbl_demo1.SingleOrDefault(b => b.tbl_id == tbl_id); if (result != null) { result.name = TB_name.Text; result.@class = TB_age.Text; result.roll_no = TB_father_name.Text; result.invoice = invoice_id; } status = ctx.SaveChanges(); } } Guid invoice_id1 = new Guid(Request.QueryString["id"].ToString()); foreach (GridViewRow row in Gv1.Rows) { using (Entities ctx = new Entities()) { ctx.tbl_demo1.Add(new tbl_demo1() { name = (Gv1.DataKeys[row.RowIndex].Values["name"].ToString()), @class = (Gv1.DataKeys[row.RowIndex].Values["class"].ToString()), roll_no = (Gv1.DataKeys[row.RowIndex].Values["roll_no"].ToString()), invoice = invoice_id1, }); status = ctx.SaveChanges(); } } } } protected void LB_edit_Click(object sender, EventArgs e) { LinkButton link = (LinkButton)sender; GridViewRow row = (GridViewRow)(link.Parent.Parent); DataTable DT = ViewState["DT"] as DataTable; TB_ID.Text = DT.Rows[row.RowIndex]["tbl_id"].ToString(); TB_name.Text = DT.Rows[row.RowIndex]["name"].ToString(); TB_age.Text = DT.Rows[row.RowIndex]["class"].ToString(); TB_father_name.Text = DT.Rows[row.RowIndex]["roll_no"].ToString(); BT_add.Text = "Update Record"; } public void GetRecordID() { DataTable DT = ViewState["DT"] as DataTable; TB_ID.Text = Convert.ToString(DT.Rows.Count + 1); } protected void LB_remove_Click(object sender, EventArgs e) { if (Request.QueryString["id"] == null) { LinkButton link = (LinkButton)sender; GridViewRow row = (GridViewRow)(link.Parent.Parent); DataTable DT = ViewState["DT"] as DataTable; DT.Rows.RemoveAt(row.RowIndex); for (int i = 0; i < DT.Rows.Count; i++) { DT.Rows[i]["tbl_id"] = i + 1; } Gv1.DataSource = DT; Gv1.DataBind(); ViewState["DT"] = DT; } if (Request.QueryString["id"] != null) { LinkButton link = (LinkButton)sender; GridViewRow row = (GridViewRow)(link.Parent.Parent); DataTable stu = ViewState["DT"] as DataTable; { stu.Rows.RemoveAt(row.RowIndex); for (int i = 0; i < stu.Rows.Count; i++) { stu.Rows[i]["tbl_id"] = i + 1; } decimal tbl_id = Convert.ToDecimal(Gv1.DataKeys[row.RowIndex].Values["tbl_id"].ToString()); using (Entities ctx = new Entities()) { tbl_demo1 customer = (from c in ctx.tbl_demo1 where c.tbl_id == tbl_id select c).FirstOrDefault(); ctx.Entry(customer).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } Gv1.DataSource = stu; Gv1.DataBind(); ViewState["DT"] = stu; } } } } example aspx <%@ Page Title="" Language="C#" MasterPageFile="~/Panel/StudentMaster.master" AutoEventWireup="true" CodeFile="example.aspx.cs" Inherits="Panel_example" %> <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> <div> <asp:HiddenField ID="HiddenField1" runat="server" /> <table> <tr> <td>Record ID</td> <td><asp:TextBox ID="TB_ID" runat="server" Enabled="false" /></td> </tr> <tr> <td>Name</td> <td><asp:TextBox ID="TB_name" runat="server" /></td> </tr> <tr> <td>Age</td> <td><asp:TextBox ID="TB_age" runat="server" /></td> </tr> <tr> <td>Father Name</td> <td><asp:TextBox ID="TB_father_name" runat="server" /></td> </tr> <tr> <td>Invoice ID</td> <td><asp:TextBox ID="TB_invoice_id" runat="server" /></td> </tr> <tr> <td><asp:Button ID="BT_add" runat="server" Text="Add In List" OnClick="BT_add_Click" /></td> <%-- <td><asp:Button ID="tb_update" runat="server" Text="Add In List" OnClick="bt_update_click" /></td>--%> <td><asp:Button ID="BT_save" runat="server" Text="Save Record" OnClick="BT_save_Click" /></td> </tr> </table> <asp:GridView ID="Gv1" runat="server" DataKeyNames="tbl_id, name, class, roll_no" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="tbl_id" HeaderText="tbl_id" /> <asp:BoundField DataField="name" HeaderText="name" /> <asp:BoundField DataField="class" HeaderText="class" /> <asp:BoundField DataField="roll_no" HeaderText="rollno" /> <%-- <asp:BoundField DataField="invoice" HeaderText="invoice_id" />--%> <asp:TemplateField HeaderText="Edit"> <ItemTemplate> <asp:LinkButton ID="LB_edit" runat="server" Text="Edit" OnClick="LB_edit_Click" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Remove"> <ItemTemplate> <asp:LinkButton ID="LB_remove" runat="server" Text="Delete" OnClick="LB_remove_Click" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </asp:Content>Monday, November 30, 2020 6:55 AM
All replies
-
User475983607 posted
There are a lot of problems with the design, it is over engineered. I recommend dropping the ViewState cache and simply store the data in the database. That will fix most if not all the logical syncing issues.
Monday, November 30, 2020 11:03 AM