Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre> if (e.CommandName == "DoPlus")//Check quantity here
        {
            string PID = (e.CommandArgument.ToString());
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("SP_getUserCartItem", con)
                {
                    CommandType = CommandType.StoredProcedure
                };
                cmd.Parameters.AddWithValue("@PID", PID);
                cmd.Parameters.AddWithValue("@UserID", UserID);
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);

                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            if (dt.Rows.Count > 0)
                            {
                                Int32 myQty = Convert.ToInt32(dt.Rows[0]["Qty"].ToString());
                                while (dr.Read())
                                {
                                    int PQuantity = Convert.ToInt32(dr["PQuantity"].ToString());
                                    
                                    if (myQty < PQuantity)
                                    {
                                        Int32 updateQty = Convert.ToInt32(dt.Rows[0]["Qty"].ToString());
                                        SqlCommand myCmd = new SqlCommand("SP_UpdateCart", con)
                                        {
                                            CommandType = CommandType.StoredProcedure
                                        };
                                        myCmd.Parameters.AddWithValue("@Quantity", updateQty + 1);
                                        myCmd.Parameters.AddWithValue("@CartPID", PID);
                                        myCmd.Parameters.AddWithValue("@UserID", UserID);
                                       
                                        Int64 CartID = Convert.ToInt64(myCmd.ExecuteNonQuery());
                                      
                                        con.Close();
                                        BindProductCart();
                                        BindCartNumber();
                                    }


                                    else if (myQty > PQuantity)//check quantity between tblCart and tblProducts
                                    {
                                        divQtyError2.Visible = true;


                                    }

                                }dr.Close();

                            }
                           
                        }
                    } 
                }con.Close();
            }
        }


What I have tried:

At first i got error message
The connection was not closed. The connection's current state is open.
But when i exclude con.open(); before
Int64 CartID = Convert.ToInt64(myCmd.ExecuteNonQuery());
and con.Close(); after that code, I got this error.
Posted
Updated 15-Mar-21 3:04am
Comments
Richard MacCutchan 15-Mar-21 6:46am    
Why are you trying to use a data reader when you have already read the result set into your data adapter?

Your code is rather confused. You load the results of the stored procedure into a DataTable, then execute the command again and read through the results you've already loaded one at a time.

The error is thrown because you have an active SqlDataReader reading rows from the connection, and you're trying to execute another command on the same connection.

Since you've already loaded the rows, you don't need the data reader at all.
C#
if (e.CommandName == "DoPlus")//Check quantity here
{
    string PID = e.CommandArgument.ToString();
    
    using (SqlConnection con = new SqlConnection(CS))
    using (SqlCommand cmd = new SqlCommand("SP_getUserCartItem", con) { CommandType = CommandType.StoredProcedure })
    {
        cmd.Parameters.AddWithValue("@PID", PID);
        cmd.Parameters.AddWithValue("@UserID", UserID);
        
        con.Open();

        DataTable dt = new DataTable();
        using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
        {
            sda.Fill(dt);
        }
        
        foreach (DataRow row in dt.Rows)
        {
            int myQty = row.Field<int>("Qty");
            int PQuantity = row.Field<int>("PQuantity");
            if (myQty < PQuantity)
            {
                int updateQty = row.Field<int>("Qty");
                
                using (SqlCommand myCmd = new SqlCommand("SP_UpdateCart", con) { CommandType = CommandType.StoredProcedure })
                {
                    myCmd.Parameters.AddWithValue("@Quantity", updateQty + 1);
                    myCmd.Parameters.AddWithValue("@CartPID", PID);
                    myCmd.Parameters.AddWithValue("@UserID", UserID);
                                       
                    myCmd.ExecuteNonQuery();
                }
                
                BindProductCart();
                BindCartNumber();
            }
            else if (myQty > PQuantity)
            {
                divQtyError2.Visible = true;
            }
        }
    }
}
 
Share this answer
 
Comments
Member 15100384 28-May-21 5:21am    
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getUserCartItem]
(
@PID int,
@UserID int
)
AS
BEGIN
SELECT * FROM tblCart WHERE PID = @PID AND UID = @UserID
SELECT * FROM tblProducts WHERE PID=@PID
END
Member 15100384 28-May-21 5:22am    
Qty from tblCart, PQuantity from tblProducts. There are error Column 'PQuantity' does not belong to table . How can I fix this?
Richard Deeming 28-May-21 5:29am    
Use the correct column name. There is no way anyone can tell you what that is, because we can't see the structure of your database, nor do we know which column you actually want to use.
You don't need to call Close on anything if you're using using.

Try removing those and see what happens.

Also, you can't re-use a SqlParameter list. You have to clear it out and re-add the parameters.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900