Solved

If DataTable Has Rows

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

623 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