Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 错误:无法将对象类型与int进行比较_C#_Asp.net_Ado.net - Fatal编程技术网

C# 错误:无法将对象类型与int进行比较

C# 错误:无法将对象类型与int进行比较,c#,asp.net,ado.net,C#,Asp.net,Ado.net,这是我的桌子: roomtype, number of rooms Ac 10 我想从表中检索该值,并用1减去房间,然后更新上面的表。如何使用C#在ASP.NET中编写检索代码 这是更新的代码。它在dt.行[0][“没有房间”]>1中显示错误,表示对象类型不能与int进行比较。但是,在解析这个no_of_rooms to int时,错误保持不变 public partial class Book_Room : System.Web.UI.Page { protected

这是我的桌子:

roomtype, number of rooms
Ac        10
我想从表中检索该值,并用1减去房间,然后更新上面的表。如何使用C#在ASP.NET中编写检索代码

这是更新的代码。它在
dt.行[0][“没有房间”]>1
中显示错误,表示对象类型不能与
int
进行比较。但是,在解析这个no_of_rooms to 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][“没有房间”]>1

,或者您可以尝试


dt.Rows[0].Field(“no_of_rooms”)>1

您没有使用从查询返回的任何其他值,因此创建
SqlDataAdapater
并填充
表有点麻烦

我建议改用
ExecuteScalar
。这将从数据库返回一个值

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
}

如果你想把它用作
int
,你必须把它转换成
int
。可能只有我一个人,但看起来它最终会转化成一些巨大的意大利面代码。不要将所有逻辑都放在按钮点击事件中,而是将功能委托给助手类,等等。只需我的两分钱。我只是讨厌乱七八糟的代码。我情不自禁。