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

Unable to cast object type 'Syatem.Int32' to type .System.String'.

Here, I am reading values from a table. Reading int type fields give me problem.

At line 14, I am getting error stating: "Unable to cast object type 'Syatem.Int32' to type .System.String'."

Question: How can I handle this.

Thank you.
public string ReadCriteria(string dataPoint)
    {
        string criteria = "";
        using (cn = 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) != "" MetricIdL
                    criteria = reader.GetString(0);   //*********************< error here
            }
            reader.Close();
        }
        return criteria;
    }

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 2
  • 2
1 Solution
 
käµfm³d 👽Commented:
The data type of the data in the reader is of type int, so you cannot use GetString. You have to use the method that is appropriate for that data type:  GetInt32.

e.g.

criteria = reader.GetInt32(0).ToString();

Open in new window


Though you do want to ensure that you won't be getting nulls back from the database. If that be the case, then the logic will need to be tweaked.
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
kaufmed,

Thank you for the solution. The values to be read are often string but sometimes int.

I suppose I need to add error handling here to handle both sting and int values to be read.

Question: If error handling is the way to go, could you please give me the syntax for it.

Later, I will learn about error handling to code it myself but at this point I could use some help.

If error handling is not necessary please add some comment on how would it work. I will try

criteria = reader.GetInt32(0).ToString();

to read both string and int values to see how it goes.

Regards,

Mike

Test Result:
Now I am getting "Specified cast is not valid." (in the case of string data of course).

It seems the following work, but the block is not complete.
try
                    {
                        criteria = reader.GetString(0);
                    }
                    catch
                    {
                   // }
                  //  finally
                   // {
                        criteria = reader.GetInt32(0).ToString();
                    }

Open in new window

0
 
käµfm³d 👽Commented:
Your situation is complicated. You are building your query dynamically via string concatenation. This means that this particular method doesn't have much knowledge about what is coming back from the database. One way you can overcome this is by treating the data as type object. It's not an ideal approach, but it should work.

e.g.

public string ReadCriteria(string dataPoint)
{
    string criteria = "";
    using (cn = 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))
            {
                object data = reader(0);

                criteria = data.ToString();
            }
        }
        reader.Close();
    }
    return criteria;
}

Open in new window

0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you very much.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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