Best way to handle exception prior to updating database












-2















I need to check if an input is valid prior to updating my db.



I have created a bool method to check if it is valid.



private bool validProject(string ProjectID)
{
using (AXEntitiesDEV db = new AXEntitiesDEV())
{
return db.PROJTABLEs.Any(c => c.PROJID==ProjectID);

}
}


and am checking it in my update method.



 protected void Insert(object sender, EventArgs e)
{
using (GPSE2Entities entities = new GPSE2Entities())
{
TextBox txtEditTime = (TextBox)gvDailyGPS.FooterRow.FindControl("txtFooterTime");
DropDownList ddlEventDateOnly = (DropDownList)gvDailyGPS.FooterRow.FindControl("ddlFooterDateOnly");
DateTime EDT = DateTime.Now;
TextBox txtAddProjectID = (TextBox)gvDailyGPS.FooterRow.FindControl("txtAddProjectID");
validProject(txtAddProjectID.Text);
DailyGPSTable newLOB = new DailyGPSTable
{
EventDateTime = EDT,
Project = txtAddProjectID.Text,
};

entities.DailyGPSTables.Add(newLOB);
{
entities.SaveChanges();
BindGrid();
}
entities.SaveChanges();
}
}


I tried catching and reporting the error but it allows invalid input through when false.



try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";

addErr.Text = "Invalid Project ID.";
}


What's the best way I can catch and report the error prior to trying the insert?










share|improve this question




















  • 3





    Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

    – Steve
    Nov 20 '18 at 20:21






  • 2





    This appears to be an XY problem.

    – Nkosi
    Nov 20 '18 at 20:23











  • I need to stop processing a db insert if this condition (one of many) is true.

    – Doug Farrell
    Nov 20 '18 at 20:26






  • 1





    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – Nkosi
    Nov 20 '18 at 20:31






  • 1





    @DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

    – Nkosi
    Nov 20 '18 at 20:33


















-2















I need to check if an input is valid prior to updating my db.



I have created a bool method to check if it is valid.



private bool validProject(string ProjectID)
{
using (AXEntitiesDEV db = new AXEntitiesDEV())
{
return db.PROJTABLEs.Any(c => c.PROJID==ProjectID);

}
}


and am checking it in my update method.



 protected void Insert(object sender, EventArgs e)
{
using (GPSE2Entities entities = new GPSE2Entities())
{
TextBox txtEditTime = (TextBox)gvDailyGPS.FooterRow.FindControl("txtFooterTime");
DropDownList ddlEventDateOnly = (DropDownList)gvDailyGPS.FooterRow.FindControl("ddlFooterDateOnly");
DateTime EDT = DateTime.Now;
TextBox txtAddProjectID = (TextBox)gvDailyGPS.FooterRow.FindControl("txtAddProjectID");
validProject(txtAddProjectID.Text);
DailyGPSTable newLOB = new DailyGPSTable
{
EventDateTime = EDT,
Project = txtAddProjectID.Text,
};

entities.DailyGPSTables.Add(newLOB);
{
entities.SaveChanges();
BindGrid();
}
entities.SaveChanges();
}
}


I tried catching and reporting the error but it allows invalid input through when false.



try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";

addErr.Text = "Invalid Project ID.";
}


What's the best way I can catch and report the error prior to trying the insert?










share|improve this question




















  • 3





    Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

    – Steve
    Nov 20 '18 at 20:21






  • 2





    This appears to be an XY problem.

    – Nkosi
    Nov 20 '18 at 20:23











  • I need to stop processing a db insert if this condition (one of many) is true.

    – Doug Farrell
    Nov 20 '18 at 20:26






  • 1





    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – Nkosi
    Nov 20 '18 at 20:31






  • 1





    @DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

    – Nkosi
    Nov 20 '18 at 20:33
















-2












-2








-2


0






I need to check if an input is valid prior to updating my db.



I have created a bool method to check if it is valid.



private bool validProject(string ProjectID)
{
using (AXEntitiesDEV db = new AXEntitiesDEV())
{
return db.PROJTABLEs.Any(c => c.PROJID==ProjectID);

}
}


