Link to home
Start Free TrialLog in
Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Eghtebas

ASKER

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
Thank you.