The ConnectionString property has not been intialized.

At line 38, I am getting error stating: The ConnectionString property has not been intialized.

But, at line 35 I am executing SetConnection(); to set the connection string.

Do I need to change cn to static?

like:

    static SqlCommand command = new SqlCommand();
    static SqlConnection cn = new SqlConnection();

Question: How can I correct this error?

Thank you.
public class RodCriteria
{
    SqlCommand command = new SqlCommand();
    SqlConnection cn = new SqlConnection();

    protected void SetConnection()
    {
        SqlConnection cn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True");
        command.Connection = cn;
    }

    public void UpdateCriteria(string dataPoint, string value, bool isInt)
    {
        string sqlString = "";
        SetConnection();
        if (isInt)
        {
            int x = Int32.Parse(value);
            sqlString = "Update RodCtiteria Set [" + dataPoint + "] =" + x;
        }
        else
        {
            sqlString = "Update RodCtiteria Set [" + dataPoint + "] = " + value;
        }
        command.CommandText = sqlString;
        command.CommandType = CommandType.Text;

        cn.Open();
        command.ExecuteNonQuery();
    }

    public string ReadCriteria(string dataPoint)
    {
        string criteria = "";
        SetConnection();
        command.CommandText = "SELECT Top 1 [" + dataPoint + "] From RodCtiteria";
        command.CommandType = CommandType.Text;
        cn.Open();
        SqlDataReader reader = command.ExecuteReader();
        if (reader.HasRows)
        {
            reader.Read();
            if (!reader.IsDBNull(0)) // instead of: reader.GetString(0) != ""
                criteria = reader.GetString(0);
        }
        reader.Close();
        return criteria;
    }
    public RodCriteria()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
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:
As I mentioned in your previous question the SetConnection declares and uses a local variable which overrides the class level variable with the same name - it does NOT initialise the class level variable.

You require something like this:
   protected void SetConnection()
    {
//set the class level variable
        cn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True");
        command.Connection = cn;
    }
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
Fernando SotoRetiredCommented:
Try it like this.

   SqlCommand command = new SqlCommand();
   // Change the below line
    SqlConnection cn = null;

    protected void SetConnection()
    {
        // Change the below line
        cn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True");
        command.Connection = cn;
    }

Open in new window

0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thanks Andy, I also had problem with closing the connection. So, while I was waiting for your response, I had time to revise it a bit as shown below. I am including here in case you have some comments on it:
public class RodCriteria
{
    SqlCommand command = new SqlCommand();
    SqlConnection cn = new SqlConnection();

    protected SqlConnection SetConnection()
    {
        cn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True");
        command.Connection = cn;
        return cn;
    }

    public void UpdateCriteria(string dataPoint, string value)
    {
        string sqlString = "";
        using (cn = SetConnection())
        {
            sqlString = "Update RodCtiteria Set [" + dataPoint + "] = " + value;
            command.CommandText = sqlString;
            command.CommandType = CommandType.Text;
            cn.Open();
            command.ExecuteNonQuery();
        }
        
    }

    public string ReadCriteria(string dataPoint)
    {
        string criteria = "";
        using (cn = SetConnection())
        {
            command.CommandText = "SELECT Top 1 [" + dataPoint + "] From RodCtiteria";
            command.CommandType = CommandType.Text;
            cn.Open();
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                reader.Read();
                if (!reader.IsDBNull(0)) // instead of: reader.GetString(0) != ""
                    criteria = reader.GetString(0);
            }
            reader.Close();
        }
        return criteria;
    }
    public RodCriteria()
    {
        //
        // TODO: Add constructor logic here
        //
    }
}

Open in new window

My apology for missing the point you have made earlier. I suppose returning cn via a function call pretty much does what you have commented earlier.

brb

Hi Fernando Soto,
Sorry I didn't see your comment prior to my post.

Regards,

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.