Cannot get value because it is DBNull
-
Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me.
if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; }
What is the best way to make this if statement? -
Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me.
if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; }
What is the best way to make this if statement? -
You can use the method IsNull of the DataRow class to check if the value in a cell is DBNull. --- b { font-weight: normal; }
-
Hi, I am using a database. And return the data to a DataSet dsPlayers. Some players have a birthday, which is a DateTime field, and some do not. These have no value so it says . I want to put their birthday to a DateTimePicker dtpBirthday, but when it is I want to give the DateTimePicker the value DateTime.Now. I have the following code, which is noit working for me.
if (dsPlayer.Players[0].BirthDay.ToString() == null) { dtpBirthday.Value = DateTime.Now; } else { dtpBirthday.Value = dsPlayer.Players[0].BirthDay; }
What is the best way to make this if statement?How about trying this? dtpBirthday.Value = dsPlayer.Players[0].BirthDay.ToString()== DBNull.Value ? DateTime.Now : dsPlayer.Players[0].BirthDay ; This is an example of a conditional if statement and will best suit your purpose. The Conditional Statement variable = op1 == op2 ? trueValue : FalseValue ; where: op1 == op2 is your condition Life is Music listen to it before it fades away
-
How about trying this? dtpBirthday.Value = dsPlayer.Players[0].BirthDay.ToString()== DBNull.Value ? DateTime.Now : dsPlayer.Players[0].BirthDay ; This is an example of a conditional if statement and will best suit your purpose. The Conditional Statement variable = op1 == op2 ? trueValue : FalseValue ; where: op1 == op2 is your condition Life is Music listen to it before it fades away
-
When you read the data from the datatable, I call it playerTable here: if (playerTable.Rows[index].IsNull["BirthDay"]) { ... store a value that represents 'no data' } else { ... get the data } --- b { font-weight: normal; }
-
Hi Azerax, I tried something like that but it gives me the following error: Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' That's why I came to codeproject.
Ohh I am sorry :(. Please note the corrected code as follows:
dtpBirthday.Value = dsPlayer.Players[0]["BirthDay"]== DBNull.Value ? DateTime.Now : dsPlayer.Players[0]["Birthday"].ToString() ;
Where birthday is a field in your dataTable. Elvis (a.k.a Azerax) :cool: Life is Music listen to it before it fades -
Hi Azerax, I tried something like that but it gives me the following error: Operator '==' cannot be applied to operands of type 'string' and 'System.DBNull' That's why I came to codeproject.
Ohh I am sorry I got it wrong again . Please note the corrected code as follows: dtpBirthday.Value = dsPlayer.Tables["Players"].Rows[0]["Birthday"]== DBNull.Value ? DateTime.Now : dsPlayer.Tables["Players"].Rows[0]["Birthday"].ToString() ; Sorry for the trouble... Elvis (a.k.a Azerax) Life is Music listen to it before it fades