Solved

If DataTable Has Rows

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

17 Experts available now in Live!

Get 1:1 Help Now