Solved

pass datetime

Posted on 2014-03-10
4
256 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
Comment Utility
Try DBNull.Value
0
 

Author Comment

by:VBdotnet2005
Comment Utility
where should I put it?
0
 
LVL 22

Expert Comment

by:p_davis
Comment Utility
or make it datetime nullable
0
 
LVL 6

Accepted Solution

by:
c1nmo earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

12 Experts available now in Live!

Get 1:1 Help Now