0

I am running this code but it throws an exception and I am not sure why. Any help would be appreciated. I checked the ID and it was the right ID for the record.

protected void DeleteSQLDB(int id)
{
    String ConnString = GetConnectAccess();

    try       
    {
        using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
        {
            String sql = "DELETE FROM tblStudent WHERE ID=" + id;

            using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
            {
                m_dbConnection.Open();
                cmd.ExecuteNonQuery();
                m_dbConnection.Close();
            }
        }
    }
    catch (Exception ex)
    {
        Response.Redirect("somwhere");
    }
    finally
    {
    }
}
14
  • 1
    You should see the details of the exception to see what's wrong. Commented Feb 9, 2018 at 19:32
  • im editing a file on the internet using notepad and uploading it via FTP so I do not know how to show the exception. :( Commented Feb 9, 2018 at 20:40
  • 1
    Change this part to print details of the exception... catch (Exception ex) { Response.Redirect("somwhere"); } Commented Feb 9, 2018 at 20:42
  • yes I know but how to a show the exception? I tried ex.toString(); That didn't work. Commented Feb 9, 2018 at 20:52
  • What's the output if you put this: Console.WriteLine(ex.ToString());? Commented Feb 9, 2018 at 20:54

1 Answer 1

1

I solved the problem. The reason was that the record was referenced in other tables so I had to remove the references before I could remove the record. Thanks user7396598 for advice about running query manually. This is the code which removes the conversations first and then the student record:

 //This deletes the archived student, First any conversations need to be deleted before the record can be removed.
protected void DeleteSQLDB(object id,String studentID)
{
  //  Response.Redirect(studentID);

    String ConnString = GetConnectAccess();
    try
    {
        using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
        {

            String sql = "DELETE FROM tblConversations WHERE StudentID=@studentID";

            using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
            {
                cmd.Parameters.AddWithValue("@studentID", studentID);
                m_dbConnection.Open();
                cmd.ExecuteNonQuery();



            }


        }


    }
    catch (Exception ex)
    {

    }

    finally
    {
        DeleteSQLDB2(id);




    }
}

protected void DeleteSQLDB2(object id)
{
    //  Response.Redirect(studentID);

    String ConnString = GetConnectAccess();
    try
    {
        using (SqlConnection m_dbConnection = new SqlConnection(ConnString))
        {
            String sql = "DELETE FROM tblStudent WHERE ID=@ID";


            using (SqlCommand cmd = new SqlCommand(sql, m_dbConnection))
            {
                cmd.Parameters.AddWithValue("@ID", id);
                m_dbConnection.Open();
                cmd.ExecuteNonQuery();



            }


        }


    }
    catch (Exception ex)
    {

    }

    finally
    {


        Response.Redirect("studentgrid.aspx");

    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. You can either locate the dependent entries, and delete them manually, OR look into CASCADE on delete. Check out this answer for both SSMS and script ways to utilize it: stackoverflow.com/questions/6260688/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.