Last post Feb 20, 2017 12:33 PM by oned_gk
Feb 20, 2017 05:29 AM|doctork32|LINK
What am I doing wrong? I want NULL in the DB if the control(s) in the ASP page are NOT checked, provided, selected, etc.
command.Parameters.Add("@BfAttempts", SqlDbType.VarChar).Value = (object)txtLabDrawNumAttempts.Text ?? DBNull.Value;
command.Parameters.Add("@Disposition", SqlDbType.VarChar).Value = (object)rbLabDisposition.SelectedValue ?? DBNull.Value;
So if @BfAttempts is not supplied, send NULL to DB.
What I get now is an empty column, no null. I want NULL...
Feb 20, 2017 06:22 AM|KarthickRaju2690|LINK
Can you try like above?
Feb 20, 2017 06:44 AM|Khuram.Shahzad|LINK
I wonder , why you cast textbox control to object, if you know that is textbox control then just get its value like txtLabDrawNumAttempts.Text.
string value that take from text box control may be not null, because an object can be null like a textbox control , but value of object can be null null or empty.
If you still have issues you can initialize your textbox control with some predefined value and put some check on that predefined value.
Feb 20, 2017 06:47 AM|KarthickRaju2690|LINK
Exactly Khuram.Shahzad, dun no why it's been casted. Since text box returns empty if no value passed , only IsNullOrEmpty check is sufficient enough.
Feb 20, 2017 12:33 PM|oned_gk|LINK
I guess you use wrong operator, ?? is null-coalescing, if the left-hand is null then return right-hand value
But left-hand is textboxt text that never has null value
use ?: operator instead
command.Parameters.Add("@BfAttempts", SqlDbType.VarChar).Value = txtLabDrawNumAttempts.Text!=string.Empty ? txtLabDrawNumAttempts.Text : DBNull.Value;