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

  • SEARCH
  • Home
  • 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 8180009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:07:28+00:00 2026-06-07T00:07:28+00:00

I have a quick issue with my connection not being intialized. When I call

  • 0

I have a quick issue with my connection not being intialized.

When I call the create method, I pass in a bunch of varnames, say “ABC,XYZ, etc”.

On the first pass of the foreach, the stored procedure gets called correctly and things work fine. But on the second pass, I get a “Connectionstring property has not been intialized.

Should I mover the myConn.open and myConn.close outside of the loop?

My code:

public bool Create()
{
        bool isSuccess = true;

        ConnectionStringSettings myConnectionStringSetting = ConfigurationManager.ConnectionStrings[0];
        DbProviderFactory dbFactory = DbProviderFactories.GetFactory(myConnectionStringSetting.ProviderName);

        DbConnection myConn = dbFactory.CreateConnection();
        myConn.ConnectionString = myConnectionStringSetting.ConnectionString;

        DataTable myDataTable = new DataTable();

        string[] varNamesArray = m_variableNames.Split(',');

        foreach (string varName in varNamesArray)
        {
            string vn = varName.Trim();

            //stored procedure

            DbCommand myCommand = dbFactory.CreateCommand();
            myCommand.CommandText = "s_LockVariables";
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Connection = myConn;

            //variables            

            DbParameter param1 = myCommand.CreateParameter();
            param1.ParameterName = "@varName";
            param1.DbType = DbType.String;
            param1.Value = vn;
            myCommand.Parameters.Add(param1);

            DbParameter param2 = myCommand.CreateParameter();
            param2.ParameterName = "@datasetID";
            param2.DbType = DbType.Int32;
            param2.Value = m_datasetID;
            myCommand.Parameters.Add(param2);

            DbParameter param3 = myCommand.CreateParameter();
            param3.ParameterName = "@userID";
            param3.DbType = DbType.Int32;
            param3.Value = m_userID;
            myCommand.Parameters.Add(param3);

            DbParameter param4 = myCommand.CreateParameter();
            param4.ParameterName = "@currentTime";
            param4.DbType = DbType.DateTime;
            param4.Value = DateTime.Now;
            myCommand.Parameters.Add(param4);

            try
            {
                using (myConn)
                {
                    myConn.Open();
                    myCommand.ExecuteNonQuery();
                    myConn.Close();
                }
            }
            catch (Exception e)
            {
                isSuccess = false;
                throw new Exception(e.Message);
                //TODO:  catch data error

                return isSuccess;
            }
        }

        return isSuccess;
    }
  • 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-06-07T00:07:29+00:00Added an answer on June 7, 2026 at 12:07 am

    You’re disposing of myConn within the foreach loop here:

    using (myConn)
    {
        myConn.Open();
        myCommand.ExecuteNonQuery();
        myConn.Close();
    }
    

    I suggest you just change that to declare the variable right there:

    using (DbConnection myConn = dbFactory.CreateConnection())
    {
        myConn.ConnectionString = myConnectionStringSetting.ConnectionString;
        myConn.Open();
        myCommand.Connection = myConn;
        myCommand.ExecuteNonQuery();
        // Closed automatically due to being disposed
    }
    

    Or just have a single using statement outside the loop – you might as well use the same connection for all the commands, right?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this quick issue please. I have this code here which permits me
Quick question that requires a long explanation.. Say I have two tables - one
I have a quick and asap issue. SELECT `deals`.* FROM `deals` WHERE `is_featured` =
I'm not good in css and not able to quick fix this issue. I
I have quick question about text parsing, for example: INPUT=a b c d e
I have quick question for you SQL gurus. I have existing tables without primary
some of our customers have Quick book online and some of our customers have
I have a quick question about something I imagine must be pretty easy -
I have this quick sort implementation to sort the characters. int main(){ char val[]
I have a quick question that I can't seem to find online. I am

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.