A new expresion requires (), [], or {} after type

In the following class, I have created a new method (protected void SetConnection())  to build connection string to be used via different methods.

At "protected"  I get message: A new expresion requires (), [], or {} after type

Question: How can I correct this?

See line 7.

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");
    }

    public string ReadCriteria(string dataPoint)
    {
        string criteria = "";
        SetConnection();
        SqlCommand command = new SqlCommand();
        command.Connection = cn;
        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:
Two things with this code:

    SqlConnection cn = new SqlConnection    <--- missing ; to terminate the statement (your error)

    protected void SetConnection()
    {
The following defines a local variable that replaces the class level one defined above - remove the SqlConnection in the line
        SqlConnection cn = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True");
    }
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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi Andy,

I have corrected these errors. The only thing is giving me problem is at line at line 19:

 (int)value;  // cannot convert string to int.

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)
            {
               sqlString ="Update RodCtiteria Set [" + dataPoint + "] =" + (int)value;
            }
              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
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I guess the following line ought to work:

              int x = Int32.Parse(value);
               sqlString ="Update RodCtiteria Set [" + dataPoint + "] =" + x;
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

AndyAinscowFreelance programmer / ConsultantCommented:
line 19 should be:
sqlString ="Update RodCtiteria Set [" + dataPoint + "] = " + value;
(which is the same as line 23) assuming it is a string value like 12 or 19.0 but not a string like 1,234
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you Andy,

Your solution sounds better than what I have use. This means, I can get rid of istInt flag. This makes the code simpler. I will change my code accordingly.

BTW, I have a follow up question at:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28494197.html
The ConnectionString property has not been intialized.

I will close this question shortly.

Thanks again,

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.