Click here to Skip to main content
15,905,686 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to delete row from transaction table without posting page to the server in C#?
C#
//this is a sales.aspx file:

<asp:GridView ID="gvproductinfo" runat="server">
                <Columns>
                    <asp:TemplateField HeaderText="Cancel">
                        <ItemTemplate>
                            <asp:CheckBox ID="chk1" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Quantity">
                        <ItemTemplate>
                            <asp:TextBox ID="txtquantity" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Price">
                        <ItemTemplate>
                            <asp:TextBox ID="txtprice" runat="server"></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Commitement Date">
                        <ItemTemplate>
                            <asp:TextBox ID="txtcommitementdate" runat="server" style="margin-top: 0px"></asp:TextBox>
                            <asp:CalendarExtender ID="CalendarExtender1" runat="server" 
                                PopupButtonID="txtcommitementdate" TargetControlID="txtcommitementdate" 
                                Format="MM/dd/yyyy">
                            </asp:CalendarExtender>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
    </asp:GridView>

C#
//this is a sales.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;



public partial class salesordertrancation : System.Web.UI.Page
{
    DataTable dt;
    SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
           
            loaddatatogrid();
        }
        dt = new DataTable("MyTable");
        dt.Columns.Add("ProductCode");
        dt.Columns.Add("ProductName");
    }
     
    protected void loaddatatogrid()
    {

        if (cn.State == ConnectionState.Closed)
            cn.Open();
        SqlCommand cm = new SqlCommand("select CustomerCode,CustomerName from customer ", cn);
        SqlDataReader dr = cm.ExecuteReader();
        gvcustomer.DataSource = dr;
        gvcustomer.DataBind();
        cn.Close();
    }
    protected void gvcustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvcustomer.SelectedRow;
        txtcustomercode.Text = row.Cells[1].Text;
        txtcustomername.Text = row.Cells[2].Text; 
    }
     protected  void databound()
    {
        if (cn.State == ConnectionState.Closed)
            cn.Open();
        SqlCommand cm = new SqlCommand("select ProductCode,ProductName from product ", cn);
        SqlDataReader dr = cm.ExecuteReader();
        if (dr.HasRows)
        {
            gvproduct.DataSource = dr;
            gvproduct.DataBind();
            
        }
        dr.Close();
        cn.Close();
    }
    protected void btnshowproducts_Click(object sender, EventArgs e)
    {
        databound();
        btnadd.Visible = true;
    }
    

    protected void btnAddproducts_Click1(object sender, EventArgs e)
    {
        SqlTransaction trans = null;
        try
        {
            if (cn.State == ConnectionState.Closed)
                cn.Open();

            trans = cn.BeginTransaction();
            int cnt;
            
            SqlCommand cm = new SqlCommand("select max(SalesOrderCode) from salesorder", cn);
            cm.Transaction = trans;
            if (cm.ExecuteScalar().ToString() == "")
            {
                cnt = 1;
            }
            else
            {
                cnt = (int)cm.ExecuteScalar();
                cnt = cnt + 1;
            }
            cm = new SqlCommand("insert into salesorder values(@p1,@p2,@p3,@p4) ", cn);
            cm.Transaction = trans;
            cm.Parameters.Add("@p1", SqlDbType.Int).Value = cnt;
            cm.Parameters.Add("@p2", SqlDbType.DateTime).Value = Convert.ToDateTime(txtdate.Text);
            cm.Parameters.Add("@p3", SqlDbType.Int).Value = Convert.ToInt16(txtcustomercode.Text);
            cm.Parameters.Add("@p4", SqlDbType.VarChar).Value = txtremarks.Text;
            cm.ExecuteNonQuery();
            
            for (int i = 0; i < gvproductinfo.Rows.Count; i++)
            {            
                GridViewRow row = gvproductinfo.Rows[i];

                TextBox t1 = (TextBox)row.FindControl("txtquantity");
                TextBox t2 = (TextBox)row.FindControl("txtprice");
                TextBox t3 = (TextBox)row.FindControl("txtcommitementdate");

                cm = new SqlCommand("insert into salesordertran(SalesOrderCode,ProductCode,SalesOrderTranQty,SalesOrderTranPrice,SalesOrderCommDate) values(@p1,@p2,@p3,@p4,@p5)", cn);
                cm.Transaction = trans;
                cm.Parameters.Add("@p1", SqlDbType.Int).Value = cnt;
                cm.Parameters.Add("@p2", SqlDbType.Int).Value = Convert.ToInt16(gvproduct.Rows[i].Cells[1].Text);
                cm.Parameters.Add("@p3", SqlDbType.Int).Value = Convert.ToInt16(t1.Text);
                cm.Parameters.Add("@p4", SqlDbType.Int).Value = Convert.ToInt16(t2.Text);
                cm.Parameters.Add("@p5", SqlDbType.DateTime).Value = Convert.ToDateTime(t3.Text);
                //cm2.Parameters.Add("@p6", SqlDbType.Bit).Value = Convert.ToBoolean(gvproduct.Rows[j].Cells[7].Text);
                //cm2.Parameters.Add("@p7", SqlDbType.DateTime).Value = Convert.ToDateTime(gvproduct.Rows[j].Cells[5].Text);
                //cm2.Parameters.Add("@p8", SqlDbType.Bit).Value = Convert.ToBoolean(gvproduct.Rows[j].Cells[6].Text);
                //cm2.Parameters.Add("@p9", SqlDbType.DateTime).Value = Convert.ToDateTime(gvproduct.Rows[j].Cells[7].Text);
                cm.ExecuteNonQuery();
            }
            trans.Commit();
            Label1.Text = "Order is Placed Successfully!!!!!!";
            ClientScript.RegisterStartupScript(this.GetType(), "Msg", "alert('Production process completed!!');", true);

            cn.Close();
            
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            trans.Rollback();
            cn.Close();
        }

        
    }

    protected void btnadd_Click(object sender, EventArgs e)
    {
        try
        {
            for (int i = 0; i < gvproduct.Rows.Count; i++)
            {
                GridViewRow row = gvproduct.Rows[i];
                CheckBox c1 = (CheckBox)row.FindControl("chk");
                if (c1.Checked == true)
                {
                    DataRow drow = dt.NewRow();
                    drow["ProductCode"] = row.Cells[1].Text;
                    drow["ProductName"] = row.Cells[2].Text;
                    dt.Rows.Add(drow);
                    gvproductinfo.DataSource = dt;
                    gvproductinfo.DataBind();

                    
                }
            }
            btnAddproducts.Visible = true;
        }

        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    
}

Now my problem is:

if by mistake, I select a product in product gridview.

And then clik on add button.

So I hav a product information in productinfo gridview.

But now i want to delete row from productinfo gridview.

And before deletion the alertbox has to b pop up.

So what should I do?
Posted
Updated 14-Mar-12 14:04pm
v2
Comments
Dave Kreskowiak 14-Mar-12 13:55pm    
So how do you think you're going to tell your ASP.NET code, which runs ONYL on the server, to delete the record without a postback of some kind?
R. Giskard Reventlov 14-Mar-12 18:20pm    
Where's that 5 button! :-)

1 solution

There are many ways to accomplish this. Take a look at the following code project article, for using WCF from Javascript.

A beginner’s guide for consuming a WCF service in JavaScript using ASP.NET AJAX[^]

Option 2: would be to use ASP.Net MVC with delete action.
Option 3: If you dont want to use WCF or MVC, following article is one of the better options

CRUD Operation in ASP.NET Web Applications Using HTTP Handler and jQuery[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900