Seeking Guidance on DataTable Error Despite Verified Correct Stored Procedure

PS 0 Reputation points
2024-01-06T17:55:42.6833333+00:00

System.ArgumentException: 'Column 'TotalPrice' does not belong to table .User's image

public partial class Invoice : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataAdapter sda;
    DataTable dt;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["userId"] != null)
            {
                if (Request.QueryString["id"] != null)
                {
                    rOrderItem.DataSource = GetOrderDetails();
                    rOrderItem.DataBind();
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
    }

    DataTable GetOrderDetails()
    {
        double grandTotal = 0;
        con = new SqlConnection(Connection.GetConnectionString());
        cmd = new SqlCommand("Invoice", con);
        cmd.Parameters.AddWithValue("@Action", "INVOICEBYID");
        cmd.Parameters.AddWithValue("@PaymentId", Convert.ToInt32(Request.QueryString["id"]));
        cmd.Parameters.AddWithValue("@UserId", Session["userId"]);
        cmd.CommandType = CommandType.StoredProcedure;

        sda = new SqlDataAdapter(cmd);
        dt = new DataTable();
        sda.Fill(dt);

        if (dt.Rows.Count > 0)
        {
            foreach (DataRow drow in dt.Rows)
            {
                grandTotal += Convert.ToDouble(drow["TotalPrice"]);
            }
        }

        DataRow dr = dt.NewRow();
        dr["TotalPrice"] = grandTotal;
        dt.Rows.Add(dr);

        return dt;
    }
}
CREATE PROCEDURE Invoice
@Action VARCHAR(10),
@PaymentId INT = NULL,
@UserId INT = NULL,
@OrderDetailsId INT = NULL,
@Status VARCHAR(50) = NULL

AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- GET INVOICE BY ID
	IF @Action = 'INVOICEBYID'
	BEGIN
	SELECT ROW_NUMBER() OVER(ORDER BY (SELECT 1)) AS [SrNo], o.OrderNo, p.Name, p.Price, o.Quantity,
	(p.Price * o.Quantity) as TotalPrice, o.OrderDate, o.Status FROM Orders o
	INNER JOIN Products p ON p.ProductsId = o.ProductId
	WHERE o.PaymentId = @PaymentId AND o.UserId = @UserId
END
END
Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2024-01-07T13:33:45.6833333+00:00

    The string 'INVOICEBYID' is 11 characters yet the procedure parameter is defined as10 characters. The string is probably truncated to 'INVOICEBYI' and the "IF" condition is never meet.

    @Action VARCHAR(10)
    

    This should be easy to figure out if you were using the watch window in the Visual Studio debugger or simply testing the stored procedure.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.