为什么我的记录没有在使用ASP.NET的SQL Server中删除?

时间:2018-09-30 11:55:33

标签: asp.net ajax ado.net asp.net-ajax

我试图在删除目录后从数据库中删除记录。

我有一个要删除的按钮,一旦单击按钮,它必须调用ajax,然后删除Folder(如果存在),一旦删除文件夹,然后删除数据库上的一条记录

这是我的Ajax代码:

// Delete Folder
$("#btnDeleteFolder").click(function () {
    var dirPath = $('#ContentPlaceHolder1_txtPath_I').val();
    var folderName = $('#txtFolderName').val();
    var location = $('#ddlLocation option:selected').text();
    alert("FolderName = "+folderName);
    alert("Location = "+location);

    //alert('calling path =' + dirPath);
    $.ajax({
        method: 'post',
        url: "GetAllFolderDetails.asmx/setDeleteFolder",

        data: {
            dirLocation: dirPath,
            folderName: folderName,
            location: location
        },

        dataType: "json",
        success: function (data) {
            location.reload(true);
            //alert("Success");

        },
        error: function (result) {
            alert("Error");
        }
    });
});

Asmx.cs代码:

public void setDeleteFolder(string dirLocation,string folderName,string location)
{
    // First check Whether Other Folder or Files Exists inside the folder
    // If it Exist, Delete those files and then delete your SELECTED Folder
    string insidePath = Server.MapPath("~/" + dirLocation);
    string[] files = Directory.GetFiles(insidePath, "*", SearchOption.AllDirectories);

    foreach (string file in files)
    {
        File.Delete(file);
    }

    if (Directory.Exists(insidePath))
    {
        Directory.Delete(insidePath);

        // Once the File Directory has deleted successfully, Then Delete the record from Database
        var locationID = 0;

        switch (location)
        {
            case "Store1":
                locationID = 1;
                break;

            case "Store1":
                locationID = 2;
                break;

            case "Store1":
                locationID = 3;
                break;
        }

        string cs = ConfigurationManager.ConnectionStrings["webConfigConnectionString"].ConnectionString;

        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;

            cmd.CommandText = "DELETE Folder WHERE StoreID = @Storeid AND FolderName = @FolderName)";

            cmd.Parameters.AddWithValue("@Storeid", 1);  // For testing I directly give the value for parameters
            cmd.Parameters.AddWithValue("@FolderName", "Sales");

            con.Open();
            cmd.ExecuteNonQuery();
        }
    }

    JavaScriptSerializer js = new JavaScriptSerializer();
    Context.Response.Write(js.Serialize("Successfully deleted"));
}

错误消息:

  

内部服务器错误

注意

我的目标文件夹已成功删除。但是在数据库中,记录不会被删除

1 个答案:

答案 0 :(得分:2)

更改行

cmd.CommandText = "DELETE Folder WHERE StoreID = @Storeid AND FolderName = @FolderName)";

cmd.CommandText = "DELETE from Folder WHERE StoreID = @Storeid AND FolderName = @FolderName";