3 Replies - 596 Views - Last Post: 06 December 2012 - 08:24 AM Rate Topic: -----

#1 adrianmcg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-December 12

ASP.net c# IndexOutOfRangeException Deleting latest record from SQL

Posted 06 December 2012 - 07:00 AM

I am getting an error where I am trying to delete a single row from the table in the database.

 Button btn = (Button)sender;
                int id = int.Parse(btn.CommandArgument);
                string session = Session["UserName"].ToString();

                string Query = "DELETE FROM ShoppingBasket WHERE Date IN (SELECT TOP 1 Date FROM shoppingBasket WHERE  ProductID = " + id + " AND UserID = '" + session  + "' ORDER BY Date DESC)";

                ConnectionHandler objHandler = new ConnectionHandler();
                DataTable dt = objHandler.ExecuteSelect(Query);





The values in the queries seem to be correct after debugging.

The error flags up in on line where bold

My DAL is for my Connection:

public class ConnectionHandler
{
SqlConnection cn;
void connect()
{
string cnstr = System.Configuration.ConfigurationManager.ConnectionStrings["ShoppingCart"].ConnectionString;
cn = new SqlConnection(cnstr);
cn.Open();
}
public DataTable ExecuteSelect(string Query)
{
connect();


SqlDataAdapter da = new SqlDataAdapter(Query, cn);

DataSet ds = new DataSet();

da.Fill(ds);

DataTable dt = ds.Tables[0];

return dt;
}

public int ExecuteStatement(string Query)
{
connect();
SqlCommand cmd = new SqlCommand(Query, cn);

return cmd.ExecuteNonQuery();
}

class Foo
{
public DataTable foo(string Query)
{
SqlConnection cn = default(SqlConnection);
DataTable dt = null;
cn.Open();
SqlDataAdapter da = new SqlDataAdapter(Query, cn);
da.Fill(dt);
cn.Close();
return dt;
}
}
}



Am getting Cannot find table 0. This is when the user is logged in.

The delete statement works as I want in SQL.

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.net c# IndexOutOfRangeException Deleting latest record from SQL

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: ASP.net c# IndexOutOfRangeException Deleting latest record from SQL

Posted 06 December 2012 - 07:54 AM

Quote

The error flags up in on line where bold

There is no bold inside code tags. Tell us the line number.

Index out of range:

Remember that all collections are zero-indexed.
10 items are indexed 0-9

So your problem arrises on the last item because you are trying to use index 10 for a collection of 10 items (as an example)

When dealing with collections you have to work up-to but not including the count.
for (int x = 0; x < myList.Count; x ++)

Notice how we only continue while x is less than, not less than or equal to, the list count.
Was This Post Helpful? 0
  • +
  • -

#3 adrianmcg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 06-December 12

Re: ASP.net c# IndexOutOfRangeException Deleting latest record from SQL

Posted 06 December 2012 - 08:14 AM

Line 21 is supposed to be bold of the second set of code.

The code which is named foo from line 34 also from the second set of code can be ignored.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,649
  • Joined: 02-June 10

Re: ASP.net c# IndexOutOfRangeException Deleting latest record from SQL

Posted 06 December 2012 - 08:24 AM

Quote

21 DataTable dt = ds.Tables[0];


You are making a huge assumption here that ds.Tables has any values.
You are trying to get the first element of the array without checking if there even *is* a first element.

If you were to put a breakpoint on this line and check the run-time value of ds.Tables you'd find there probably aren't any.

It is best to check such things before just calling them:

if (ds.Tables.Count > 0) 
{
   DataTabe dt = ds.Tables[0];
   // the rest of our method 
}
 else 
{
     MessageBox.Show("No tables", "Error"); 
}


Some tutorials that will help you debug:

What does this error message mean?

This post has been edited by tlhIn`toq: 06 December 2012 - 08:25 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1