Trying to assign a value but nothing gets assigned

I am trying to assign a Boolean value to my variable called flag.  For some reason, I am unable to assign anything to the return value.  How could I write this code so that the returned Boolean value picks up one of the values that gets assigned in the if statement?  The code is below..thanks!

private bool CompareSiteDBNames(string dbname)
        {
            string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TestConnection"].ToString();

            System.Data.SqlClient.SqlConnection sqlCon = new System.Data.SqlClient.SqlConnection(connectionString);

            try
            {
                sqlCon.Open();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }

            System.Data.SqlClient.SqlCommand sqlCom = new SqlCommand();

            sqlCom.Connection = sqlCon;

            sqlCom.CommandType = CommandType.StoredProcedure;

            sqlCom.CommandText = "sp_databases";
            System.Data.SqlClient.SqlDataReader SqlDr;
            
            
            SqlDr = sqlCom.ExecuteReader();            

            string matcheddb;
            bool flag;

            while (SqlDr.Read())
            {
                string retrieveddbnames;

                retrieveddbnames = SqlDr.GetString(0);

                if (dbname == retrieveddbnames)
                {
                    if (dbname.Length > 0)
                    {
                        Session["dbname"] = dbname;
                        flag = true;
                        valCustDBValidator.IsValid = false;
                        valCustDBValidator.Visible = true;
                        
                    }
                    else
                    {
                        flag = false;
                    }
                }
                
            }

         

            return flag;
        }

Open in new window

VBBRettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
instead of
            bool flag;
give it a default value eg.
            bool flag = false;

If you look very carefully you only ever enter the if statement where you assign a value when certain conditions are true.
It needs SqlDr.Read() to give a value of true to enter the while loop AND it then needs dbname == retrieveddbnames to be true to actually get to the inner if statement where you assign a value.

ps. This is weird
            try
            {
                sqlCon.Open();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
Is sqlCon open or not at this point - you do not know.
0
VBBRettAuthor Commented:
This doesn't make a difference to add a value, trust me.  Ultimately it doesn't get the correct value which should be coming from the if statement.  And as far as the connection to the server, I am not looking for a solution for those lines of code.   And as far as stepping through the code, the code never stops or steps through the catch(exception ex) part of the connection command so I would assume the connection works just fine.
0
guru_samiCommented:
If you set the breakpoint at line 42 and 49 where you are setting the flag, did you hit any of those when you debug?
0
AndyAinscowFreelance programmer / ConsultantCommented:
As I said initially to get to where you assign the values a number of things need to be true.  Have you actually checked it is getting into the section you assign the values ?

Everyone makes mistakes (myself included) and the most common one is assuming something which is not correct.

Connection to server - that was in a ps.  I just pointed out that it was a 'nonsense' section of code and probably did not serve any purpose.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
VBBRettAuthor Commented:
It turns out that I wasn't getting an error, I just didn't have rights to the stored procedure that I was running and so therefore I was getting a null value in the loop when in fact I should have gotten a list of database values.  That has been rectified and now everything seems fine.  I appreciate both of your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.