Unable to cast object of type for adding data into access database

Last post 08-28-2008 3:57 AM by photossed. 1 replies.

Sort Posts:

  • Unable to cast object of type for adding data into access database

    08-27-2008, 11:30 PM
    • Member
      5 point Member
    • photossed
    • Member since 06-16-2008, 2:51 PM
    • Posts 43

     

     

    In my addListToDB() function, I try to save the entry.Key and entry.Value into my DB.

    It works fine with entry.Key, but when it comes to entry.Value, it appears "Unable to cast object of type 'CartItem' to type 'System.IConvertible'."

    It is a invaludcastexception. But, pID, oID and Quantity are all "Numbers" which is integer....

    Thanks alot.

     

    public partial class order_Cart : System.Web.UI.Page
    {
        // Access Database oledb connection string
        // Using Provider Microsoft.Jet.OLEDB.4.0
        String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/db.mdb");

        // Object created for Oledb Connection
        OleDbConnection myAccessConnection;

        private SortedList cart;
        private int orderID;

        protected void Page_Load(object sender, EventArgs e)
        {
            myAccessConnection = new OleDbConnection(connStr);

            this.GetCart();
            if (!IsPostBack)
                this.DisplayCart();
        }
        
        protected void btnRemove_Click(object sender, EventArgs e)
        {
            if (lstCart.SelectedIndex > -1 && cart.Count > 0)
            {
                cart.RemoveAt(lstCart.SelectedIndex);
                this.DisplayCart();
            }
        }
        
        protected void btnEmpty_Click(object sender, EventArgs e)
        {
            cart.Clear();
            lstCart.Items.Clear();
        }
        
        protected void btnCheckOut_Click(object sender, EventArgs e)
        {
            this.GetCart();
            if (cart.Count > 0 && this.txtEmail.Text != "" && this.txtName.Text != "" && this.txtPhone.Text != "")
            {
                AddUserToDB();
                AddListToDB();
                Response.Redirect("Order_Success.aspx");
                Session.Clear();
            }
            else
            {
                lblError.Visible = true;
                lblError.Text = "請輸入您的稱呼以及聯絡方式!";
            }
        }

        private void GetCart()
        {
            if (Session["Cart"] == null)
                Session.Add("Cart", new SortedList());
            cart = (SortedList)Session["Cart"];
        }

        private void DisplayCart()
        {
            lstCart.Items.Clear();
            CartItem item;
            foreach (DictionaryEntry entry in cart)
            {
                item = (CartItem)entry.Value;
                lstCart.Items.Add(item.Display());
            }
        }

        private void AddListToDB()
        {
            OleDbCommand myCommand = new OleDbCommand("INSERT INTO tblOrderDetail(oID, pID, Quantity) VALUES (@oID ,@pID, @Quantity)", myAccessConnection);

            foreach (DictionaryEntry entry in cart)
            {
                myCommand.CommandType = CommandType.Text;

                // Add Parameters to Command
                OleDbParameter oID = new OleDbParameter("@oID", OleDbType.Integer);
                oID.Value = orderID;
                myCommand.Parameters.Add(oID);

                OleDbParameter pID = new OleDbParameter("@pID", OleDbType.Integer);
                pID.Value = Convert.ToInt32(entry.Key);
                myCommand.Parameters.Add(pID);

                OleDbParameter Quantity = new OleDbParameter("@Quantity", OleDbType.Integer);
                Quantity.Value = Convert.ToInt32(entry.Value);
                myCommand.Parameters.Add(Quantity);

                try
                {
                    openAccessConnection();
                    myCommand.ExecuteNonQuery();
                    closeAccessConnection();
                }
                catch (Exception exc)
                {
                    Response.Write(exc.Message.ToString());
                }
            }
        }

        private void AddUserToDB()
        {
            OleDbCommand myCommand = new OleDbCommand("INSERT INTO tblOrder(oUserName, oUserPhone, oUserEmail, oDate) VALUES (@oUserName,@oUserPhone,@oUserEmail, @oDate)", myAccessConnection);
            string query = "SELECT @@Identity";

            // Mark the Command as a Text
            myCommand.CommandType = CommandType.Text;

            // Add Parameters to Command
            OleDbParameter oUserName = new OleDbParameter("@oUserName", OleDbType.VarChar);
            oUserName.Value = this.txtName.Text;
            myCommand.Parameters.Add(oUserName);

            OleDbParameter oUserPhone = new OleDbParameter("@oUserPhone", OleDbType.VarChar);
            oUserPhone.Value = this.txtPhone.Text;
            myCommand.Parameters.Add(oUserPhone);

            OleDbParameter oUserEmail = new OleDbParameter("@oUserEmail", OleDbType.VarChar);
            oUserEmail.Value = this.txtEmail.Text;
            myCommand.Parameters.Add(oUserEmail);

            OleDbParameter oDate = new OleDbParameter("@oDate", OleDbType.Date);
            oDate.Value = DateTime.Today;
            myCommand.Parameters.Add(oDate);

            try
            {
                openAccessConnection();
                myCommand.ExecuteNonQuery();
                myCommand.CommandText = query;
                orderID = (int)myCommand.ExecuteScalar();
                closeAccessConnection();
            }
            catch (Exception exc)
            {
                Response.Write("更新資料庫失敗. 錯誤訊息 : " + exc.Message.ToString());
            }
        }

        protected void openAccessConnection()
        {
            // If condition that can be used to check the access database connection
            // whether it is already open or not.
            if (myAccessConnection.State == ConnectionState.Closed)
            {
                myAccessConnection.Open();
            }
        }

        protected void closeAccessConnection()
        {
            // If condition to check the access database connection state
            // If it is open then close it.
            if (myAccessConnection.State == ConnectionState.Open)
            {
                myAccessConnection.Close();
            }
        }
    }

  • Re: Unable to cast object of type for adding data into access database

    08-28-2008, 3:57 AM
    • Member
      5 point Member
    • photossed
    • Member since 06-16-2008, 2:51 PM
    • Posts 43

     anyone?

Page 1 of 1 (2 items)