Handle DBNull in C#

The shortest (IMHO) is: int stockvalue = (reader[“StockValue”] as int?) ?? 0; Explanation: If reader[“StockValue”] is of type int, the value will be returned, and the “??” operator will return the result If reader[“StockValue”] is NOT of type int (e.g. DBNull), null will be returned, and the “??” operator will return the value 0 (zero).

Multiples Table in DataReader

Try this because this will close connection ,data reader and command once task get over , so that this will not give datareader close exception Also do check like this if(reader.NextResult()) to check there is next result, using (SqlConnection connection = new SqlConnection(“connection string here”)) { using (SqlCommand command = new SqlCommand (“SELECT Column1 FROM … Read more

How can I easily convert DataReader to List? [duplicate]

I would suggest writing an extension method for this: public static IEnumerable<T> Select<T>(this IDataReader reader, Func<IDataReader, T> projection) { while (reader.Read()) { yield return projection(reader); } } You can then use LINQ’s ToList() method to convert that into a List<T> if you want, like this: using (IDataReader reader = …) { List<Customer> customers = reader.Select(r … Read more

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