Why my record is not deleted in SQL Server using ASP.NET?

Liam

New Member
#1
I am trying to delete records from the database after it deletes the directory.

I have a button to delete, once I click the button, it has to call the ajax, then delete the Folder if it exists, Once it delete folder,then delete one record on database

Here is my Ajax code:
Mã:
// 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 code:
Mã:
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"));
}
Error message:
Internal Server Error
Note
My destination folder is Successfully Deleted. But In the database, the records are not deleted
 

Admin

Administrator
Thành viên BQT
#2
change the line
Mã:
cmd.CommandText = "DELETE Folder WHERE StoreID = @Storeid AND FolderName = @FolderName)";
to
Mã:
cmd.CommandText = "DELETE from Folder WHERE StoreID = @Storeid AND FolderName = @FolderName";
 

Từ khóa phổ biến

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top