Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello
Into my asp.net application there is a Listview called ( pendingorderLV ) shown the pending orders of the customer and it include a delete button to delete the orders, what i am looking for is how i can make the listview refresh it self when the user click on each delete button and show the remain orders.

the listview behind code where user can reach it from a button into my application called: gotovieworder


C#
protected void gotovieworder_Click(object sender, EventArgs e)
    {


        MultiView1.ActiveViewIndex = 7;

        if (Session["UsrNme"] != null)
        {
            var user = Session["UsrNme"];
            using (var UsOrderCon = new SqlConnection(sc))
            {
                UsOrderCon.Open();

                string chksUsOrderstring = "Select count (*) from ShoppingCart where UID=@chkUID";

                SqlCommand ChkUsOrderCMD = new SqlCommand(chksUsOrderstring, UsOrderCon);

                ChkUsOrderCMD.Parameters.AddWithValue("@chkUID", user);

                var orderExists = (Int32)ChkUsOrderCMD.ExecuteScalar() > 0;

                if (orderExists)
                {

                    pendingorderpanel.Visible = true;
                    SqlDataAdapter UsOrderADPA = new SqlDataAdapter("SELECT [UID], [Product], [DateAdded], [PayMeth], [ProdDesc], [CartID] FROM [ShoppingCart] WHERE  [UID] = @uSer ", sc);

                    UsOrderADPA.SelectCommand.Parameters.AddWithValue("@uSer", Convert.ToString(Session["UsrNme"]));

                    DataSet UsOrderDST = new DataSet();
                    UsOrderADPA.Fill(UsOrderDST);
                    pendingorderLV.DataSource = UsOrderDST.Tables[0];
                    pendingorderLV.DataBind();
                    pendingorderpanel.Visible = true;


                }
                else
                {
                    UsexcOrderpanel.Visible = true;
                    UsOrderlbl.Text = "You dont have any orders, if you would like to become a premium user";
                }
            }
        }
    }


And the delete button inside the listview behind code is:

C#
protected void deltPendOrder_Command(object sender, CommandEventArgs e)
    {
        using (SqlConnection DeltPndOrdSQLCon = new SqlConnection(sc))
        {
            int OrdPendID = Convert.ToInt32(e.CommandArgument);

            System.Data.SqlClient.SqlCommand DeltPendOrdcmd = new System.Data.SqlClient.SqlCommand();
            DeltPendOrdcmd.CommandType = System.Data.CommandType.Text;

            DeltPendOrdcmd.CommandText = "DELETE FROM ShoppingCart WHERE CartID = @CartID";

            DeltPendOrdcmd.Parameters.AddWithValue("@CartID", OrdPendID);
            DeltPendOrdcmd.Connection = DeltPndOrdSQLCon;
            DeltPndOrdSQLCon.Open();

            DeltPendOrdcmd.ExecuteNonQuery();
            DeltPndOrdSQLCon.Close();
            viewmsgView.Visible = true;
        }


    }
Posted
Comments
jgakenhe 1-Aug-15 0:31am    
In your Delete, you'll need to do your bind again and call the database, just like you did in your gotovieworder_Click. To miminize the code, you may want to pull that database call and databind into another method and then call the method in the click event and delete event.
Member 10690878 1-Aug-15 1:11am    
hi @jgakenhe many thanks for your advice, but i am not so prof with c# i will be thanks if you explain to me in details with code how i can do that, many people told me that i need to rebind the gotovieworder_click but i dont know how.

1 solution

//This should be close. I can't test it because I don't have your datasource.
//I took out the call to the database and stuck it in its own method.
//Then after you delete, you refresh the grid with fresh data.
protected void gotovieworder_Click(object sender, EventArgs e)
{
	MultiView1.ActiveViewIndex = 7;

	if (Session["UsrNme"] != null)
	{
		var user = Session["UsrNme"];
		using (var UsOrderCon = new SqlConnection(sc))
		{
			UsOrderCon.Open();

			string chksUsOrderstring = "Select count (*) from ShoppingCart where UID=@chkUID";

			SqlCommand ChkUsOrderCMD = new SqlCommand(chksUsOrderstring, UsOrderCon);

			ChkUsOrderCMD.Parameters.AddWithValue("@chkUID", user);

			var orderExists = (Int32)ChkUsOrderCMD.ExecuteScalar() > 0;

			if (orderExists)
			{
				BindData();
				pendingorderpanel.Visible = true;
			}
			else
			{
				UsexcOrderpanel.Visible = true;
				UsOrderlbl.Text = "You dont have any orders, if you would like to become a premium user";
			}
		}
	}
}
protected void deltPendOrder_Command(object sender, CommandEventArgs e)
{
	using (SqlConnection DeltPndOrdSQLCon = new SqlConnection(sc))
	{
		int OrdPendID = Convert.ToInt32(e.CommandArgument);

		System.Data.SqlClient.SqlCommand DeltPendOrdcmd = new System.Data.SqlClient.SqlCommand();
		DeltPendOrdcmd.CommandType = System.Data.CommandType.Text;

		DeltPendOrdcmd.CommandText = "DELETE FROM ShoppingCart WHERE CartID = @CartID";

		DeltPendOrdcmd.Parameters.AddWithValue("@CartID", OrdPendID);
		DeltPendOrdcmd.Connection = DeltPndOrdSQLCon;
		DeltPndOrdSQLCon.Open();

		DeltPendOrdcmd.ExecuteNonQuery();
		DeltPndOrdSQLCon.Close();
		viewmsgView.Visible = true;
	}
	BindData();
}

protected void BindData()
{
	SqlDataAdapter UsOrderADPA = new SqlDataAdapter("SELECT [UID], [Product], [DateAdded], [PayMeth], [ProdDesc], [CartID] FROM [ShoppingCart] WHERE  [UID] = @uSer ", sc);

	UsOrderADPA.SelectCommand.Parameters.AddWithValue("@uSer", Convert.ToString(Session["UsrNme"]));

	DataSet UsOrderDST = new DataSet();
	UsOrderADPA.Fill(UsOrderDST);
	pendingorderLV.DataSource = UsOrderDST.Tables[0];
	pendingorderLV.DataBind();
}
 
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