and am checking it in my update method.



 protected void Insert(object sender, EventArgs e)
{
using (GPSE2Entities entities = new GPSE2Entities())
{
TextBox txtEditTime = (TextBox)gvDailyGPS.FooterRow.FindControl("txtFooterTime");
DropDownList ddlEventDateOnly = (DropDownList)gvDailyGPS.FooterRow.FindControl("ddlFooterDateOnly");
DateTime EDT = DateTime.Now;
TextBox txtAddProjectID = (TextBox)gvDailyGPS.FooterRow.FindControl("txtAddProjectID");
validProject(txtAddProjectID.Text);
DailyGPSTable newLOB = new DailyGPSTable
{
EventDateTime = EDT,
Project = txtAddProjectID.Text,
};

entities.DailyGPSTables.Add(newLOB);
{
entities.SaveChanges();
BindGrid();
}
entities.SaveChanges();
}
}


I tried catching and reporting the error but it allows invalid input through when false.



try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";

addErr.Text = "Invalid Project ID.";
}


What's the best way I can catch and report the error prior to trying the insert?










share|improve this question
















I need to check if an input is valid prior to updating my db.



I have created a bool method to check if it is valid.



private bool validProject(string ProjectID)
{
using (AXEntitiesDEV db = new AXEntitiesDEV())
{
return db.PROJTABLEs.Any(c => c.PROJID==ProjectID);

}
}


and am checking it in my update method.



 protected void Insert(object sender, EventArgs e)
{
using (GPSE2Entities entities = new GPSE2Entities())
{
TextBox txtEditTime = (TextBox)gvDailyGPS.FooterRow.FindControl("txtFooterTime");
DropDownList ddlEventDateOnly = (DropDownList)gvDailyGPS.FooterRow.FindControl("ddlFooterDateOnly");
DateTime EDT = DateTime.Now;
TextBox txtAddProjectID = (TextBox)gvDailyGPS.FooterRow.FindControl("txtAddProjectID");
validProject(txtAddProjectID.Text);
DailyGPSTable newLOB = new DailyGPSTable
{
EventDateTime = EDT,
Project = txtAddProjectID.Text,
};

entities.DailyGPSTables.Add(newLOB);
{
entities.SaveChanges();
BindGrid();
}
entities.SaveChanges();
}
}


I tried catching and reporting the error but it allows invalid input through when false.



try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";

addErr.Text = "Invalid Project ID.";
}


What's the best way I can catch and report the error prior to trying the insert?







c# boolean try-catch






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 21:30







Doug Farrell

















asked Nov 20 '18 at 20:19









Doug FarrellDoug Farrell

176




176








  • 3





    Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

    – Steve
    Nov 20 '18 at 20:21






  • 2





    This appears to be an XY problem.

    – Nkosi
    Nov 20 '18 at 20:23











  • I need to stop processing a db insert if this condition (one of many) is true.

    – Doug Farrell
    Nov 20 '18 at 20:26






  • 1





    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – Nkosi
    Nov 20 '18 at 20:31






  • 1





    @DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

    – Nkosi
    Nov 20 '18 at 20:33
















  • 3





    Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

    – Steve
    Nov 20 '18 at 20:21






  • 2





    This appears to be an XY problem.

    – Nkosi
    Nov 20 '18 at 20:23











  • I need to stop processing a db insert if this condition (one of many) is true.

    – Doug Farrell
    Nov 20 '18 at 20:26






  • 1





    Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

    – Nkosi
    Nov 20 '18 at 20:31






  • 1





    @DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

    – Nkosi
    Nov 20 '18 at 20:33










3




3





Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

– Steve
Nov 20 '18 at 20:21





Why do you want to throw a costly exception when you could simply get the return value and set the error text without any problem?

– Steve
Nov 20 '18 at 20:21




2




2





This appears to be an XY problem.

– Nkosi
Nov 20 '18 at 20:23





This appears to be an XY problem.

– Nkosi
Nov 20 '18 at 20:23













I need to stop processing a db insert if this condition (one of many) is true.

– Doug Farrell
Nov 20 '18 at 20:26





