• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 4
  • 2
1 Solution
 
AndyAinscowCommented:
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
 
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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
AndyAinscowCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now