|
-
August 10th, 2012, 09:43 AM
#1
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.
-
August 10th, 2012, 10:28 AM
#2
Re: NULL Value
Why put ' ' around it then?
-
August 10th, 2012, 10:34 AM
#3
Re: NULL Value
If I exclude the '' I receive the error message - Incorrect syntax near ','.
Thanks
-
August 10th, 2012, 10:40 AM
#4
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.
-
August 13th, 2012, 01:37 AM
#5
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.
-
August 13th, 2012, 06:49 AM
#6
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?
-
August 13th, 2012, 11:29 AM
#7
Re: NULL Value
Don't see your .Execute() statement in there (for myCommand)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|