user2334012
user2334012

Reputation: 61

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;
        }
    }
}

Upvotes: 4

Views: 125

Answers (3)

Kami
Kami

Reputation: 19437

You are not using any of the other values returned from your query, so creating a SqlDataAdapater and filling a Table is a bit much.

I would recommend using ExecuteScalar instead. This returns a single value from the database.

string qry3 = "select * from availiability where RoomType=@type";
SqlCommand cmd3 = new SqlCommand(qry3, connection);
cmd3.Parameters.AddWithValue("@type", type);
object objCount = command.ExecuteScalar();
int count = objCount == null ? 0 : (int)objCount;
if (count > 0)
{
    // Do other things
}

Upvotes: 2

progpow
progpow

Reputation: 1580

Or you can try

dt.Rows[0].Field<int>("no_of_rooms")>1

Upvotes: 3

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

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

Upvotes: 3

Related Questions