Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

If DataTable Has Rows

Posted on 2004-09-24
5
Medium Priority
?
1,488 Views
Last Modified: 2008-03-03
How do I write a HasRows for my DataTable?  If there is a row I want to perform a delete but if not, just write something.

I tried saying if e.Item.ItemIndex >-1 and it works until there are no rows then if i press refresh I get an error: "There is no row at position 0" so I think I should say if the table has any rows or not.  This is what I am trying to avoid.  if there are no rows it should just say "Your shopping cart is empty" instead of going into the if statment to try and do a Delete.

void DeleteItem(Object Sender,DataGridCommandEventArgs e)
{
       //---------Get DataTable from Session:
      
                tblCart=(DataTable)Session["dgCart"];

      //--------IF rows exist then do delete  ELSE just write "Your shopping cart is empty"
     //---------I'm trying to prevent error on refresh. If there are no rows it should only go to Response.Write but I'm getting an error now because it still enters if statement
      //--------SHOULD I TRY IF tblCart HAS ROWS HERE INSTEAD of  e.Item.ItemIndex?----
      
      if (e.Item.ItemIndex >-1)
          {
           tblCart.Rows[e.Item.ItemIndex].Delete();

           Session["dgCart"]=tblCart;
                     BindData();
                     GetTotals();
      }
                 else
                 {
              Response.Write("Your shopping cart is empty");
      }
}
0
Comment
Question by:champ_010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12146477
tblCart.Rows.Count - check to see if it is 0
0
 
LVL 9

Expert Comment

by:Rodney Helsens
ID: 12146478
if (tblCart.Rows.Count > 0)
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 400 total points
ID: 12146792
It looks like you want to see if a row in the grid exists in the cart datatable in Session state.  Let's assume the datagrid has a row that is the unique id for the record and that the table also has this column.  I'll call the column "UniqueIDColumn" for this example:

// i'm assuming the unique id is in first column of the grid; index 0.
string rowID = e.Item.Cells[0].Text

// the Select method of the datatable object returns a datarow array.
DataRow[] rows = tblCart.Select("UniqueIDColumn = " + rowID);

// if the row exists, the length of the array should be 1.
if (rows.Length == 1) rows[0].Delete();

John

0
 
LVL 1

Author Comment

by:champ_010
ID: 12147641
John,

You always have an amazing answer--not only do you show me some code but you also explain what I should be thinking about in a situation and why the answer you provide would make sense in the over all scheme of things. It's nice to get a piece of code that fixes my problem but it's a bonus when someone like you points out logistic issues that teach me to look at the over all picture to find my errors.  I wouldn't have thought about comparing my DataGrid with the DataTable in Session otherwise.
0
 
LVL 10

Expert Comment

by:jnhorst
ID: 12148023
Thanks for the kind comments...  Glad to help.

John
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

730 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