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

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.

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

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:
Thank you very much.
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.