1 Replies - 3564 Views - Last Post: 06 December 2012 - 07:57 AM

#1 adrianmcg  Icon User is offline

  • New D.I.C Head

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

ASP.net c# Getting IndexOutOfRangeException. delete button for SQL

Posted 06 December 2012 - 07:41 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 ok.

The error flags up in on line where bold

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);

            [b]DataTable dt = ds.Tables[0];
[/b]
            return dt;
        }

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

            return cmd.ExecuteNonQuery();
        }

    }



The query works great when entered directly into SQL, (deleting the latest record where userID and product is specified)
When debugged the values in the query look good.

Just getting IndexOutOfRangeException and Cannot find table 0. This is when the user is logged in.

Is This A Good Question/Topic? 0
  • +

Replies To: ASP.net c# Getting IndexOutOfRangeException. delete button for SQL

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9089
  • View blog
  • Posts: 34,141
  • Joined: 12-June 08

Re: ASP.net c# Getting IndexOutOfRangeException. delete button for SQL

Posted 06 December 2012 - 07:57 AM

Do not create duplicate topics. Closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1