Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have this code and when i click a button i wanna delete the last row of the data table and save that table in a session. how can i do that?? this is my code..

public partial class Myreservations : System.Web.UI.Page
{


    DBAccess isusrobj = new DBAccess();


    protected void Page_Load(object sender, EventArgs e)
    {
        String email = isusrobj.setSessionVar();

        if (email != (String)Session["email"]) 
        {

            HyperLink choosemore = new HyperLink();
            ImageButton cancelDVD = new ImageButton();
            ImageButton BookDVDs = new ImageButton();

            choosemore.ImageUrl = "~/Images/more.gif";
            choosemore.NavigateUrl = "Movies.aspx";

            cancelDVD.ImageUrl = "~/Images/cancel.gif";
            cancelDVD.OnClientClick = cancelDVDs(); //this is where i wanna delete the rows

            BookDVDs.ImageUrl = "~/Images/bookdvds.gif";
            BookDVDs.OnClientClick = BookDVDsinDB();

            Panel1.Controls.Add(choosemore);
            Panel1.Controls.Add(cancelDVD);
            Panel1.Controls.Add(BookDVDs);


            String Mname = Request.QueryString["Vn"];
            String Myear = Request.QueryString["Vy"];
            String Mstars = Request.QueryString["Vs"];


            DataTable bookingtable = (DataTable)Session["table"];
            DataRow row = bookingtable.NewRow();
            row["Movie Name"] = Mname;
            row["Movie Year"] = Myear;
            row["Starring"] = Mstars;

            if (bookingtable.Rows.Count == 0)
            {

                bookingtable.Rows.Add(row);
                bookingtable.AcceptChanges();
                Reservations.DataSource = bookingtable;
                Reservations.DataBind();
                Session.Add("table", bookingtable);



            }
            else
            {
                bookingtable = (DataTable)Session["table"];
                bookingtable.Rows.Add(row);
                bookingtable.AcceptChanges();
                Session.Add("table", bookingtable);
                Reservations.DataSource = bookingtable;
                Reservations.DataBind();
            }



        }
        else
        {
            loginLabel.Text = "You have not logged in.Please log in to book the DVD!";
        }
    }

    private string BookDVDsinDB()
    {
  
        throw new NotImplementedException();
    }

    private string cancelDVDs()
    {

        throw new NotImplementedException();
    }
Posted

C#
bookingtable = (DataTable)Session["table"];
if(bookingtable.Rows.Count > 0) bookingtable.Rows(bookingtable.Rows.Count - 1).Delete();
bookingtable.AcceptChanges();
Session.Add("table", bookingtable);
Reservations.DataSource = bookingtable;
Reservations.DataBind();
 
Share this answer
 
Comments
Suresh Suthar 26-Aug-11 1:19am    
Nice answer.
aroshlakshan 26-Aug-11 1:42am    
bookingtable.Rows(bookingtable.Rows.Count - 1).Delete();
this dosen't work. it says .Rows cannot be used like a method.
To delete last row of DataTable use this..
[Alternate Solution]
C#
bookingtable.Rows.RemoveAt(bookingtable .Rows.Count-1]);
 
Share this answer
 
v2
Comments
aroshlakshan 26-Aug-11 1:43am    
i tried this and what happened is, it added the last row to the gridview every time i click the button instead of deleting
Suresh Suthar 26-Aug-11 1:54am    
Can you post your latest code?
Prerak Patel 26-Aug-11 1:45am    
My 5. I just forgot about Remove and RemoveAt.
aroshlakshan 26-Aug-11 2:05am    
this is the code. the reason for adding rows instead of deleting,
it adds the existing values in Vn,Vy,Vs to the table when i press the cancelDVD button because the page reloads. i need a solution to solve that. can you help??

public partial class Myreservations : System.Web.UI.Page
{


DBAccess isusrobj = new DBAccess();


protected void Page_Load(object sender, EventArgs e)
{
String email = isusrobj.setSessionVar();

if (email != (String)Session["email"]) //**********this is the problemo**************
{

HyperLink choosemore = new HyperLink();
ImageButton cancelDVD = new ImageButton();
ImageButton BookDVDs = new ImageButton();

choosemore.ImageUrl = "~/Images/more.gif";
choosemore.NavigateUrl = "Movies.aspx";

cancelDVD.ImageUrl = "~/Images/cancel.gif";
//cancelDVD.OnClientClick = cancelDVDs();

BookDVDs.ImageUrl = "~/Images/bookdvds.gif";
//BookDVDs.OnClientClick = BookDVDsinDB();

Panel1.Controls.Add(choosemore);
Panel1.Controls.Add(cancelDVD);
Panel1.Controls.Add(BookDVDs);


String Mname = Request.QueryString["Vn"];
String Myear = Request.QueryString["Vy"];
String Mstars = Request.QueryString["Vs"];


DataTable bookingtable = (DataTable)Session["table"];
DataRow row = bookingtable.NewRow();
row["Movie Name"] = Mname;
row["Movie Year"] = Myear;
row["Starring"] = Mstars;

if (bookingtable.Rows.Count == 0)
{

bookingtable.Rows.Add(row);
bookingtable.AcceptChanges();
Reservations.DataSource = bookingtable;
Reservations.DataBind();
Session.Add("table", bookingtable);



}
else
{
bookingtable = (DataTable)Session["table"];
bookingtable.Rows.Add(row);
bookingtable.AcceptChanges();
Session.Add("table", bookingtable);
Reservations.DataSource = bookingtable;
Reservations.DataBind();
}



}
else
{
loginLabel.Text = "You have not logged in.Please log in to book the DVD!";
}
}

private string BookDVDsinDB()
{

throw new NotImplementedException();
}

private string cancelDVDs()
{
DataTable deleteRows = (DataTable)Session["table"];
if (deleteRows.Rows.Count > 0)
{
deleteRows.Rows[deleteRows.Rows.Count - 1].Delete();
deleteRows.AcceptChanges();
Session.Add("table", deleteRows);
}
else
{
loginLabel.Text = "You haven't selected any DVDs to cancel!";
}
throw new NotImplementedException();
}

}

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