CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: NULL Value

  1. #1
    Join Date
    Feb 2009
    Posts
    192

    NULL Value

    Hi,

    I have the follwoing code;

    Code:
    query = "INSERT INTO dbo.TblPracExclude (prac_no, Goldpracid) VALUES (" & _
                                                          chk_prac_no & ", '" & _
                                                          DgvPracExcl.Rows(RowID).Cells(1).Value  & "')"
    
                                    cmd = New SqlCommand(query, conn1)
                                    cmd.ExecuteNonQuery()
    I am trying to add these values to a table in SQL database.

    The problem am facing is that DgvPracExcl.Rows(RowID).Cells(1).Value is empty so this inserts an empty string to the database.
    Instead I want it to insert a NULL value.

    Is this possisble?

    Thanks
    Last edited by dr223; August 10th, 2012 at 09:46 AM.

  2. #2
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: NULL Value

    Why put ' ' around it then?

  3. #3
    Join Date
    Feb 2009
    Posts
    192

    Re: NULL Value

    If I exclude the '' I receive the error message - Incorrect syntax near ','.

    Thanks

  4. #4
    Join Date
    Aug 2009
    Location
    NW USA
    Posts
    173

    Re: NULL Value

    maybe test to see if the value is Null. If it is not (has a value) then include the ' '. If it is Null don't use them.

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: NULL Value

    while i prefer not to use NULLS in a db as it raises problems later when reading the data, the answer here is to not include it in the query if you want it to be null...

    IE:
    Code:
    Tmpstring = DgvPracExcl.Rows(RowID).Cells(1).Value
    query = "INSERT INTO dbo.TblPracExclude (prac_no " & _
            IIF(Tmpstring="" ,")" ,",Goldpracid)").tostring & " VALUES (" & chk_prac_no & _
            IIF(Tmpstring="" ,")" ,",'" & Tmpstring & "')").tostring 
    cmd = New SqlCommand(query, conn1)
    cmd.ExecuteNonQuery()
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Feb 2009
    Posts
    192

    Re: NULL Value

    Ok - Changed it to
    Code:
         Dim conn As SqlConnection = GetDbConnection()
    
         Dim sql As String = "INSERT INTO dbo.TblPracExclude (prac_no, Goldpracid, prac_name, prac_status, prac_enabled, Datetime) " & _
                                                        "VALUES (@prac_no, @Goldpracid, @prac_name, @prac_status, @prac_enabled, @Datetime)"
                                    Dim myCommand As New SqlCommand(sql, conn)
    
                                    With myCommand.Parameters
                                        .AddWithValue("@prac_no", DgvPracExcl.Rows(RowID).Cells(0).Value)
                                        .AddWithValue("@Goldpracid", DgvPracExcl.Rows(RowID).Cells(1).Value)
                                        .AddWithValue("@prac_status", Me.DgvPracExcl.Rows(RowID).Cells(2).Value)
                                        .AddWithValue("@prac_enabled", Me.DgvPracExcl.Rows(RowID).Cells(3).Value)
                                        .AddWithValue("@Datetime", DateTime.Now)
                                    End With
    Works, but doesnt update the table why?

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: NULL Value

    Don't see your .Execute() statement in there (for myCommand)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured