.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

how to insert null or empty string to datetime var ?

Posted By:      Posted Date: September 15, 2010    Points: 0   Category :Windows Application
hi i get data from xml file and sometime the date is empty. i have this code:     try { TimeTo = Convert.ToDateTime(R[15].ToString()); }         catch { TimeTo = null ; } but i got error because i cant insert null to datetime var what i can do ? thak's in advance

View Complete Post

More Related Resource Links

Dropdown menu - Null or empty string??


I have drop dowm menu as follow.

<asp:ListItem Value="">none</asp:ListItem>
<asp:ListItem Value="STO">Stock</asp:ListItem>
<asp:ListItem Value="ORD">Order</asp:ListItem>

If I chose none, it stores data as "Null"

If I Query the data as below, I don't get the data has value of "Null"

SELECT tabale From type Where type <> STO or type <> ORD

I get data that has empty string but not Null.

Here is my question.

1, How do I write dropdown menu value in oder to get empty string instead of Null?
I did <asp:ListItem Value="">none</asp:ListItem> but this stores Null.

2, Why this Query won't pick up Null?
SELECT mytabale FROM type WHERE type <> STO or type <> ORD

Thank you for your help.

error in MVC2 on empty string form fields which are required: "This property cannot be set to a null

In model I have:[DisplayName("ISBN")] [Required(ErrorMessage = "ISBN is required.")] [StringLength(50, ErrorMessage = "ISBN book must have max {1} chars.")] [DisplayFormat(ConvertEmptyStringToNull = false)] public string isbn { get; set; }But when I submit form with empty field ISBN I have an error - ConstraintException: This property cannot be set to a null value.I have find that other people have the same problem and they say that debbugger in VS2010 works wrong - yes ?http://p2p.wrox.com/book-professional-asp-net-mvc-2/79788-constraintexception-unhandled-user-code.html#post259245http://efreedom.com/Question/1-3129080/Server-Side-Validation-REQUIRED-String-Property-MVC2-Entity-Framework-Workhttp://mvcmusicstore.codeplex.com/workitem/6604

if NULL in database table - display empty string?


I have a table in an Sql Server Database from which I want to fetch data to a form. 

[Headline] [varchar](max) NULL,
[Text] [varchar](max) NULL,

[ID] [int] IDENTITY(1,1) NOT NULL,

[Headline] [varchar](max) NULL,

[Text] [varchar](max) NULL,

I want to check both Headline and Text to see if they are null, and if so display an empty string each for them. I've looked at some tutorials but don't yet understand really how to use it. DBNull? DataRow.IsNull? Something else? Can I check both Headline and Text at the same time or do I need do check them separately?

Webservices with NULL or Empty String


Hi ,

We have developed a Webservice which returns data in the form of xml to client. When we access the webservice from our brower, we were able to see the message with empty string. But in SSRS dataset designing we dont see column at all.


For example:- Webservice developed  returns the XML data to client.When we bound this webservice has datasource for dataset, we dont see column in dataset which contains empty string or null.

How can i have column present in dataset in though XML element value is null or empty.

Like DOB in employee xml is sent to client. But this XML contain DOB with empty data like <DOB></DOB>. Since DOB xml element is empty we could not see this DOB column in dataset of Report. How can show tht DOB column in dataset



Insert null datetime into SQL DB


Hi There,

I have 2 textboxes for BEGINNING DATE and END DATE.
First I compare the dates so that the END DATE is bigger than the BEGINNING DATE. This is OK with the validators.

If the END DATE´S TEXTBOX is empty, then it should insert into the SQL DB, a null value. The field has a DATETIME type.
Since my INSERT Query requires a parameter when i call the ADD METHOD, it returns that the END DATE parameter is required.

Thank you.

ObjectDataSource NULL to Empty String


I have pass following SQL Query to the through ObjectDataSource

SELECT     RequesterID, CompanyID, Name, emailID, Department, JobTitle, MobileNo, LandLine, Ext, Remarks
FROM         Requester
Where Name Like @Name + '%'

And I am passing parameter through QueryString and My idea is like when empty string is pass it should show me all records but when I am passing empty string ObjectDataSource is making NULL and I am not getting desire result

Kindly can somebody help me on that. It is okay if you change query of help me on objectDataSource.

How to store NULL instead of empty string in LinqToSQL scenario


Technology used : VS2008, VB.NET, LinqToSQL, BindingSource

I used LinqToSQL to create a detailsform. So I have a Textbox bound to a Varchar column in the dababase. When I created a record with the form it stores some columns as NULL values which is perfect. When I change the value of the textbox to something and do a db.SubmitChanges() the value is stored. 

However, when I make the textbox empty and save, the value in the database is an empty string. I want it to be NULL. How can i achieve this?


How to check for null/empty strings on dynamic objects?


What is the proper way to check for null or empty strings on dynamic objects? This is the only solution I've found so far:

if (admin.PasswordHash.GetType() == typeof(DBNull) || admin.PasswordHash == string.Empty)

