0

I was unable to find a answer that helped me so here we go,

I have this function that makes a command for my SQLiteDataAdapter, Now it works all nice in my program that uses FireBird, sadly enough im now working on a mobile framework and it doesnt seem to work here.

public DataTable Get(string tableName, string[] selectColumns, string[] conditionColumns,
                         object[] conditionValues)
    {
        if (conditionColumns.Length != conditionValues.Length)
        {
            throw new Exception("Length of columns and values is different");
        }

        DataTable dt = new DataTable();
        StringBuilder stringB = new StringBuilder();
        try
        {
            stringB.Append("SELECT ");
            if (selectColumns == null)
            {
                stringB.Append("* ");
            }
            else
            {
                for (int i = 0; i < selectColumns.Length; i++)
                {
                    stringB.Append(selectColumns[i]);
                    if (i == selectColumns.Length - 1)
                    {
                        stringB.Append(" ");
                    }
                    else
                    {
                        stringB.Append(", ");
                    }
                }
            }
            stringB.Append("FROM ");
            stringB.Append(tableName + " ");
            stringB.Append("WHERE (");
            for (int i = 0; i < conditionColumns.Length; i++)
            {
                stringB.Append(conditionColumns[i] + " = @" + conditionColumns[i]);
                if (i < (conditionColumns.Length - 1))
                {
                    stringB.Append(" AND ");
                }
                else
                {
                    stringB.Append(")");
                }
            }
            SQLiteDataAdapter da = new SQLiteDataAdapter(stringB.ToString(), connection);

            for (int i = 0; i < conditionColumns.Length; i++)
            {
                da.SelectCommand.Parameters.Add("@" + conditionColumns[i], (DbType)conditionValues[i]);
            }
            da.Fill(dt);
            return dt;
        }
        catch (Exception e)
        {
            throw new Exception("Database error :\r\n" + stringB + "\r\n" + e.Message, e);
        }
    }

Now this i my function, The error gets thrown at

da.SelectCommand.Parameters.Add("@" + conditionColumns[i], (DbType)conditionValues[i]);

with the error

Database error : SELECT * FROM ANIMALS WHERE (animal_number = @animal_number) InvalidCastException

I am not sure wat to do now, i cast DbType since its the only type it seems to accept, but still throws the error.

1 Answer 1

1

Since conditionValues[i] returns object, you can't explicitly parse it to DbType.

You can check your object with is operator to get it's compatible type or not.

For example;

if(conditionValues[i] is int)
{
     da.SelectCommand.Parameters.Add("@" + conditionColumns[i], DbType.Int32).Value = conditionColumns[i];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Seems to have fixed it, Thanks
So quick question that als has to do with casts, i am trying to pull a item from my SQLite database like this animal.FeedScheduleType = (BcFeedScheduleType)drAnimal["feedschedule_type"]; Where BcFeedScheduleType is a public enum, Now it throws me a InvalidCastException you have any idea why? or how to fix that?
Try something like animal.FeedScheduleType = (BcFeedScheduleType)Convert.ToInt32(drAnimal["feedschedule_type"]);

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.