?
Solved

add new method to a class...

Posted on 2014-08-09
9
Medium Priority
?
228 Views
Last Modified: 2014-08-09
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

0
Comment
Question by:Mike Eghtebas
  • 6
  • 2
9 Comments
 
LVL 40
ID: 40250832
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40250840
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40250878
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40250883
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40250890
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40251156
I've requested that this question be deleted for the following reason:

I rephrased my question and posted a new one instead.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40251157
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
 
LVL 34

Author Comment

by:Mike Eghtebas
ID: 40251180
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
 
LVL 34

Author Closing Comment

by:Mike Eghtebas
ID: 40251184
Good alternate solution.

Thanks,

Mike
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question