add new method to a class...

In the following class, I have two methods:

1. public void ReadCriteria(string dataPoint)      <-- this one functions okay
2. public void UpdateCriteria(string dataPoint, string value, isInt as boolean)

The 2nd one is what I need to add to this class. Please note that UpdateCriteria() has different signature. The value to be updated for  dataPoint field is stored in value as string and isInt parameter indicate if the data type in the table is integer so we can cast it as int before updating it.

Question: Could you please help me to write UpdateCriteria() method?

Thank you.
public class RodCriteria
{
   public void UpdateCriteria(string dataPoint, string value, isInt as boolean)
    {

    public string ReadCriteria(string dataPoint)
    {
   //     string criteria = "";
   //     using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
        {
   //         SqlCommand command = new SqlCommand();

   //         command.Connection = connection;
    //        command.CommandText = "SELECT Top 1 [" + dataPoint + "] From RodCtiteria";
    //        command.CommandType = CommandType.Text;

   //         connection.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 void ReadCriteria(string dataPoint)
    {

    public string ReadCriteria(string dataPoint)
    {
        string criteria = "";
        using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
        {
            SqlCommand command = new SqlCommand();

            command.Connection = connection;
            command.CommandText = "SELECT Top 1 [" + dataPoint + "] From RodCtiteria";
            command.CommandType = CommandType.Text;

            connection.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.

Jacques Bourgeois (James Burger)PresidentCommented:
You are OK, you just forgot the second bracket to terminate the method:


   public void UpdateCriteria(string dataPoint, string value, isInt as boolean)
    {
         // Your code here
    }
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
re:> // Your code here

This is what I am looking for. How to do the update.


Here is some modifications to my original post:
Line 10:      //{
Line 31:      //public void ReadCriteria(string dataPoint)
Line 32:     //  {
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Please clarify what exactly you are looking for
Ee is not doing your homework or your coding
As you are already using sql objects I see nothing you need help with exactly
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I came up with:
   public void UpdateCriteria(string dataPoint, string value, isInt as boolean)
    {

        string sqlString = "";
        using (SqlConnection connection = new SqlConnection("Data Source=USER-PC;Initial Catalog=ROD_July18;Integrated Security=True"))
        {
            SqlCommand command = new SqlCommand();

            command.Connection = connection;
            
            if (isInt)
            {
               sqlString ="Update RodCtiteria Set [" + dataPoint + "] = ' + (int)value;
            }
              else
            {
              sqlString ="Update RodCtiteria Set [" + dataPoint + "] = ' + value;
            }

            command.CommandText = sqlString ;           
            command.CommandType = CommandType.Text;

            connection.Open();
            command.ExecuteNonQuery();    
    }

Open in new window


brb
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Hi a3,
Sorry making you worry. This is not homework. I am new with .net thus there are some uncertainties at play once in a while.

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
I've requested that this question be deleted for the following reason:

I rephrased my question and posted a new one instead.
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
actually, this is the point where the experts can really chime in, your code is not OK at this point:
 
            if (isInt)
            {
               sqlString ="Update RodCtiteria Set [" + dataPoint + "] = ' + (int)value;
            }
              else
            {
              sqlString ="Update RodCtiteria Set [" + dataPoint + "] = ' + value;
            }

Open in new window


it must be:

 
            if (isInt)
            {
               sqlString ="Update RodCtiteria Set [" + dataPoint + "] = ' + (int)value;
            }
              else
            {
              sqlString ="Update RodCtiteria Set [" + dataPoint + "] = '" + value.Replace("'", "''") + "'";
            }

Open in new window


this to avoid sql injection
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:
a3,

Thank you for the solution. What I found out is I shouldn't worry about casting the values. Apparently, the code implicitly handles it.

However, when reading the values, I have to cast int32. This is why I have a new question at:

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28494258.html

and looking for a solution as to how I can handle it.

Please take a look at this question and help me out.

Mike
0
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Good alternate solution.

Thanks,

Mike
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.