Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am making a store managment system using database sql server and i am putting products using form cashout and i want to print a sum of all products in textbox then i set sum statement in sql and their coloum name bill is a sum and now i want to send the sum of products from class to form. but there is error Cannot implicitly convert type 'float' to 'System.Data.DataTable' .please help i am begineer. i want urgent answer.

What I have tried:

C#
public DataTable slectbill()
        {

            string query = "select sum(subTotal) as bill from Cashout";

            SqlDataAdapter adapt = new SqlDataAdapter(query, connection());

            openConnection();
            DataTable dt = new DataTable();
            DataRow row = dt.Rows[0];
            bill = row["bill"].ToString();
            adapt.Fill(dt);
            closeConnection();

            return bill;
        }
Posted
Updated 9-Aug-20 18:41pm
v3

"i am begineer" - ok
"i want urgent answer" - not ok - people on this site are volunteers, ie do not get paid - we are not your servants - I suggest you learn some manners

I can't see why you're complicating things with a datatable - you seem to want a straightforward decimal (money) result back, so off the top of my head, wouldn't something like this

public Decimal slectbill(string connString)
{
    Decimal bill = 0;
    string query = "select sum(subTotal) as bill from Cashout";

    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        try
        {
            conn.Open();
            bill = (Decimal)cmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return bill;
}
make more/better sense ?
 
Share this answer
 
If you want to get a single value from ADO query it is not suitable to use
SqlDataAdapter
prefer using SQLCommand with Scalar value calling
sqlCommand.ExecuteScalar()
 
Share this answer
 

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