简体   繁体   English

使用请求querystring asp.net

[英]using request querystring asp.net

I want to delete a sub category and display a message about the deletion but my query doesntt delete anything. 我想删除一个子类别并显示有关删除的消息,但是我的查询没有删除任何内容。 Help me plz. 请帮我。

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

Try with this... 试试这个...

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

I just assign the SQLConnection to SQLCommand... 我只是将SQLConnection分配给SQLCommand ...

Hopes this helps 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM