Shweta Gupta
Shweta Gupta

Reputation: 7

Could not find specified column in results in MySQL

I am getting following error while executing simple select statement in my custom code. Could not find specified column in results

Here is my code-

string queryBuilder="select BASKET_DESCRIPTION from MARKET_BASKET_REQUESTS order by BASKET_DESCRIPTION limit 1";
 public T SelectSingle<T>(string queryBuilder) where T : new()//made new
        {
            T result = new T();
            TableScheme dbTable = GetTableSchemeFromType(typeof(T));
            IDataReader reader = ExecuteReader(queryBuilder); 
            result = ParseDataReaderToEntityListtttt<T>(reader,dbTable);
            reader.Close();
            return result;
        }
        private T ParseDataReaderToEntityListtttt<T>(IDataReader reader, TableScheme dbTable) where T : new()
        {
            Type type = typeof(T);
           T result = new T();
            while (reader.Read())
            {
                T t = new T();
                foreach (var column in dbTable.Columns)
                {
                    type.GetProperty(column.AssociatedPropertyName).SetValue(t, reader[column.ColumnName], null);
                }
                result = t;
            }
            return result;
        }

Upvotes: 0

Views: 2685

Answers (1)

CodeCaster
CodeCaster

Reputation: 151588

Your SELECT statement only selects the BASKET_DESCRIPTION column, while your TableScheme (which is generated by the not shown method GetTableSchemeFromType(), but I guess it uses Type.GetProperties() to get all properties) is requesting other columns by name which aren't present in the result set, which is what the error is trying to tell you.

So either SELECT * (or at least all relevant columns), or stop building your own ORM and use an existing one.

Upvotes: 1

Related Questions