Results 1 to 10 of 10

Thread: [2005] Save DBNULL

  1. #1

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Question [2005] Save DBNULL

    Hi all
    How to save dbnull in the sql server database

    basically i have datetype column in table and on the form i have chacked dtpicker

    if the dtpicker is chacked then it save date other wise it save DBNull
    Last edited by shakti5385; Jul 12th, 2007 at 08:04 AM.

  2. #2

    Thread Starter
    Just Married shakti5385's Avatar
    Join Date
    Mar 2006
    Location
    Udaipur,Rajasthan(INDIA)
    Posts
    3,747

    Re: [2005] Save DBNULL

    any idea
    It Saving 1 Jan 1900 in the database when the dtpicker is not checked!!

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Save DBNULL

    I use the method discussed in this thread:
    http://www.vbforums.com/showthread.p...79#post2881879

    Just change the # to single qoutes ( ' )
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Save DBNULL

    YEs... that's what databases do.... a bit odd that it's 1 Jan 1900 and not 31 Dec 1899 though.... but that's how NULL dates are stored in most databases.


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Save DBNULL

    Why? I can save NULL to a datetime field in a database with out any problems?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6
    Member apolizoi's Avatar
    Join Date
    Jan 2005
    Location
    Greece
    Posts
    44

    Re: [2005] Save DBNULL

    If you want to do it through VB.Net try this
    Code:
    MyDataTable.Rows(i)("YourDateFieldName") = DBNull.Value
    If you want to do it through SQL try this :

    YourDateFieldName= NULL

  7. #7
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Save DBNULL

    If you use a parameterized query you can set the parameter value to DBNull.Value

    This is my ExecuteQuery Function
    it accepts Parameters in the form of @param,Value
    If Value is empty it sets the parameter value to DBNull.Value
    Code:
        Public Function ExecuteQuery(ByVal SQL As String, ByVal ParamArray Parameters As String()) As Boolean
            Dim con As New SqlClient.SqlConnection(ConnString)
            Dim oCmd As New SqlClient.SqlCommand(SQL, con)
    
            If Not Parameters Is Nothing Then
                Dim split As String()
                For Each p As String In Parameters
                    Dim delimeter As Char() = {","c}
                    split = p.Split(delimeter, 2)
                    If split(1) = String.Empty Then
                        oCmd.Parameters.AddWithValue(split(0), DBNull.Value)
                    Else
                        oCmd.Parameters.AddWithValue(split(0), split(1))
                    End If
                Next
            End If
            Try
                con.Open()
                If oCmd.ExecuteNonQuery() > 0 Then
                    ExecuteQuery = True
                Else
                    ExecuteQuery = False
                End If
            Catch ex As Exception
                ErrorMessage = ex.Message
                ExecuteQuery = False
            Finally
                If Not oCmd Is Nothing Then
                    oCmd.Dispose()
                End If
            End Try
            Return ExecuteQuery
        End Function 'Parameter Query
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] Save DBNULL

    Of course, your column must be declared as nullable in the first place.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Save DBNULL

    I didn't say you COULDN'T save a NULL value.... in every database I've ever stuffed a NULL value into a DateTime field ends up being 12/31/1899 0:00:00.000

    it's led to some problems during data extractions.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [2005] Save DBNULL

    I don't seem to get that back. When I post a null to the database the field is null. On return I test for NULL and process accordingly.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width