Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've created a code that accesses various sql servers extracts data to a dataTable and then calculates the average between those various tables to then display it on a webform.

Long story short: I wanted the program to bypass/ignore invalid user inputs and still display the averages in the form a table.

Here is what I've tried below:

getUserInput();
                foreach (string item in itemList)
                {//
                    connectionString = string.Format(theString, item); //my modified connection string to allow user to open sql server connection.
                    using (SqlConnection sqlConnect = new SqlConnection(connectionString))
                    {
                                try
                                {
                                    sqlConnect.Open();
                                }
                                catch
                                {
                                    continue;
                                }
                                    sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
createTable();
}


What I have tried:

try
                                {
                                    sqlConnect.Open();
                                }
                                catch
                                {
                                    continue;
                                }
Posted
Updated 5-Apr-19 2:48am
Comments
Graeme Cooper 1-Apr-19 11:40am    
Just to add more context to the problem I'm facing; when the foreach loop comes across the invalid input it ignores the string and my table doesn't get generated.
RmcbainTheThird 1-Apr-19 14:40pm    
So what are the inputs valid or invalid and how are they used? What do they have to do with the sql query?
Graeme Cooper 2-Apr-19 7:16am    
the inputs (list<string>) are entered on the Webform; valid inputs generate a table of averages. The information for table is taken from a SQL query and that information is only accessible if the inputs are valid.
What I was searching for is way to generate that table regardless of an invalid input or have a way of removing the invalid input and generate the table.

If the Connection fails to open, there is nothing more you can do: you cannot proceed to use the connection - that's like turning up the radio in your car to cover the sound of metal on tarmac because all four tires have burst ... It's not going to "magically go away" and work the next time you try.

try...catch isn't there to ignore problems - it's there so you can gracefully fail and report them without your application crashing out on your user.

Quote:
I'm not trying to ignore the problem per say, I'm trying to get the program to generate the table of averages regardless of the failed connection.

So if I had 3 connections; the first and third connection in the list work however the second doesn't. I don't want the foreach loop to end on the second connection, I want it to grab the information it can from the two working connections to form the table.


Then the try block has to surround ALL the code that uses it, not just the attempt to open the connection:
C#
foreach (string item in itemList)
    {
    try
        {
        connectionString = string.Format(theString, item); /
        using (SqlConnection sqlConnect = new SqlConnection(connectionString))
            {
            sqlConnect.Open();
            sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
            createTable();
            ...
            }
        }
    catch {} // Ignore connections that don't work: we want the average of the others.
    }
 
Share this answer
 
v2
Comments
Graeme Cooper 2-Apr-19 7:36am    
I'm not trying to ignore the problem per say, I'm trying to get the program to generate the table of averages regardless of the failed connection.

So if I had 3 connections; the first and third connection in the list work however the second doesn't. I don't want the foreach loop to end on the second connection, I want it to grab the information it can from the two working connections to form the table.
OriginalGriff 2-Apr-19 8:21am    
Answer updated.
Remove items while iterating a collection in C# | CodeAddiction.net[^]

This works!!! @OriginalGriff Thanks for the help I attempted your suggestion before, however I still had the same error. what I've done is below; it works but only if the invalid isn't at the end of the itemList (just ended a message saying to remove invalid from the user input).

In my case:
foreach (string item in itemList.ToArray())
    {
    try
        {
        connectionString = string.Format(theString, item); /
        using (SqlConnection sqlConnect = new SqlConnection(connectionString))
            {
            sqlConnect.Open();
            sqlQuery1 = new SqlDataAdapter("my sql query",sqlConnect);
            createTable();
            ...
            }
        }
    catch {
itemList.Remove(item);
continue;
} // Ignore connections that don't work: we want the average of the others.
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900