Mike Eghtebas
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.
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
//
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.
ASKER
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