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

asked on

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

Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

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
    }
Avatar of Mike Eghtebas

ASKER

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:     //  {
Avatar of Guy Hengel [angelIII / a3]
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
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
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
I've requested that this question be deleted for the following reason:

I rephrased my question and posted a new one instead.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg 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
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:

https://www.experts-exchange.com/questions/28494258/Unable-to-cast-object-type-'Syatem-Int32'-to-type-System-String'.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
Good alternate solution.

Thanks,

Mike