使用 C# asp.net 更新值

本文关键字:更新 net asp 使用 | 更新日期: 2023-09-27 18:35:12

这是我的表

roomtype, number of rooms
Ac         10

我想从表中检索值并将房间减去 1 并更新上表。如何使用 c# 在 asp.net 中编写检索代码。更新代码如下,我正在检查房间的可用性是否为>1,然后它才会更新和插入,但它没有这样做。它直接执行 else 部分。帮我找到错误。

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 ((int)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;
    }
}

使用 C# asp.net 更新值

看起来您正在尝试从头开始编写此内容(也称为 Web 窗体)。 虽然这令人钦佩,但您是否可以选择使用更适合您要做的事情的框架? 例如,ASP.NET 带有实体框架的 MVC。 甚至是带有实体框架的Web API。 两者都支持POST/DELETE/PUT/GET,并且一切都为您连接,因此您无需担心手动编码查询等。 实体框架将为你完成所有工作。

但是,如果你不能,似乎你所拥有的应该有效。 这是一个非常简单的问题。 如上所示,只需将"房间"更新为新值即可。

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);
    Dataset ds = new Dataset();
    ad.Fill(ds);
    if (ds ! =null)
 {  
    if (ds.Rows.Count > 0)
    {
        if ((int)ds.Rows[0]["no_of_rooms"] > num) //Check if rooms are available 
        {
            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", convert.toint32(ds.Rows[0]["no_of_rooms"]) - num);
            connection.Open();
            cmd2.ExecuteNonQuery();
            connection.Close();
        }
    }
    }
    else
    {
        label5.Text = "No Rooms Availiable in " + type;
    }
}