Solved

pass datetime

Posted on 2014-03-10
4
257 Views
Last Modified: 2014-03-15
I have a sub below. Date2 is not required. It can be blank sometimes because the data comes from a textbox. I can't just pass "" to this sub and store procedure. What should I do?


ALTER procedure [dbo].[InsertAppointments_tmp]

@ID      int,
@date1      smalldatetime,
@date2  smalldatetime,
@time1      varchar(12),
@time2  varchar(12)


as
insert into mytable(
ID,
date1,
date2,
time1,
time2)
values
(
@ID,
@date1,
@date2,
@time1,
@time2)                   






Public Sub load_appointment(ByVal id As Integer, _
                                    ByVal date1 As DateTime, ByVal date2 As DateTime, _
                                    ByVal date1_time As String, ByVal date2_time As String)
        Dim result As Integer = 0
        Using con As New SqlConnection(WebConfigurationManager.ConnectionStrings("mycon")
            Using sqlcmnd As New SqlCommand("mysp", con)
                sqlcmnd.CommandType = CommandType.StoredProcedure

                sqlcmnd.Parameters.AddWithValue("@id", id)
                sqlcmnd.Parameters.AddWithValue("@date1", date1)
                sqlcmnd.Parameters.AddWithValue("@date2", date2)
                sqlcmnd.Parameters.AddWithValue("@time1", date1_time)
                sqlcmnd.Parameters.AddWithValue("@time2", date2_time)

                con.Open()
                result = sqlcmnd.ExecuteNonQuery
                con.Close()
            End Using
        End Using
    End Sub
0
Comment
Question by:VBdotnet2005
  • 2
4 Comments
 
LVL 6

Expert Comment

by:c1nmo
ID: 39918438
Try DBNull.Value
0
 

Author Comment

by:VBdotnet2005
ID: 39918445
where should I put it?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39918464
or make it datetime nullable
0
 
LVL 6

Accepted Solution

by:
c1nmo earned 500 total points
ID: 39918738
Replace---------------------

sqlcmnd.Parameters.AddWithValue("@date2", date2)

With----------------------------

if date2 = "" then

sqlcmnd.Parameters.AddWithValue("@date2", DBNull.Value )

else

sqlcmnd.Parameters.AddWithValue("@date2", date2)

end if
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now