I need to stop processing a db insert if this condition (one of many) is true.

– Doug Farrell
Nov 20 '18 at 20:26




1




1





Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

– Nkosi
Nov 20 '18 at 20:31





Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.

– Nkosi
Nov 20 '18 at 20:31




1




1





@DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

– Nkosi
Nov 20 '18 at 20:33







@DougFarrell until you better explain the actual problem with a Minimal, Complete, and Verifiable example, those trying to help you wont be able to properly address the issue.

– Nkosi
Nov 20 '18 at 20:33














3 Answers
3






active

oldest

votes


















3














You can throw your own exception:



try
{
if (!validProject(txtAddProjectID.Text))
throw new ArugmentExceptioN(txtAddProjectID.Text);
}
catch (ArgumentException)
{
addErr.Text = "";
addErr.Text = "Invalid Project ID.";
}


But don't do that!



You already have a boolean telling you the function failed. Just use that with a normal if block:



if (!validProject(txtAddProjectID.Text))
{
addErr.Text = "";
addErr.Text = "Invalid Project ID.";
}





this is only one of many conditions that must be met prior to updating the db




There's still no need for an exception.



bool validRecord = true;

validRecord = validRecord && validProject(txtAddProjectID.Text);
validRecord = validRecord && someOtherCheck();
validRecord = validRecord && someFinalCheck();
validRecord = validRecord && howEverManyYouNeed();

if (!validRecord)
{
ShowError();
}
else
{
UpdateDB();
}


You can do a similar thing by just appending to a string or adding to a list that starts out empty. At the end, if the item has any length, show the errors.






share|improve this answer


























  • Yes but this is only one of many conditions that must be met prior to updating the db

    – Doug Farrell
    Nov 20 '18 at 20:31











  • Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

    – Doug Farrell
    Nov 20 '18 at 21:41













  • @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

    – Nkosi
    Nov 20 '18 at 21:48



















0














try
{
validProject(txtAddProjectID.Text);
}
catch (ArgumentException e)
{
addErr.Text = "";

addErr.Text = e.Message;
}


private static void validProject(object text)
{
throw new ArgumentException($"Invalid Project ID.{text}");
}





share|improve this answer
























  • This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

    – Lews Therin
    Nov 20 '18 at 20:26



















0














Just check the result of the call to validProject and throw an exception if it returns false:



