Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

pass datetime

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
VBdotnet2005
Asked:
VBdotnet2005
  • 2
1 Solution
 
c1nmoCommented:
Try DBNull.Value
0
 
VBdotnet2005Author Commented:
where should I put it?
0
 
p_davisCommented:
or make it datetime nullable
0
 
c1nmoCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now