Check for column name in a SqlDataReader object

public static class DataRecordExtensions { public static bool HasColumn(this IDataRecord dr, string columnName) { for (int i=0; i < dr.FieldCount; i++) { if (dr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase)) return true; } return false; } } Using Exceptions for control logic like in some other answers is considered bad practice and has performance costs. It also sends false positives … Read more

SQL Data Reader – handling Null column values

You need to check for IsDBNull: if(!SqlReader.IsDBNull(indexFirstName)) { employee.FirstName = sqlreader.GetString(indexFirstName); } That’s your only reliable way to detect and handle this situation. I wrapped those things into extension methods and tend to return a default value if the column is indeed null: public static string SafeGetString(this SqlDataReader reader, int colIndex) { if(!reader.IsDBNull(colIndex)) return reader.GetString(colIndex); … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)