Solved

If DataTable Has Rows

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error in JQuery 5 52
Need a starter for ETL protocol? 4 56
i have to take the screenshot of command prompt? how to do this? 1 37
Expression Evaluater 3 25
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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