Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have tried different method to do this but none is working:

<pre lang="c#">
string strListOthLog = ((TextBox)fvAddBookingInfo.FindControl("txtOtherLogList")).Text;
string strComment = ((TextBox)fvAddBookingInfo.FindControl("txtComment")).Text;
.......
 cmd.Parameters.Add("@other_logon_list", SqlDbType.NVarChar).Value = string.IsNullOrEmpty(strListOthLog) ? (object)DBNull.Value : strListOthLog;
   
if (string.IsNullOrEmpty(strComment))
            {
                cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = System.DBNull.Value;
            }
            else
            {
                cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = strComment;
            }


What I have tried:

System.DBNull.Value in different ways.
Posted
Updated 26-Aug-16 2:40am
Comments
Maciej Los 18-Aug-16 12:17pm    
"Not working" is not informative at all!
What's your issue? What's wrong wiht above code? What kind of error do you get?
Member 12671834 18-Aug-16 12:25pm    
I don't get any error but it does not enter null in the database, it enter blank.
Karthik_Mahalingam 18-Aug-16 12:26pm    
try passing 'null' value
Maciej Los 18-Aug-16 16:56pm    
Does the database accept null values for that field? Why to add comment if its connent is null?!? Illogical...
Bernhard Hiller 19-Aug-16 4:07am    
HOW do you see that it is "blank" instead of "null"? What's your editor/viewer?

use null instead of DBNull.Value

C#
cmd.Parameters.Add("@other_logon_list", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(strListOthLog) ? null : strListOthLog;
cmd.Parameters.Add("@additional_comments", SqlDbType.NVarChar).Value = string.IsNullOrWhiteSpace(strComment) ? null : strComment;


DBNull.Value:
DBNull.Value is for the purpose of working with databases. A database column can contain a null value. However, when that data is selected into an object (such as a DataTable) in code, there is an object there to reference as far as the code is concerned. However, that object contains a representation of a null value from the database. Thus, DBNull.Value exists to represent that distinction. It basically means "There is a C# object here which grabbed a value from a database, but that value is null."


refer null vs DBNull.Value[^]
 
Share this answer
 
Comments
ZurdoDev 19-Aug-16 11:39am    
"DBNull.Value is for the purpose of working with databases." - DBNull.Value is appropriate for SqlCommand.
This code will work
C#
if (strListOthLog == "")
  cmd.Parameters.AddWithValue("@other_logon_list", DBNull.Value);
else
  cmd.Parameters.AddWithValue("@other_logon_list", strListOthLog);
 
Share this answer
 
Comments
Maciej Los 18-Aug-16 17:10pm    
Well...
Of course, this should work as well, but - in my opinion - that does not resolve OP's issue. Please, read my comment to the OP's comment. Mayby i'm wrong, but is there any sense to insert useless data to the database? ;)
ZurdoDev 18-Aug-16 19:55pm    
I don't understand your comment.
Member 12671834 19-Aug-16 11:37am    
@RyanDev: I tried but this didn't work. Thanks for trying to help.
ZurdoDev 19-Aug-16 11:38am    
It does work, so please explain what you mean by "this didn't work."
I thank you all for helping me. I tried all the above mentioned ways one at a time and was not successful entering null value instead of blank empty space in a table. Finally I got it worked by doing the following:
In the stored procedure I set the default value null for the fields that are not required (user may choose not to enter a value).
@additional_comments varchar (500) = null
Then in the lbInsert_Click event I have following:
string strComment = ((TextBox)fvAddBookingInfo.FindControl("txtComment")).Text;
....
if (strComment != String.Empty)
{
cmd.Parameters.Add("@additional_comments", SqlDbType.VarChar).Value = strComment;
}
This is working for me.
Thanks again to all of you for your support.
 
Share this answer
 
you change in db set column accept null also.

if(txtval=="")
{
val=" ";

}
else
{
val=txtval;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900