How to omit checking for SqlNullValueException in Mysql Connector/NET


Problem :

When reading data from the reader I have to check each parameter that allows for null values in the database whether it is threw a NullValueException. Especially I have to check each value with a separate try/catch because I still want to parse the next value it the first one was null. In C# some classes have a tryParse(key, out value) function, which returns a boolean on success, but I didn't find it for Connector/NET. Is there any way to shorten the following statements?

Product product;
try {
    product = new Product(
        reader.GetString("product_id"),
        reader.GetDateTime("starttime")
        );
    try {
        product.EndTime = reader.GetDateTime("endtime");
    } catch (System.Data.SqlTypes.SqlNullValueException) { }
    try {
        product.Description = reader.GetString("description");
    } catch (System.Data.SqlTypes.SqlNullValueException) { }
    try {
        product.Type = reader.GetString("type");
    } catch (System.Data.SqlTypes.SqlNullValueException) { }
} catch (MySqlException ex) {
    throw ex;
} catch (Exception ex) {
    throw ex;
}


Solution :

I usually use this pattern with SqlDataReader, I imagine it should be the same with MySQL Connector/Net.

product.EndTime = reader["endtime"] as DateTime? ?? DateTime.MinValue;
product.Description = reader["description"] as string;
product.Type = reader["type"] as string;

Mysql Tutorials

Mysql Howto..

How to fetch 2 types of data with a single mysql query?

How to change the time zone in a MySQL database

How can I use mysql partitioning on this table?

How to save passwords with security but i need to get it as clean text later?

How to optimize mysql query with multiple fulltext matches

how to use regex in mysql UPDATE query?

PHP mysql how can i insert an url using tinymce

how to use mysql query inside the javascript, php

How to fetch results from MySQL cursor in Flask? [closed]

How to solve duplicate entry, and instead, have one entry inserted into mysql table?

How to return variables that have been set in MySQL back to PHP

How to display dates between to given values along with another column in MySQL

How to set default port on MySQL for Mac

How to apply CSS to dynamically generated PHP database results

How to format input text from a texarea into mysql and than display it the same way

how to get the last three months of last year using mysql?

How to echo with PHP this MySQL command

POSTGRE how to select parent name from hierarchical table

How to select rows and update them without concurrency issues?

How to I input my mysql database table into this list?

How do I connect to MySQL 5.1 in Visual Studio 2010?

How to optimize a query when grouping on a date / unix timestamp?

MySQL: Two tables share common “date” column, how to ORDER dates together after a JOIN?

How do i know ssl is being used in mysql replication?

MySQL - How to get a list of values in Table A that don't exist in Table B?

How does MySQL know which algorithm to use when reading logins stored in mysql.user?

How to SELECT NOT IN, but taking into account CASE SENSITIVE in Mysql?

With multiple Python installs, how does MacPorts know which one to install MySQLdb for?

how to get looped textbox values with comma seperated in jSON to insert into mysql database in jquery

PHP/MySQL - how to auto generate delete link for a row in an already auto generated table?