If the field is null, the object returned is DBNull.Value, but if the field is empty string, it's return as a System.String object. This makes it impossible to do regular "admin.PasswordHash == DBNull.Value" check, as that will fail whenever the field has been set to empty string. Is my example the right (and only) way to go?

Linq to SQL returns null for DateTime fields.


I have a linq to sql entity class in which I declared some DateTime fileds like this:

[Column(DbType = "smalldatetime")]
public DateTime? StartDate { get; set; }

And this is how I fetch data form repository:

return repository.Logs.OrderByDescending(l => l.LogID).FirstOrDefault();

All non DateTime fields are filled properly but all datetimes are null.

[Column(DbType = "smalldatetime")]

        public DateTime? StartDate { get; set; }

VS2005 calling VS2010 asmx service returns empty string


Anyone have any experience with this?

We are using VS 2005 for the front end, and vs 2010 for the web services part (asmx), not wcf yet.


Web service works just like the vs2005 when run locally, no problems.


Connecting to it from a test app returns an empty string. Connecting to a 2005 app doesn't!

Anyone run into this before or have a suggestion? Surely vs 2005 can connect to web services from a later version?






SQL Null Insert fails

I am developing an SSIS package and trying to update an existing table with info from new columns.  In other words, this table already has records for my input CSV file.  But this CSV file has additional columns for these same records.  So I need to keep existing columns in table.  How can I achieve this? I began by using Import/Export Wizard and it did most of hard work for me.  But one of the columns previously defined in this table doesn't allow NULLs.  So although I want these new rows to map into existing table, it seems like this will just overwrite the table values and I get the error below.  How can I fix this? I have a Flat File CSV source, Data Conversoin, and DestinationConnectionOLEDB Error: 0xC0202009 at Data Flow Task 1, Destination - j5c_ALL_DATA2 [138]: SSIS Error Code DTS_E_OLEDBERROR. An OLE DB error has occurred. Error code: 0x80004005. An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "The statement has been terminated.". An OLE DB record is available. Source: "Microsoft SQL Server Native Client 10.0" Hresult: 0x80004005 Description: "Cannot insert the value NULL into column 'timestamp', table 'berkeley.dbo.j5c_ALL_DATA2'; column does not allow nulls. INSERT fails.". Error: 0xC0209029 at Data Flow Task 1, Destinati

Data Conversion - string to datetime help!!!

Hi all, I need help on concatenate two fields into one then display it as datetime format as such yyyy-mm-dd hh:mm:ss AM/PM. The db is on SQL 2008 SP1. The date and time columns are on the same table and store as varchar. Date has varchar(10) and time has varchar(16). Date column shows result from query is 20100827 while the time column is either 070101.000000 or 070101. The question is how go about concatenate the two together and display in the format above in a report? How should I write my select query for this report? I am using SQL 2008 R2 BI Development Studio to design my report. Thank you. VCHS

Convertion failed when converting datetime to String

DECLARE @usr datetime SET @usr = user+'::Date' IF NOT EXISTS (SELECT 'X' FROM table WHERE Name = 'File' AND CONVERT(varchar,DateAdd, 101) = CONVERT(varchar, getdate(), 101)) BEGIN INSERT INTO table (Code, Name, Date) VALUES ('DD', 'File',@usr) END the date will be created by another process and its value will be loaded in the variable DATE. I'm calling that variable here. I;m getting the error Convertion failed when converting datetime to String. how to resolve this..??

Convertion failed when converting datetime to String

DECLARE @usr datetime SET @usr = user+'::Date' IF NOT EXISTS (SELECT 'X' FROM table WHERE Name = 'File' AND CONVERT(varchar,DateAdd, 101) = CONVERT(varchar, getdate(), 101)) BEGIN INSERT INTO table (Code, Name, Date) VALUES ('DD', 'File',@usr) END I;m getting the error Convertion failed when converting datetime to String. how to resolve this..??

Varchar string to int - how to convert empty cells to nulls

Hi, I have a large table from which I want to convert certain columns from varchar to int while loading the table to another table. However, I run into problems when the input column contains empty cells. The SSIS data conversion data flow item can't handle these so I tried the script component. With the script component I can convert these empty cells to zeros but after that I'd need to use SQL update to update the zeros to nulls which in the case of a large table with over 3 million rows takes unnecessarily long. So, is there another way?

how to insert null value

hi, All   hear is my statement i am trying to insert null value through statement if (txtBankAcNo.Text == "") { sSqlDbCommand.Parameters["@BankAcNo"].Value = null; }                         else { sSqlDbCommand.Parameters["@BankAcNo"].Value = txtBankAcNo.Text; } but is throwing Error: expects the parameter '@BankAcNo' which is not supplied   Regards!   Hitesh    

how to in Insert database NULL if drop down list item is not selected ?

hi,   on my aspx I have the below DDL   <asp:DropDownList ID="ColorDDL" runat="server" DataSourceID="ColorObj"                                                 DataTextField="Size" DataValueField="BustId" Width="150px"                                                 AppendDataBoundItems="True">                                                    <asp:ListItem Value="-1">(Optional)</asp:ListItem>                             &n
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend