locked
Database.Open() throws an exception - WebMatrix/C# RRS feed

  • Question

  • User-2081248521 posted

    Hi, I have a problem. Every once and a while the following line of code throws an exception.

    db = Database.Open ("company_database");

    I would like to put the code in a try-block but according to the MSDN --> assignments in a try-block are not recognized outside the try-block. How do I handle this problem? The system only throws an exception every once and a while. And once in front of the potential customer :-{

    // Here's what I would like to do

    @{

    WebMatrix.Data.Database db;

    try {

          db = Database.Open ("company_database");

    }

    catch { }

    var table_records = db.Query ("SELECT * FROM Main_table");

    }

    // Here's what MSDN says:

    http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.110%29.aspx

    static void Main() {

    int n;

         try {

             // Do not initialize this variable here.

             n = 123;

         }

         catch { }

         // Error: use of unassigned local variable 'n'

        Console.Write(n);

    }

    Tuesday, June 18, 2013 10:40 PM

Answers

  • User-821857111 posted

    You can reassign variables within the try block, but they must have been assigned outside of the block for them to be available to other code outside of the block. If the try part fails, then the variable will not be assigned a value and hte compiler will not allow that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 22, 2013 3:36 AM

All replies

  • User1526116210 posted
    Can you please show the code you are using on your site that throws the error?
    Wednesday, June 19, 2013 10:46 PM
  • User-821857111 posted

    You can initialise variables outside of the block with default values:

    Database db = null;
    int n = 0;
    IEnumerable<dynamic> data = null; 
    var result = string.Empty;
    try{
       db = Database.Open("MyDatabase");
       data = db.Query("SELEECT * FROM Table");
    }
    catch(Exception ex){
    } if(data != null){ //try worked }



    Thursday, June 20, 2013 1:06 AM
  • User-2081248521 posted

    Sure, here it is. Thanks for helping.

     

    @{

        Layout = "~/_Head.cshtml";

    Page.Title = "Rim project";

        bool while_1 = true;

        string must_match_all_fields = "Selected";

        string any_match = "";

        bool exact_match;

        bool use_plusandminus;

        double pm_value;

        string table_name;

        bool search_in_progress = false;

        bool only_records_that_match_all_fields = false;

        string search_results = "Un-initialized";

        string form_value;

        bool successful_parsing_of_forms = true;

        string result_of_the_form_parse = "";

        var message = "message is un-initialized";

        var message1 = "message1 is un-initialized";

        var message2 = "message2 is un-initialized";

        var title = "";

        var type = "";

        var year = "";

        int result;

        string temp_string;

        var prod_num = "";

        var prod_name = "";

        var prod_type = "";

        double prod_offset = 22.44;

        double prod_diameter;

        double prod_width;

        int prod_num_of_lugs;

        double prod_lug_spacing;

        string prod_num_menu_value = "prod_num_menu_value is un-initialized";

        string prod_num_how_to_match = "prod_num_how_to_match is un-initialized";

        string prod_num_exact_match = "";

        string prod_num_char_match = "Selected";

        string offset_exact_value = "";

        string offset_menu_value = "offset_menu_value is un-initialized";

        string offset_pm_0_1 = "Selected";

        string offset_pm_0_2 = "";

        string offset_pm_0_3 = "";

        string offset_pm_0_4 = "";

        string offset_pm_0_5 = "";

        string offset_msg = "";

        var db = Database.Open ("company_database");

        var table_records = db.Query ("SELECT * FROM Main_table");

        var grid_main_table_records = new WebGrid (source: table_records, rowsPerPage: 12);

    Thursday, June 20, 2013 1:14 AM
  • User-2081248521 posted

    Thank you, that is what I'll do. I feel better. But why does this web page say that I can not assign a value within a try block? Is this an initialize versus assign a value to issue?

    http://msdn.microsoft.com/en-us/library/0yd65esw%28v=vs.110%29.aspx

    static void Main() {

    int n;

         try {

             // Do not initialize this variable here.

             n = 123;

         }

         catch { }

         // Error: use of unassigned local variable 'n'.

         Console.Write(n);

    }

    Thursday, June 20, 2013 1:27 AM
  • User-821857111 posted

    You can reassign variables within the try block, but they must have been assigned outside of the block for them to be available to other code outside of the block. If the try part fails, then the variable will not be assigned a value and hte compiler will not allow that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 22, 2013 3:36 AM
  • User-2081248521 posted

    Oh, that finally makes sense. Thanks for taking your time to help me understand how everything works. I appreciate it.

    Robert

    Saturday, June 22, 2013 4:41 PM