Obviously your column is not of type string . Apparently this is an int . Therefore use:
dr.getInt32(1).ToString()
or even
dr.GetValue(1).ToString()
which should be larger than roubst to enter changes to the database.
As some general advice, I try to follow at least:
- Choose only what you need. This is mainly due to performance considerations and the reason that you must explicitly specify column names, thereby getting at least a reasonable error if you are incompatible with your schema.
Access fields using their names, for example
dr.GetGuid(dr.GetOrdinal("id"))
Such a thing can also be well solved using the extension method:
public T GetFieldValue<T>(this DbDataReader reader, string columnName) { return reader.GetFieldValue<T>(reader.GetOrdinal(columnName)); }
Side note. Including stack traces (or at least telling which line in the code the exception comes from) may be useful to others who are trying to help you. As you can see from wild guesses, what could be a criminal. I assume the stack trace looks something like this:
SqlDataReader.GetString YourCode.YourMethod
and that GetString looks something like this:
public string GetString(int index) { return (string) GetValue(index); }
source share