c# - 错误 : object type cannot be compared with an int

标签 c# asp.net ado.net

这是我的 table :

roomtype, number of rooms
Ac        10

我想从表中检索值并将房间数减 1 并更新上表。如何使用 C# 在 ASP.NET 中编写检索代码?

这是更新后的代码。它在 dt.Rows[0]["no_of_rooms"] > 1 中显示错误,表示无法将对象类型与 int 进行比较。但是在将此 no_of_rooms 解析为 int 时,错误保持不变。

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

最佳答案

将其更改为此 (int)dt.Rows[0]["no_of_rooms"] > 1

关于c# - 错误 : object type cannot be compared with an int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19365211/

相关文章:

c# - 确定 MethodInfo 是否表示 lambda 表达式

c# - 在 linq 中使用 GroupBy 时,如何获取组名列表?

c# - 插入错误 ASP.NET

c# - 当 url 末尾有 DOT (.) 时,Asp.Net MVC 路由操作方法不会调用

c# - 合并两个数据表

SqlDb类型和地理

c# - 如何解决 .NET.CORE 中的代理服务器 407 错误

c# - 不完整的视频流式传输到移动浏览器 - HTTP header

c# - SqlCommand.ExecuteReader/SqlDataReader.Read 是否吞咽异常?

c# - 为什么我可以在同一个对象上多次调用 ToString()?