Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pass datetime

Posted on 2014-03-10
4
Medium Priority
?
266 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

661 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