Solved

If DataTable Has Rows

Posted on 2004-09-24
5
1,463 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
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 100 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

Suggested Solutions

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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