rbend
asked on
SQL making blank date fields 1/1/1900 ...need to change to 'blank' ??
I am binding data to a grid then updating data back to SQL with a button.
The update code is:
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
For xx = 0 To UltraWebGrid1.Rows.Count - 1
var0 = UltraWebGrid1.Rows(xx).Cel ls(0).Text
var1 = UltraWebGrid1.Rows(xx).Cel ls(1).Text
var2 = UltraWebGrid1.Rows(xx).Cel ls(2).Text
var3 = UltraWebGrid1.Rows(xx).Cel ls(3).Text
var4 = UltraWebGrid1.Rows(xx).Cel ls(4).Text
Replace(var4, "'", "''")
var5 = UltraWebGrid1.Rows(xx).Cel ls(5).Text
var6 = UltraWebGrid1.Rows(xx).Cel ls(6).Text
var7 = UltraWebGrid1.Rows(xx).Cel ls(7).Text
var8 = UltraWebGrid1.Rows(xx).Cel ls(8).Text
var9 = UltraWebGrid1.Rows(xx).Cel ls(9).Text
var10 = UltraWebGrid1.Rows(xx).Cel ls(10).Tex t
var11 = UltraWebGrid1.Rows(xx).Cel ls(11).Tex t
var12 = UltraWebGrid1.Rows(xx).Cel ls(12).Tex t
var13 = UltraWebGrid1.Rows(xx).Cel ls(13).Tex t
var14 = UltraWebGrid1.Rows(xx).Cel ls(14).Tex t
var15 = UltraWebGrid1.Rows(xx).Cel ls(15).Tex t
var16 = UltraWebGrid1.Rows(xx).Cel ls(16).Tex t
sqlstring = "UPDATE EIS.dbo.tcdata set InvoiceNbr = '" & var1 & "', MstrBuildNr= '" & var2 & "', so_ord_date= '" & var8 & "', so_req_date= '" & var9 & "', so_due_date= '" & var10 & "', so_pricing_dt= '" & var11 & "', ActCarrier= '" & var13 & "', ActShipDate= '" & var14 & "', CarrInvcd= '" & var15 & "', Estimate= '" & var16 & "' where so_nbr = '" & var0 & "'"
sqlcmd.CommandText = sqlstring
sqlcmd.ExecuteNonQuery()
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- -----
var14 is quite often blank or "", but when I write the "" or blank to SQL its defaulting to 1/1/1900 and putting that in the field. This is not desireable. How can I tell it to write a "null" if it is "" or blank.?
The update code is:
--------------------------
For xx = 0 To UltraWebGrid1.Rows.Count - 1
var0 = UltraWebGrid1.Rows(xx).Cel
var1 = UltraWebGrid1.Rows(xx).Cel
var2 = UltraWebGrid1.Rows(xx).Cel
var3 = UltraWebGrid1.Rows(xx).Cel
var4 = UltraWebGrid1.Rows(xx).Cel
Replace(var4, "'", "''")
var5 = UltraWebGrid1.Rows(xx).Cel
var6 = UltraWebGrid1.Rows(xx).Cel
var7 = UltraWebGrid1.Rows(xx).Cel
var8 = UltraWebGrid1.Rows(xx).Cel
var9 = UltraWebGrid1.Rows(xx).Cel
var10 = UltraWebGrid1.Rows(xx).Cel
var11 = UltraWebGrid1.Rows(xx).Cel
var12 = UltraWebGrid1.Rows(xx).Cel
var13 = UltraWebGrid1.Rows(xx).Cel
var14 = UltraWebGrid1.Rows(xx).Cel
var15 = UltraWebGrid1.Rows(xx).Cel
var16 = UltraWebGrid1.Rows(xx).Cel
sqlstring = "UPDATE EIS.dbo.tcdata set InvoiceNbr = '" & var1 & "', MstrBuildNr= '" & var2 & "', so_ord_date= '" & var8 & "', so_req_date= '" & var9 & "', so_due_date= '" & var10 & "', so_pricing_dt= '" & var11 & "', ActCarrier= '" & var13 & "', ActShipDate= '" & var14 & "', CarrInvcd= '" & var15 & "', Estimate= '" & var16 & "' where so_nbr = '" & var0 & "'"
sqlcmd.CommandText = sqlstring
sqlcmd.ExecuteNonQuery()
--------------------------
var14 is quite often blank or "", but when I write the "" or blank to SQL its defaulting to 1/1/1900 and putting that in the field. This is not desireable. How can I tell it to write a "null" if it is "" or blank.?
If it is C# you can set the value to DBNull.Value that will insert a null value in the column if the column is null allowed in the database.
Wim
Wim
ASKER
No VB.
Please show example using my code.
Please show example using my code.
rbend, does your datetime column allow nulls? What is specified as the default? If it is 1/1/1990, you could change the default to NULL if you really want to allow null dates in that column. If you change the column constraint to allow nulls, and remove any defaults that may exist defaulting to 1/1/1990, then the NULL values should, and the empty string should just not update anything.
ASKER
yes...the column allows and initially defaults to nulls.
I believe that could be ok.
Where do I disalble the invokation of 1/1/1900 as the default?
I believe that could be ok.
Where do I disalble the invokation of 1/1/1900 as the default?
rbend,
I'm a C# programmer but allright here it is:
if (UltraWebGrid1.Rows(xx).Ce lls(14).Te xt = "") then
var14 = DBNull.Value
else
var14 = UltraWebGrid1.Rows(xx).Cel ls(14).Tex t
end if
I'm a C# programmer but allright here it is:
if (UltraWebGrid1.Rows(xx).Ce
var14 = DBNull.Value
else
var14 = UltraWebGrid1.Rows(xx).Cel
end if
rbend,
you can not disable the default, if you did not defined that in the first place.
you can not disable the default, if you did not defined that in the first place.
You can't disable that, that was my bad.. My solution would take care of nulls not empty strings, I would take care of it in VB with an ISdate function.. Is it ever possible that someone may put nondate characters or spaces in that column (var14)? Is var14 defined as a Date value in excel?
ASKER
Not Excel....A ASP page with Ultrawebgrid.
Taking the value from a grid column.
Not error checking the date value yet.
Could be prone to erroneous entry.
wimthepimscake:
Got error (squiggly blue underscore) on 'var14 = DBNull.Value'
Didn't like that.
ASP.NET is my platform.
Taking the value from a grid column.
Not error checking the date value yet.
Could be prone to erroneous entry.
wimthepimscake:
Got error (squiggly blue underscore) on 'var14 = DBNull.Value'
Didn't like that.
ASP.NET is my platform.
that may be because your data type for that variable (var14) is defined as datetime.. Try and define it as a string.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
right...that's precisely my issue Mike.
I don't know the syntax.
:-)
I don't know the syntax.
:-)
ASKER
I found this nugget which answers my question quite well.
http://www.lazydba.com/sql/1__9303.html
thanks
http://www.lazydba.com/sql/1__9303.html
thanks
what code is it? C# ?
Wimthepimscake