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

时间:2016-02-16 12:10:22

标签: c# asp.net

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

categories table is here

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

1 个答案:

答案 0 :(得分:0)

试试这个...

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 ...

希望这有帮助