473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Insert date 1900-01-01

Hey,
I am inserting values in the table:

Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)
sqlcomm1.ExecuteNonQuery()

Query is working fine if there is value in the txtOutDate, but if user
leaves the field txtOutdate empty, system insert date 1900-01-01. I want
system to insert null.

How can I achieve that?

Regards
Arek


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #1
7 2415
in c#
txtOutdate.Text == string.empty ? null : txtOutdate.Text

i'm sorry i am not expert in visualbasic.

but i suppose you can make an if
if txtOutdate.Text == string.empty then
str = null
else
str = txtOutdate.Text

I think you should also transform your string in datetime format recognized
by sql.
"Arek" wrote:
Hey,
I am inserting values in the table:

Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)
sqlcomm1.ExecuteNonQuery()

Query is working fine if there is value in the txtOutDate, but if user
leaves the field txtOutdate empty, system insert date 1900-01-01. I want
system to insert null.

How can I achieve that?

Regards
Arek


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #2
Hi Arek,

It isn't clear whether your data is supposed to be a datetime or a quoted
string. If it is a string and you want to insert a Null if the text is empty,
you can try testing with the function code below. For a datetime, you'll need
a different value. You can try hardcoding the values at first until you see
what type is acceptable.

Let us know how you make out?

Ken

<%@ Page Language="VB" %>
<script runat="server">

Sub Button1_Click(sender As Object, e As EventArgs)
label1.text="INSERT INTO tblTasks (idTask, outdate) VALUES ('" &
IDTask.text & "'," & CheckNull(txtOutdate.Text) & ")"
End Sub

Function CheckNull(strInVal as object) as string
if isNothing(strInVal) then
return "Null"
end if

if strInval="" then
return "Null"
end if

' Everything is fine
return " '" & strInval & "' "
end function

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
</p>
<p>
<asp:TextBox id="IDTask" runat="server">MyValue</asp:TextBox>
</p>
<p>
<asp:TextBox id="txtOutdate" runat="server">The value goes
here</asp:TextBox>
</p>
<p>
<asp:Button id="Button1" onclick="Button1_Click" runat="server"
Text="Button"></asp:Button>
</p>
<p>
<asp:Label id="Label1" runat="server">Label</asp:Label>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
"Arek" wrote:
Hey,
I am inserting values in the table:

Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)
sqlcomm1.ExecuteNonQuery()

Query is working fine if there is value in the txtOutDate, but if user
leaves the field txtOutdate empty, system insert date 1900-01-01. I want
system to insert null.

How can I achieve that?

Regards
Arek


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #3
I am not a not an expert in C#, but shouldn't that be DBNull.Value?

Regardless, it is a lot cleaner using parameters...

Dim sqlcomm1 As New SqlCommand("INSERT INTO tblTasks (idTask, outdate)
VALUES (@ID, @OutDate)", conn)

sqlcomm1.Parameters.Add("@ID", SqlDbType.Int).Value = CInt(IDTask.text)

If Not txtOutdate.Text = String.Empty Then
sqlcomm1.Parameters.Add("@OutDate", SqlDbType.DateTime).Value =
CDate(txtOutdate)
Else
sqlcomm1.Parameters.Add("@OutDate", SqlDbType.DateTime).Value =
DBNull.Value
End If

sqlcomm1.ExecuteNonQuery()

HTH,
Greg

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...
in c#
txtOutdate.Text == string.empty ? null : txtOutdate.Text

i'm sorry i am not expert in visualbasic.

but i suppose you can make an if
if txtOutdate.Text == string.empty then
str = null
else
str = txtOutdate.Text

I think you should also transform your string in datetime format
recognized
by sql.
"Arek" wrote:
Hey,
I am inserting values in the table:

Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)
sqlcomm1.ExecuteNonQuery()

Query is working fine if there is value in the txtOutDate, but if user
leaves the field txtOutdate empty, system insert date 1900-01-01. I want
system to insert null.

How can I achieve that?

Regards
Arek


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #4
should have been...
sqlcomm1.Parameters.Add("@OutDate", SqlDbType.DateTime).Value =
CDate(txtOutdate.Text)

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP10.phx.gbl...I am not a not an expert in C#, but shouldn't that be DBNull.Value?

Regardless, it is a lot cleaner using parameters...

