Operator '*' cannot be applied to operands of type 'string' and 'string'

Paul Abby 0 Reputation points
2023-02-26T12:20:36.3666667+00:00
 }
        public void SaveBillDetails(BillDetail details)
        {
            SqlConnection con = new SqlConnection(ConnectionString);
            try
            {
                
                con.Open();
                SqlCommand cmd = new SqlCommand("spt_saveEBillDetails", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@CustomerName",details.CustomerName);
                cmd.Parameters.AddWithValue("@MobileNumber",details.MobileNumber);
                cmd.Parameters.AddWithValue("@Address",details.Address);
                cmd.Parameters.AddWithValue("@TotalAmount",details.TotalAmount);

                SqlParameter outputPara = new SqlParameter();
                outputPara.DbType = DbType.Int32;
                outputPara.Direction = ParameterDirection.Output;
                outputPara.ParameterName = "@Id";
                cmd.Parameters.Add(outputPara);
                cmd.ExecuteNonQuery();
                int id = int.Parse(outputPara.Value.ToString());
                if(details.Items.Count> 0)

Note : Please help out guru............ Thanks

details.TotalAmount = details.Items.Sum(i => i.Price * i.Quantity); // This is where my issue is
Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2023-02-26T12:32:28.3133333+00:00

    Check this:

    details.TotalAmount = details.Items.Sum(i => decimal.Parse(i.Price) * int.Parse(i.Quantity));
    

    Or maybe adjust the types of Price and Quantity.

    1 person found this answer helpful.
    0 comments No comments

  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-02-26T15:46:23.7933333+00:00

    No need to use decimal.Parse or int.Parse conceptual example

    SET ANSI_NULLS ON;
    GO
    SET QUOTED_IDENTIFIER ON;
    GO
    
    CREATE PROCEDURE spDemo @Parm DECIMAL(18, 2) OUT
    AS
    BEGIN
        SET NOCOUNT ON;
        SET @Parm = 93.77;
    END;
    GO
    

    Code

    public static void StoredDemo()
    {
        using var cn = new SqlConnection(ConnectionString());
        using var cmd = new SqlCommand("spDemo", cn);
    
        cmd.CommandType = CommandType.StoredProcedure;
    
        cmd.Parameters.Add("@Parm", SqlDbType.Decimal)
            .Direction = ParameterDirection.Output;
        
        
        cn.Open();
        cmd.ExecuteNonQuery();
        int quantity = 10;
        decimal value = (decimal)cmd.Parameters["@Parm"].Value ;
        decimal result = value * quantity;
    }
    
    1 person found this answer helpful.
    0 comments No comments

  3. Paul Abby 0 Reputation points
    2023-02-27T15:50:13.6966667+00:00

    Thanks for your contributions @Viorel and @Karen

    I used this below and it worked for me

    details.TotalAmount = details.Items.Sum(i => int.Parse(i.Price) * int.Parse(i.Quantity));

    I converted both strings to int using parse..

    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.