?
Solved

The ConnectionString property has not been intialized.

Posted on 2014-08-09
4
Medium Priority
?
330 Views
Last Modified: 2014-08-09
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

0
Comment
Question by:Mike Eghtebas
  • 2
4 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 1200 total points
ID: 40250973
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
 
LVL 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 800 total points
ID: 40250979
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40250980
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40251020
Thank you.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question