Dim sqlcomm1 As New SqlCommand("INSERT INTO tblTasks (idTask, outdate)
VALUES (@ID, @OutDate)", conn)

sqlcomm1.Parameters.Add("@ID", SqlDbType.Int).Value = CInt(IDTask.text)

If Not txtOutdate.Text = String.Empty Then
sqlcomm1.Parameters.Add("@OutDate", SqlDbType.DateTime).Value =
CDate(txtOutdate)
Else
sqlcomm1.Parameters.Add("@OutDate", SqlDbType.DateTime).Value =
DBNull.Value
End If

sqlcomm1.ExecuteNonQuery()

HTH,
Greg

"Psycho" <Ps****@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...
in c#
txtOutdate.Text == string.empty ? null : txtOutdate.Text

i'm sorry i am not expert in visualbasic.

but i suppose you can make an if
if txtOutdate.Text == string.empty then
str = null
else
str = txtOutdate.Text

I think you should also transform your string in datetime format
recognized
by sql.
"Arek" wrote:
Hey,
I am inserting values in the table:

Dim sqlcomm1 As SqlCommand = New SqlCommand("INSERT INTO tblTasks
(idTask, outdate) VALUES ('" & IDTask.text & "','" & txtOutdate.Text &
"')", conn)
sqlcomm1.ExecuteNonQuery()

Query is working fine if there is value in the txtOutDate, but if user
leaves the field txtOutdate empty, system insert date 1900-01-01. I want
system to insert null.

How can I achieve that?

Regards
Arek


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 18 '05 #5
I was thinking about using parameters and converting my query to stored
procedure. The thing is that this is just part of the query and query
itself is big. So I was trying to avoid this.
Anyway, my outdate is in datetime format.
I will try to use proposed here solutions and I will post whatever
worked for me...:)

Regards
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #6
Ok,

I finally use query with parameters and then check
If Not TxtOutDate.Text = "" Then
sqlcomm1.Parameters.Add("@TxtOutDate",
SqlDbType.SmallDateTime).Value = TxtOutDate.Text
Else
sqlcomm1.Parameters.Add("@TxtOutDate",
SqlDbType.SmallDateTime).Value = DBNull.Value
End If

This is working for me.

When I tried to use CDate or Cint, or if not txtOutdate.text =
String.Empty, system was giving me errors.

Thank you for your help
Arek

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #7
Ok,

I finally use query with parameters and then check
If Not TxtOutDate.Text = "" Then
sqlcomm1.Parameters.Add("@TxtOutDate",
SqlDbType.SmallDateTime).Value = TxtOutDate.Text
Else
sqlcomm1.Parameters.Add("@TxtOutDate",
SqlDbType.SmallDateTime).Value = DBNull.Value
End If

This is working for me.

When I tried to use CDate or Cint, or if not txtOutdate.text =
String.Empty, system was giving me errors.

Thank you for your help
Arek

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Cristian Martinello | last post by:
There's a way to check if a string is a valid date ? Thanks helpers. -- Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
4
by: Treetop | last post by:
I have a script for my church that we use for the weekly events. I currently have it as week of Feb 1, 2003 at the top, then list Sun - Sat below the date. I have been asked to put the date next...
4
by: Dave | last post by:
Hi, Can anyone tell me a simple way of inserting some sort of code into a Dreamweaver template (it goes at the bottom of the page in Copyright info) that will cause the current year to be...
16
by: KL | last post by:
I am working on a problem and desperately need help! I need to prompt a user for the numerical month of birth, day of birth and year of birth and store it in varialbes and the use the variables...
8
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month"...
6
by: Franz | last post by:
Hy all, I have a problem with a date in javascript on firefox Browser. I use this javascript method: var dataNow=new Date(); var dd=dataNow.getDate(); var mm=dataNow.getMonth()+1; var...
1
by: Lam | last post by:
hi Does anyone know how to insert Date to SQL server 2000? I used DataTime.Today.ToShortDateString() it shows up "1/1/1900" in the database how can I correct that? also, how can I pull out the...
4
by: sam | last post by:
Possible convert VB 6 to VB.Net 1.1 for date serial function? Example, Calendar_Date = DateSerial(1900+(Julian_Date/1000), 1,Julian_Date Mod 1000) ? Calendar_Date and Julian_Date are input...
3
by: Bon | last post by:
Dear all How can I convert a datetime in Short Date format to General Date format and then insert into SQL database? I get a date from a field using Request("in_date"), which is in Short...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.