try
{
if (!validProject(txtAddProjectID.Text))
throw new ArgumentException("Argument is invalid", nameof(txtAddProjectID.Text));
}
catch (ArgumentException)
{
addErr.Text = "";

addErr.Text = "Invalid Project ID.";
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400922%2fbest-way-to-handle-exception-prior-to-updating-database%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can throw your own exception:



    try
    {
    if (!validProject(txtAddProjectID.Text))
    throw new ArugmentExceptioN(txtAddProjectID.Text);
    }
    catch (ArgumentException)
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }


    But don't do that!



    You already have a boolean telling you the function failed. Just use that with a normal if block:



    if (!validProject(txtAddProjectID.Text))
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }





    this is only one of many conditions that must be met prior to updating the db




    There's still no need for an exception.



    bool validRecord = true;

    validRecord = validRecord && validProject(txtAddProjectID.Text);
    validRecord = validRecord && someOtherCheck();
    validRecord = validRecord && someFinalCheck();
    validRecord = validRecord && howEverManyYouNeed();

    if (!validRecord)
    {
    ShowError();
    }
    else
    {
    UpdateDB();
    }


    You can do a similar thing by just appending to a string or adding to a list that starts out empty. At the end, if the item has any length, show the errors.






    share|improve this answer


























    • Yes but this is only one of many conditions that must be met prior to updating the db

      – Doug Farrell
      Nov 20 '18 at 20:31











    • Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

      – Doug Farrell
      Nov 20 '18 at 21:41













    • @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

      – Nkosi
      Nov 20 '18 at 21:48
















    3














    You can throw your own exception:



    try
    {
    if (!validProject(txtAddProjectID.Text))
    throw new ArugmentExceptioN(txtAddProjectID.Text);
    }
    catch (ArgumentException)
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }


    But don't do that!



    You already have a boolean telling you the function failed. Just use that with a normal if block:



    if (!validProject(txtAddProjectID.Text))
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }





    this is only one of many conditions that must be met prior to updating the db




    There's still no need for an exception.



    bool validRecord = true;

    validRecord = validRecord && validProject(txtAddProjectID.Text);
    validRecord = validRecord && someOtherCheck();
    validRecord = validRecord && someFinalCheck();
    validRecord = validRecord && howEverManyYouNeed();

    if (!validRecord)
    {
    ShowError();
    }
    else
    {
    UpdateDB();
    }


    You can do a similar thing by just appending to a string or adding to a list that starts out empty. At the end, if the item has any length, show the errors.






    share|improve this answer


























    • Yes but this is only one of many conditions that must be met prior to updating the db

      – Doug Farrell
      Nov 20 '18 at 20:31











    • Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

      – Doug Farrell
      Nov 20 '18 at 21:41













    • @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

      – Nkosi
      Nov 20 '18 at 21:48














    3












    3








    3







    You can throw your own exception:



    try
    {
    if (!validProject(txtAddProjectID.Text))
    throw new ArugmentExceptioN(txtAddProjectID.Text);
    }
    catch (ArgumentException)
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }


    But don't do that!



    You already have a boolean telling you the function failed. Just use that with a normal if block:



    if (!validProject(txtAddProjectID.Text))
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }





    this is only one of many conditions that must be met prior to updating the db




    There's still no need for an exception.



    bool validRecord = true;

    validRecord = validRecord && validProject(txtAddProjectID.Text);
    validRecord = validRecord && someOtherCheck();
    validRecord = validRecord && someFinalCheck();
    validRecord = validRecord && howEverManyYouNeed();

    if (!validRecord)
    {
    ShowError();
    }
    else
    {
    UpdateDB();
    }


    You can do a similar thing by just appending to a string or adding to a list that starts out empty. At the end, if the item has any length, show the errors.






    share|improve this answer















    You can throw your own exception:



    try
    {
    if (!validProject(txtAddProjectID.Text))
    throw new ArugmentExceptioN(txtAddProjectID.Text);
    }
    catch (ArgumentException)
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }


    But don't do that!



    You already have a boolean telling you the function failed. Just use that with a normal if block:



    if (!validProject(txtAddProjectID.Text))
    {
    addErr.Text = "";
    addErr.Text = "Invalid Project ID.";
    }





    this is only one of many conditions that must be met prior to updating the db




    There's still no need for an exception.



    bool validRecord = true;

    validRecord = validRecord && validProject(txtAddProjectID.Text);
    validRecord = validRecord && someOtherCheck();
    validRecord = validRecord && someFinalCheck();
    validRecord = validRecord && howEverManyYouNeed();

    if (!validRecord)
    {
    ShowError();
    }
    else
    {
    UpdateDB();
    }


    You can do a similar thing by just appending to a string or adding to a list that starts out empty. At the end, if the item has any length, show the errors.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 '18 at 21:11

























    answered Nov 20 '18 at 20:27









    Joel CoehoornJoel Coehoorn

    309k96494728




    309k96494728













    • Yes but this is only one of many conditions that must be met prior to updating the db

      – Doug Farrell
      Nov 20 '18 at 20:31











    • Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

      – Doug Farrell
      Nov 20 '18 at 21:41













    • @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

      – Nkosi
      Nov 20 '18 at 21:48



















    • Yes but this is only one of many conditions that must be met prior to updating the db

      – Doug Farrell
      Nov 20 '18 at 20:31











    • Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

      – Doug Farrell
      Nov 20 '18 at 21:41













    • @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

      – Nkosi
      Nov 20 '18 at 21:48

















    Yes but this is only one of many conditions that must be met prior to updating the db

    – Doug Farrell
    Nov 20 '18 at 20:31





    Yes but this is only one of many conditions that must be met prior to updating the db

    – Doug Farrell
    Nov 20 '18 at 20:31













    Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

    – Doug Farrell
    Nov 20 '18 at 21:41







    Joel, Thanks for the answer I will try it and let you know the results. And just for my information can you please tell my why this is superior to throwing an exception?

    – Doug Farrell
    Nov 20 '18 at 21:41















    @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

    – Nkosi
    Nov 20 '18 at 21:48





    @DougFarrell exceptions are expensive and should actually only be thrown for exceptional circumstances (pun intended). check this post stackoverflow.com/questions/891217/…

    – Nkosi
    Nov 20 '18 at 21:48













    0














    try
    {
    validProject(txtAddProjectID.Text);
    }
    catch (ArgumentException e)
    {
    addErr.Text = "";

    addErr.Text = e.Message;
    }


    private static void validProject(object text)
    {
    throw new ArgumentException($"Invalid Project ID.{text}");
    }





    share|improve this answer
























    • This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

      – Lews Therin
      Nov 20 '18 at 20:26
















    0














    try
    {
    validProject(txtAddProjectID.Text);
    }
    catch (ArgumentException e)
    {
    addErr.Text = "";

    addErr.Text = e.Message;
    }


    private static void validProject(object text)
    {
    throw new ArgumentException($"Invalid Project ID.{text}");
    }





    share|improve this answer
























    • This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

      – Lews Therin
      Nov 20 '18 at 20:26














    0












    0








    0







    try
    {
    validProject(txtAddProjectID.Text);
    }
    catch (ArgumentException e)
    {
    addErr.Text = "";

    addErr.Text = e.Message;
    }


    private static void validProject(object text)
    {
    throw new ArgumentException($"Invalid Project ID.{text}");
    }





    share|improve this answer













    try
    {
    validProject(txtAddProjectID.Text);
    }
    catch (ArgumentException e)
    {
    addErr.Text = "";

    addErr.Text = e.Message;
    }


    private static void validProject(object text)
    {
    throw new ArgumentException($"Invalid Project ID.{text}");
    }






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 20 '18 at 20:23









    Ryan SchlueterRyan Schlueter

    1,9921918




    1,9921918













    • This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

      – Lews Therin
      Nov 20 '18 at 20:26



















    • This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

      – Lews Therin
      Nov 20 '18 at 20:26

















    This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

    – Lews Therin
    Nov 20 '18 at 20:26





    This throws the exception every time validProject() is called. The OP asked for it to throw an exception only when validProject() returns false.

    – Lews Therin
    Nov 20 '18 at 20:26











    0














    Just check the result of the call to validProject and throw an exception if it returns false:



    try
    {
    if (!validProject(txtAddProjectID.Text))
    throw new ArgumentException("Argument is invalid", nameof(txtAddProjectID.Text));
    }
    catch (ArgumentException)
    {
    addErr.Text = "";

    addErr.Text = "Invalid Project ID.";
    }





    share|improve this answer




























      0














      Just check the result of the call to validProject and throw an exception if it returns false:



      try
      {
      if (!validProject(txtAddProjectID.Text))
      throw new ArgumentException("Argument is invalid", nameof(txtAddProjectID.Text));
      }
      catch (ArgumentException)
      {
      addErr.Text = "";

      addErr.Text = "Invalid Project ID.";
      }





      share|improve this answer


























        0












        0








        0







        Just check the result of the call to validProject and throw an exception if it returns false:



        try
        {
        if (!validProject(txtAddProjectID.Text))
        throw new ArgumentException("Argument is invalid", nameof(txtAddProjectID.Text));
        }
        catch (ArgumentException)
        {
        addErr.Text = "";

        addErr.Text = "Invalid Project ID.";
        }





        share|improve this answer













        Just check the result of the call to validProject and throw an exception if it returns false:



        try
        {
        if (!validProject(txtAddProjectID.Text))
        throw new ArgumentException("Argument is invalid", nameof(txtAddProjectID.Text));
        }
        catch (ArgumentException)
        {
        addErr.Text = "";

        addErr.Text = "Invalid Project ID.";
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 20:24









        Lews TherinLews Therin

        2,47411539




        2,47411539






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400922%2fbest-way-to-handle-exception-prior-to-updating-database%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?