0

Say I have a table and I want to parse it's data. For example, table abc has a column called Seq. I want to parse through the column Seq to see if there are any irregularities. Example: if there's a number > 10 I want to let the user know.

I'm getting 2 errors, one regarding the data type and another regarding my list. What I plan to do with the list is to use it to display the irregularities to the user through a message box,

I don't know much syntax at all So here's what I think:

string sql4 = "select * from abc";
SQLiteCommand command = new SQLiteCommand(sql4, sqlite_conn);

SQLiteDataReader reader = command.ExecuteReader();
Convert.ToInt32(reader);
while (reader.Read())
{
    if (reader["Seq"] > 30) // Getting error: operator cannot be applied to
    // type objects and int
    {
        Parse1.Add(reader["Seq"]);
        // Another error saying that the best overloaded method for the string type int
        // Has some invalid arguments
    }
}

private void button7_Click(object sender, EventArgs e)
{
    foreach (object o in Parse1)
    {
        MessageBox.Show(o.ToString());
    }
}

1 Answer 1

1

This is of type object:

reader["Seq"]

But you're trying to use it as an integer. To do that, you should parse it into an integer first. Maybe something like this:

int seq;
if (int.TryParse(reader["Seq"].ToString(), out seq))
    if (seq > 30)
        Parse1.Add(seq);

C# is a statically-typed language so the data types need to be consistent for the compiler. The "Seq" value in reader may intuitively be a number, but computers don't have intuition. You need to actually convert it into an integer to use it as one.

Sign up to request clarification or add additional context in comments.

11 Comments

It should we worth noting that the original values in the db, column seq are not ints
@user7068: Definitely not. For starters, I would fully expect that to fail on reader since reader is a complex object and not an integer value. Second, even if it did succeed you don't actually assign the result to anything, so you never use the converted integer. In any event, int.TryParse() is usually a better approach for converting an integer-ish value (from a string, for example) into an int.
@user7068: The approach doesn't logically make sense anyway. In the while loop is where you access each individual numeric value from the data. So you need to convert that value (reader["Seq"]) to an integer within the loop. For values which aren't integers (as you indicate can happen), what do you plan to do? Skip/ignore them? int.TryParse() returns false if the conversion to an integer fails, so my code will silently ignore non-integer values. If you want to respond to them in some way, just add an else block.
@user7068: The call to int.TryParse() stores the result of the conversion in seq (notice that it's an out parameter... which I'm normally against for this very reason, in that it's not intuitive, but for TryParse() it's necessary because the return value is already a bool indicating whether or not the conversion was successful).
@user7068: All that method does is try to convert the first argument into an integer and set that integer to the second argument. If the conversion succeeds, the second argument will have the integer value and the return value will be true. If it fails, the second argument will have its default value (0) and the return value will be false.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.