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



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

我有一个按钮要删除,一旦我点击按钮,它必须调用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"));
}

错误信息:

内部服务器错误

注意

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

更改行

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

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

最新更新