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.