Solved

How to delete a row from a dataTable and adjust a cost

Posted on 2009-04-08
2
658 Views
Last Modified: 2013-11-26
Hello,
 I am working on a basic shopping cart that will allow users to register for an event. I have built the cart using a dataTable and I have been able to add the users as well as calculate a running total. I am having difficulty though figuring out how to delete a row from the shopping cart. If a user decides they want to remove an attendee I want them to be able to click on a remove link in my datagrid and remove the user. This function should remove the row as well as adjust the total cost.
I have built a similar page in vb.net but I am having trouble translating the code over to C#.net  When I try to call the dataTable from another function I get an error that the dataTable name does not exist in the current context.

Can you please help me understand what I am missing and how to correct this so that I am able to remove items from my shopping cart.
Thanks in advance,
//create shopping cart

    private void DataTable()

    {

        DataTable MyDT = new DataTable();

        DataRow MyRow;
 

        if (ViewState["DataTable"] == null)

        {

            MyDT.Columns.Add("regID", System.Type.GetType("System.Int32"));

            MyDT.Columns.Add("Fname");

            MyDT.Columns.Add("Lname");

            MyDT.Columns.Add("Title");

            MyDT.Columns.Add("Agency");

            MyDT.Columns.Add("Email");

            MyDT.Columns.Add("Shirt");

            MyDT.Columns.Add("FstAtt");

            MyDT.Columns.Add("Price", typeof(Decimal));

            MyRow = MyDT.NewRow();

            MyRow[0] = MyDT.Rows.Count + 1;

            MyRow[1] = TxtFName.Text;

            MyRow[2] = TxtLName.Text;

            MyRow[3] = TxtTitle.Text;

            MyRow[4] = TxtAgency.Text;

            MyRow[5] = TxtEmail.Text;

            MyRow[6] = ddShirt.SelectedItem.Text;

            MyRow[7] = ddAttend.SelectedItem.Text;

            MyRow[8] = 100; //current rate for User Conference registration

            MyDT.Rows.Add(MyRow);

            //clear fields

            TxtFName.Text = "";

            TxtLName.Text = "";

            TxtTitle.Text = "";

            TxtAgency.Text = "";

            TxtEmail.Text = "";

            ddShirt.SelectedIndex = -1;

            ddAttend.SelectedIndex = -1;

            //Calculate Total for Shopping Cart

            Decimal Total = 0.00M;

            foreach (DataRow Row in MyDT.Rows)

            {

                Total += (Decimal)Row["Price"];

            }

            runTotalLBL.Text = Total.ToString();

        }

        else

        {

            MyDT = (DataTable)ViewState["DataTable"];

            MyRow = MyDT.NewRow();

            MyRow[0] = MyDT.Rows.Count + 1;

            MyRow[1] = TxtFName.Text;

            MyRow[2] = TxtLName.Text;

            MyRow[3] = TxtTitle.Text;

            MyRow[4] = TxtAgency.Text;

            MyRow[5] = TxtEmail.Text;

            MyRow[6] = ddShirt.SelectedItem.Text;

            MyRow[7] = ddAttend.SelectedItem.Text;

            MyRow[8] = 100;

            MyDT.Rows.Add(MyRow);

            //clear fields after each registrant is entered

            TxtFName.Text = "";

            TxtLName.Text = "";

            TxtTitle.Text = "";

            TxtAgency.Text = "";

            TxtEmail.Text = "";

            ddShirt.SelectedIndex = -1;

            ddAttend.SelectedIndex = -1;

            //Calculate Total for Shopping Cart

            Decimal Total = 0.0M;

            foreach (DataRow Row in MyDT.Rows)

            {

                Total += (Decimal)Row["Price"];

            }

            runTotalLBL.Text = Total.ToString();

        }

        ViewState["DataTable"] = MyDT;

        //bind datatable to datagrid

        dg.DataSource = MyDT;

        dg.DataBind();

    }
 

    //add attendees to roster datagrid

    protected void AddToCart(object sender, EventArgs e)

    {

        DataTable();

    }
 

    //remove attendee from data grid

    protected void Delete_Item(object source, DataGridCommandEventArgs e)

    {

       //Here is where I get stuck. In vb.net the datatable was recognized across functions however here it only seems to be accessible within the function "DataTable"

        ViewState["DataTable"] = MyDT;
 
 
 

    }

Open in new window

0
Comment
Question by:rlcrews
2 Comments
 
LVL 14

Accepted Solution

by:
jjardine earned 250 total points
ID: 24102279
It i sbecause MyDT is only declared in the function DataTable.    YOu don't have it declared in Delete_Item.   You would have to declare it outside of the DataTable function or declare it again in Delete_Item.  It looks like you are doing this backwards though.   Wouldn't you want to set MyDT = the Veiwstate version of the datatable to work with it?
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 24105865
//Here is where I get stuck. In vb.net the datatable was recognized across functions however here it only seems to be accessible within the function "DataTable"

This statement is not valid. Even in VB, you could not access the variable outside of the function in which you declare it.

Try this code. I hope you could convert it into C#


Dim MyDT as DataTable = ViewState["DataTable"] = MyDT

if not isnothing(myDT) then

   myDT.DefaultView.RowFilter = "ItemID = " & ItemID 

   if MyDT.DefaultView.Count = 1 Then

      TotalAmount = TotalAmount - myDT.DefaultView.Item(0).Item("Amount")

      myDT.DefaultView.Item(0).Delete

   End if

   MyDT.DefaultView.Rowfilter = ""

end if

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Vb.net dynamic formulas in runtime 11 61
Data is not showing from images 15 36
Filename to be appended with DateTimeof Download 3 40
C# parent child form 5 13
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now