Ahmed Zafar
Ahmed Zafar

Reputation: 29

Error while retrieving data from database table and get its value in label

I get an error

System.Data.SqlClient.SqlException: Incorrect syntax near '='

My class is

public void bil()
{
    SqlConnection con = new SqlConnection();
    con.ConnectionString = (@"Server=DESKTOP-8JJO9NL\SQLEXPRESS;Database=HMS;Trusted_Connection=True");

    con.Open();

    string sqlquery = "select  DATEDIFF(day,arrivaldate,departure) AS TotalDays=@tdays ,(DATEDIFF(day,arrivaldate,departure) * 10) AS CalculatedAmount=@amount from Roomsdata where room#=@roomnum2";

    SqlCommand command = new SqlCommand(sqlquery, con);

    SqlDataReader sReader;

    command.Parameters.Clear();
    command.Parameters.AddWithValue("@roomnum2", Roomnum);

    sReader = command.ExecuteReader();

    while (sReader.Read())
    {
        Tdays = sReader["TotalDays"].ToString(); 
        Amount = sReader["CalculatedAmount"].ToString();
    }

    con.Close();

More error details:

Line 142: command.Parameters.Clear();
Line 143: command.Parameters.AddWithValue("@roomnum2", Roomnum);
Line 144: sReader = command.ExecuteReader();
Line 145:
Line 146: while (sReader.Read())

Upvotes: 0

Views: 369

Answers (2)

Ilconzy
Ilconzy

Reputation: 21

You did not initiate the @tdays and @amount parameters

Upvotes: 1

omelgarejo
omelgarejo

Reputation: 1

Why wrote "#" after where room? maybe that is error

Upvotes: 0

Related Questions