使用请求查询字符串 asp.net

本文关键字:asp net 字符串 查询 请求 | 更新日期: 2023-09-27 18:34:51

我想删除一个子类别并显示有关删除的消息,但我的查询没有删除任何内容。请帮帮我。

分类目录在这里

<a href="SubCategories.aspx?Process=Delete&CategoryID=<%#Eval("CategoryID") %>" title="Delete" class="tip" ><span class="icon12 icomoon-icon-remove"></span></a>
if (Process == "Delete")
{
    DataTable dtProducts = system.GetDataTable("Select COALESCE(COUNT(1),0) as TOTAL from TBLPRODUCTS where CategoryID = " + CategoryID);
    if (dtProducts.Rows[0]["TOTAL"].ToString() == "0")
    {
        SqlConnection cnn = new SqlConnection();
        SqlCommand cmd = new SqlCommand("Delete from TBLCATEGORIES where SubCategoryID=" +  Request.QueryString["CategoryID"] );
        try {
            cnn.Open();
             cmd.ExecuteNonQuery();}
        catch {
            lblMsg.Text = "Error!";
        }
        finally {  
             cnn.Close(); 
        }
             DeleteMsg.Visible = true;
    }
}

使用请求查询字符串 asp.net

试试这个...

if (Process == "Delete")
    {
        DataTable dtProducts = system.GetDataTable("Select COALESCE(COUNT(1),0) as TOTAL from TBLPRODUCTS where CategoryID = " + CategoryID);
        if (dtProducts.Rows[0]["TOTAL"].ToString() == "0")
        {
            SqlConnection cnn = new SqlConnection("data source=localhost'<instance name>;initial catalog=Shqipar; Integrated Security=SSPI;");
            SqlCommand cmd = new SqlCommand("Delete from TBLCATEGORIES where SubCategoryID=" +  Request.QueryString["CategoryID"] );
            try {
                cnn.Open();
                 cmd.connection=cnn;
                 cmd.ExecuteNonQuery();}
            catch(Exception ex) {
                lblMsg.Text = ex.message;
            }
            finally {  
                 cnn.Close(); 
            }
                 DeleteMsg.Visible = true;
        }
    }
else
{
 InfoMsg.Visible=True;
}

我只是将 SQLConnection 分配给 SQLCommand...

希望这有帮助