Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 994325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:33:26+00:00 2026-05-16T06:33:26+00:00

public bool AddEntity(int parentId, string description) { try { _connection.Open(); SqlCommand command = new

  • 0

public bool AddEntity(int parentId, string description)
{
try
{
_connection.Open();
SqlCommand command = new SqlCommand(“INSERT Structure (Path,Description) ” +
“VALUES(” + GetPath(parentId) + “.GetDescendant(” + GetLastChildPath(parentId, 1) + “, NULL), ” +
description + “)”, _connection);

            if (command.ExecuteNonQuery() <= 0) _success = false;

            command.Connection.Close();

            if (_success)
            {
                return true;
            }

            throw new Exception("An error has occured whilst trying to add a entity");
        }
        catch (Exception ex)
        {
            AddError(new ErrorModel("An error has occured whilst trying to add a entity", ErrorHelper.ErrorTypes.Critical, ex));
            return false;
        }
    }

Is there a better way of handling the exceptions in the example above?

Thanks in advance for any help.

Clare

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:33:26+00:00Added an answer on May 16, 2026 at 6:33 am

    There’s quite a few things wrong here.

    a. You’re using inline SQL and injecting what I can only assume to be user generated data into it. This is a security risk. Use a parameterised query.

    b. You’re exception handling is ok but this will leave the connection open if an error occurs. I’d write it like so:

     public bool AddEntity(int parentId, string description)
     {
        try
        {
            //Assuming you have a string field called connection string
            using(SqlConnection conn = new SqlConnection(_connectionString))
            {
                SqlParameter descriptionParam = new SqlParameter("@description", SqlDbType.VarChar, 11);
                descriptionParam.Value = description;
    
                SqlParameter parentIdParam = new SqlParameter("@parentId", SqlDbType.Int, 4);
                parentIdParam.Value = parentId;
    
                //Bit confused about the GetPath bit.
                SqlCommand command = new SqlCommand("INSERT Structure (Path,Description) " +
                                                "VALUES(" + GetPath(parentId) + ".GetDescendant(" + GetLastChildPath(parentId, 1) + ", NULL),@description)", conn);
    
                command.Parameters.Add(descriptionParam);
    
                if (command.ExecuteNonQuery() <= 0) _success = false;
            }
    
            if (_success)
            {
                return true;
            }
    
            //This isn't really an exception. You know an error has a occured handle it properly here.
            throw new Exception("An error has occured whilst trying to add a entity");
        }
        catch (Exception ex)
        {
            AddError(new ErrorModel("An error has occured whilst trying to add a entity", ErrorHelper.ErrorTypes.Critical, ex));
            return false;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public bool send() { try { string body = _omschrijving; var fromAddress = new
public bool InsertBank() { try { SqlCommand cmd = new SqlCommand(INSERTBANKMASTER, dal.con); cmd.Parameters.Add(@Bank_Name,SqlDbType.NVarChar).Value =
public bool DeleteLoc(int Location_Id) { DataClassesLocationDataContext dc = new DataClassesLocationDataContext(); Location loc = dc.Locations.Single(u
I have the code: public bool RemoveUserFromAdministratorsGroup(UserPrincipal oUserPrincipal, string computer) { try { PrincipalContext
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
[DataMember] public int? NumberOfPages; //////////// Is this supported???? [DataMember] public bool? Color; //////////// Is
Public bool SqlCheck(string username, string password) { // sql checks here return true }
running this code: public bool CheckTime(DateTime date, int UserID, int TotalTimeMin) { using (var
I have this method: public bool CantMoveIfCreatedCheck(Pieces[,] pieces, int rowStart, int columnStart, int rowEnd,
Please consider the following: public class MyObject { public bool B; public string Txt;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.