Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: object type cannot be compared with an int

This is my table:

roomtype, number of rooms
Ac        10

I want to retrieve the value from the table and subtract the rooms by 1 and update the above table. How do I write the retrieval code in ASP.NET using C#?

This is the updated code. It is showing errors in dt.Rows[0]["no_of_rooms"] > 1 saying that an object type cannot be compared with an int. But on parsing this no_of_rooms to int the error remains the same.

public partial class Book_Room : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        string type = DropDownList1.SelectedItem.ToString();
        string name = TextBox2.Text;
        string nop = DropDownList2.SelectedItem.ToString();
        int num = int.Parse(nop);
        string connectionString = WebConfigurationManager.ConnectionStrings["HMSConnectionString"].ConnectionString;
        SqlConnection connection = new SqlConnection(connectionString);

        string qry3 = "select * from availiability where RoomType=@type";
        SqlCommand cmd3 = new SqlCommand(qry3, connection);
        cmd3.Parameters.AddWithValue("@type", type);
        cmd3.ExecuteReader();
        SqlDataAdapter ad = new SqlDataAdapter(cmd3);
        DataTable dt = new DataTable();
        if (dt.Rows.Count > 0)
        {    
            if (dt.Rows[0]["no_of_rooms"] > 1)
            {
                string qry = "insert into RoomType values('" + type + "','" + name + "','" + num + "') ";
                SqlCommand cmd = new SqlCommand(qry, connection);
                connection.Open();
                int g = cmd.ExecuteNonQuery();
                if (g != 0)
                    Label5.Text = "Reserved for" + name;
                connection.Close();

                string qry2 = "update availiability set RoomType=@type ,availiable_rooms=@av";
                SqlCommand cmd2 = new SqlCommand(qry2, connection);
                cmd2.Parameters.AddWithValue("@type", type);
                cmd2.Parameters.AddWithValue("@av", dt.Rows[0]["no_of_rooms"] - 1);
                connection.Open();
                cmd2.ExecuteNonQuery();
                connection.Close();
            }
        }

        else
        {
            label5.Text = "No Rooms Availiable in " + type;
        }
    }
}
like image 663
user2334012 Avatar asked Feb 15 '23 00:02

user2334012


1 Answers

Change it to this (int)dt.Rows[0]["no_of_rooms"] > 1.

like image 147
Mike Perrenoud Avatar answered Feb 23 '23 12:02

Mike Perrenoud