Thursday, March 8, 2012

Convert string into sql date time

i have a sql statement that i created in code and it is sending a query to the database
when i dim the variable a datetime variable it says that it cant convert it
if i make the variable a varchar it works but it only returns one result when it should be returning about 10

here is the code


Public Function dbDGQSSearch(ByVal BatchID As String, ByVal CreatedBy As Integer, ByVal CreatedFor As Integer, ByVal DateCreatedMod As Integer, ByVal DateCreated As String, ByVal DateCompletedMod As Integer, ByVal DateCompleted As String, ByVal DateStartedMod As Integer, ByVal DateStarted As String, ByVal SearchType As Integer, ByVal Completed As Integer, ByVal PriorityMod As Integer, ByVal Priority As Integer, ByVal RemainingCallsMod As Integer, ByVal RemainingCalls As Integer, ByVal TotalCallsMod As Integer, ByVal TotalCalls As Integer, ByVal Bonus As Integer, ByVal Keyword1 As String, ByVal Keyword2 As String, ByVal Keyword3 As String, ByVal Keyword4 As String, ByVal Keyword5 As String)

Dim strQueSearch As String
strQueSearch = "SELECT tlkup_Rep.RepID, tlkup_Rep.PositionID, tlkup_Rep.RepFName, tlkup_Rep.RepLName, tlkup_Rep.RepPassword, tlkup_Rep.RepUserName, tlkup_Rep.RepFName + ' ' + tlkup_Rep.RepLName AS RepName, t_Que.QueID, t_Que.BatchID, t_Que.AdminID, t_Que.Manager, t_Que.BonusID, t_Que.QueCompleted, t_Que.QueDate, t_Que.QueNotes, t_Que.QuePriority, t_Que.QueQuantity, t_Que.QueStartDate, t_Que.Mail, t_Que.QueDateComplete, t_Que.QueTotal FROM t_Que INNER JOIN tlkup_Rep ON t_Que.Manager = tlkup_Rep.RepID AND t_Que.Manager = tlkup_Rep.RepID WHERE BatchID<>'' and BatchID<>'2' and BatchID<>'3' and BatchID<>'4' "

'Creates statement for selecting the add to batch data where the criteria appear
If BatchID <> "" Then

strQueSearch = strQueSearch + " and t_Que.BatchID= @.BatchID "
End If
If CreatedBy > 1 Then
strQueSearch = strQueSearch + " and t_Que.RepID =@.RepID "
End If
If CreatedFor > 1 Then
strQueSearch = strQueSearch + " and t_Que.Manager = @.Manager "
End If

If DateCreated <> "" Then
If DateCreatedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueDate >@.QueDate "
ElseIf DateCreatedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueDate <@.QueDate "
ElseIf DateCreatedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueDate =@.QueDate "
End If
End If

If DateCompleted <> "" Then
If DateCompletedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueDateComplete >@.QueDateComplete "
ElseIf DateCompletedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueDateComplete <@.QueDateComplete and t_Que.QueDateComplete >'1/1/1900' "
ElseIf DateCompletedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueDateComplete =@.QueDateComplete "
End If
End If

If DateStarted <> "" Then
If DateStartedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueStartDate >@.QueStartDate "
ElseIf DateStartedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueStartDate <@.QueStartDate "
ElseIf DateStartedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueStartDate =@.QueStartDate "
End If
End If

If SearchType = 0 Then
'Both
'strQueSearch = strQueSearch + " and t_Que.Mail=0 and t_Que.Mail=1 "
ElseIf SearchType = 1 Then
'Mail
strQueSearch = strQueSearch + " and t_Que.Mail=1 "
ElseIf SearchType = 2 Then
'Phone
strQueSearch = strQueSearch + " and t_Que.Mail=0 "
End If

If Completed = 0 Then
'Both
'strQueSearch = strQueSearch + " and t_Que.Mail=0 and t_Que.Mail=1 "
ElseIf Completed = 1 Then
'Yes
strQueSearch = strQueSearch + " and t_Que.QueCompleted=1 "
ElseIf Completed = 2 Then
'No
strQueSearch = strQueSearch + " and t_Que.QueCompleted=0 "
End If

If Priority > 0 Then
If PriorityMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QuePriority >@.QuePriority "
ElseIf PriorityMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QuePriority <@.QuePriority "
ElseIf PriorityMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QuePriority =@.QuePriority "
End If
End If
If RemainingCalls > 0 Then
If RemainingCallsMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueQuantity >@.QueQuantity "
ElseIf RemainingCallsMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueQuantity <@.QueQuantity "
ElseIf RemainingCallsMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueQuantity =@.QueQuantity "
End If
End If

If TotalCalls > 0 Then
If TotalCallsMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueTotal >@.QueTotal "
ElseIf TotalCallsMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueTotal <@.QueTotal "
ElseIf TotalCallsMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueTotal =@.QueTotal "
End If
End If

If Bonus > 1 Then
strQueSearch = strQueSearch + " and t_Que.BonusID =@.BonusID "
End If

If Keyword1 <> "" Then
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword1+'%' "
End If
If Keyword2 <> "" Then
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword2+'%' "
End If
If Keyword3 <> "" Then
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword3+'%' "
End If
If Keyword4 <> "" Then
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword4+'%' "
End If
If Keyword5 <> "" Then
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword5+'%' "
End If

'makes statement into sqlcommand
C.daQueSearch.SelectCommand.CommandText = strQueSearch

'var declaration
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.BatchID", SqlDbType.VarChar, 12))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Manager", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.RepID", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueDate", SqlDbType.VarChar, 20)) '<-- This is what,when i change to datetime, says it cant convert
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueStartDate", SqlDbType.VarChar, 20))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueDateComplete", SqlDbType.VarChar, 20))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QuePriority", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueQuantity", SqlDbType.BigInt))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueTotal", SqlDbType.BigInt))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.BonusID", SqlDbType.SmallInt))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword1", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword2", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword3", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword4", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword5", SqlDbType.VarChar, 50))

'data entry
C.daQueSearch.SelectCommand.Parameters("@.BatchID").Value = BatchID
C.daQueSearch.SelectCommand.Parameters("@.Manager").Value = CreatedBy
C.daQueSearch.SelectCommand.Parameters("@.RepID").Value = CreatedFor
C.daQueSearch.SelectCommand.Parameters("@.QueDate").Value = DateCreated
C.daQueSearch.SelectCommand.Parameters("@.QueStartDate").Value = DateStarted
C.daQueSearch.SelectCommand.Parameters("@.QueDateComplete").Value = DateCompleted
C.daQueSearch.SelectCommand.Parameters("@.QuePriority").Value = Priority
C.daQueSearch.SelectCommand.Parameters("@.QueQuantity").Value = RemainingCalls
C.daQueSearch.SelectCommand.Parameters("@.QueTotal").Value = TotalCalls
C.daQueSearch.SelectCommand.Parameters("@.BonusID").Value = Bonus
C.daQueSearch.SelectCommand.Parameters("@.Keyword1").Value = Keyword1
C.daQueSearch.SelectCommand.Parameters("@.Keyword2").Value = Keyword2
C.daQueSearch.SelectCommand.Parameters("@.Keyword3").Value = Keyword3
C.daQueSearch.SelectCommand.Parameters("@.Keyword4").Value = Keyword4
C.daQueSearch.SelectCommand.Parameters("@.Keyword5").Value = Keyword5

Try
C.ndConnection.Open()
C.daQueSearch.SelectCommand.ExecuteNonQuery()
Catch ex As Exception
lblMainError1.Text = err("dbDGQSSearch " + ex.Source, ex.Message, CurUsr)
lblMainError1.Visible = True
Finally
C.ndConnection.Close()
End Try

FillQSDG()' this fills the datagrid

End Function

does your above code work ? because you are building the search string conditionally but adding the parameters without checking the conditions...

for xample :


If BatchID <> "" Then
strQueSearch = strQueSearch + " and t_Que.BatchID= @.BatchID "
End If

you are appending to the sql stmt if batchid <> ""...but here


C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.BatchID", SqlDbType.VarChar, 12))

you are adding the parameter to the collection without any checks..

lets say the the user did not supply any id for the batchid...then the sql stmt wil not be appended with "and t_Que.BatchID= @.BatchID " part...but the parameter is still being added ...

do you get my point ?|||i get your point but it seems to work correctly
it is adding to the parameter collection but doesnt actually use it until it is in the statement.
it probably isnt proper but it does work|||just for kicks i changed it and it still did not work but the weird thing is it doesnt work even if there is no criteria entered.

what is weird is i used the cool little red dot program walkthrough thing and i stopped it right on the sql transaction and copied the command.text and pasted it into query analizer and it got the require results
But the data grid that it is outputting to only shows one record

This is the new code


Public Function dbDGQSSearch(ByVal BatchID As String, ByVal CreatedBy As Integer, ByVal CreatedFor As Integer, ByVal DateCreatedMod As Integer, ByVal DateCreated As String, ByVal DateCompletedMod As Integer, ByVal DateCompleted As String, ByVal DateStartedMod As Integer, ByVal DateStarted As String, ByVal SearchType As Integer, ByVal Completed As Integer, ByVal PriorityMod As Integer, ByVal Priority As Integer, ByVal RemainingCallsMod As Integer, ByVal RemainingCalls As Integer, ByVal TotalCallsMod As Integer, ByVal TotalCalls As Integer, ByVal Bonus As Integer, ByVal Keyword1 As String, ByVal Keyword2 As String, ByVal Keyword3 As String, ByVal Keyword4 As String, ByVal Keyword5 As String)
Dim strQueSearch As String
strQueSearch = "SELECT tlkup_Rep.RepID, tlkup_Rep.PositionID, tlkup_Rep.RepFName, tlkup_Rep.RepLName, tlkup_Rep.RepPassword, tlkup_Rep.RepUserName, tlkup_Rep.RepFName + ' ' + tlkup_Rep.RepLName AS RepName, t_Que.QueID, t_Que.BatchID, t_Que.AdminID, t_Que.Manager, t_Que.BonusID, t_Que.QueCompleted, t_Que.QueDate, t_Que.QueNotes, t_Que.QuePriority, t_Que.QueQuantity, t_Que.QueStartDate, t_Que.Mail, t_Que.QueDateComplete, t_Que.QueTotal FROM t_Que INNER JOIN tlkup_Rep ON t_Que.Manager = tlkup_Rep.RepID AND t_Que.Manager = tlkup_Rep.RepID WHERE BatchID<>'' and BatchID<>'2' and BatchID<>'3' and BatchID<>'4' "

'Creates statement for selecting the add to batch data where the criteria appear
If BatchID <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.BatchID", SqlDbType.VarChar, 12))
C.daQueSearch.SelectCommand.Parameters("@.BatchID").Value = BatchID
strQueSearch = strQueSearch + " and t_Que.BatchID= @.BatchID "
End If
If CreatedBy > 1 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.RepID", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters("@.RepID").Value = CreatedFor
strQueSearch = strQueSearch + " and t_Que.RepID =@.RepID "
End If
If CreatedFor > 1 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Manager", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters("@.Manager").Value = CreatedBy
strQueSearch = strQueSearch + " and t_Que.Manager = @.Manager "
End If

If DateCreated <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueDate", SqlDbType.VarChar, 20))
C.daQueSearch.SelectCommand.Parameters("@.QueDate").Value = DateCreated
If DateCreatedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueDate >@.QueDate "
ElseIf DateCreatedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueDate <@.QueDate "
ElseIf DateCreatedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueDate =@.QueDate "
End If
End If

If DateCompleted <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueDateComplete", SqlDbType.VarChar, 20))
C.daQueSearch.SelectCommand.Parameters("@.QueDateComplete").Value = DateCompleted
If DateCompletedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueDateComplete >@.QueDateComplete "
ElseIf DateCompletedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueDateComplete <@.QueDateComplete and t_Que.QueDateComplete >'1/1/1900' "
ElseIf DateCompletedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueDateComplete =@.QueDateComplete "
End If
End If

If DateStarted <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueStartDate", SqlDbType.VarChar, 20))
C.daQueSearch.SelectCommand.Parameters("@.QueStartDate").Value = DateStarted
If DateStartedMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueStartDate >@.QueStartDate "
ElseIf DateStartedMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueStartDate <@.QueStartDate "
ElseIf DateStartedMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueStartDate =@.QueStartDate "
End If
End If

If SearchType = 0 Then
'Both
'strQueSearch = strQueSearch + " and t_Que.Mail=0 and t_Que.Mail=1 "
ElseIf SearchType = 1 Then
'Mail
strQueSearch = strQueSearch + " and t_Que.Mail=1 "
ElseIf SearchType = 2 Then
'Phone
strQueSearch = strQueSearch + " and t_Que.Mail=0 "
End If

If Completed = 0 Then
'Both
'strQueSearch = strQueSearch + " and t_Que.Mail=0 and t_Que.Mail=1 "
ElseIf Completed = 1 Then
'Yes
strQueSearch = strQueSearch + " and t_Que.QueCompleted=1 "
ElseIf Completed = 2 Then
'No
strQueSearch = strQueSearch + " and t_Que.QueCompleted=0 "
End If

If Priority > 0 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QuePriority", SqlDbType.Int))
C.daQueSearch.SelectCommand.Parameters("@.QuePriority").Value = Priority
If PriorityMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QuePriority >@.QuePriority "
ElseIf PriorityMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QuePriority <@.QuePriority "
ElseIf PriorityMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QuePriority =@.QuePriority "
End If
End If

If RemainingCalls > 0 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueQuantity", SqlDbType.BigInt))
C.daQueSearch.SelectCommand.Parameters("@.QueQuantity").Value = RemainingCalls
If RemainingCallsMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueQuantity >@.QueQuantity "
ElseIf RemainingCallsMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueQuantity <@.QueQuantity "
ElseIf RemainingCallsMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueQuantity =@.QueQuantity "
End If
End If

If TotalCalls > 0 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.QueTotal", SqlDbType.BigInt))
C.daQueSearch.SelectCommand.Parameters("@.QueTotal").Value = TotalCalls
If TotalCallsMod = 0 Then
'>
strQueSearch = strQueSearch + " and t_Que.QueTotal >@.QueTotal "
ElseIf TotalCallsMod = 1 Then
'<
strQueSearch = strQueSearch + " and t_Que.QueTotal <@.QueTotal "
ElseIf TotalCallsMod = 2 Then
'=
strQueSearch = strQueSearch + " and t_Que.QueTotal =@.QueTotal "
End If
End If

If Bonus > 1 Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.BonusID", SqlDbType.SmallInt))
C.daQueSearch.SelectCommand.Parameters("@.BonusID").Value = Bonus
strQueSearch = strQueSearch + " and t_Que.BonusID =@.BonusID "
End If

If Keyword1 <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword1", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters("@.Keyword1").Value = Keyword1
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword1+'%' "
End If
If Keyword2 <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword2", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters("@.Keyword2").Value = Keyword2
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword2+'%' "
End If
If Keyword3 <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword3", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters("@.Keyword3").Value = Keyword3
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword3+'%' "
End If
If Keyword4 <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword4", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters("@.Keyword4").Value = Keyword4
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword4+'%' "
End If
If Keyword5 <> "" Then
C.daQueSearch.SelectCommand.Parameters.Add(New SqlParameter("@.Keyword5", SqlDbType.VarChar, 50))
C.daQueSearch.SelectCommand.Parameters("@.Keyword5").Value = Keyword5
strQueSearch = strQueSearch + " and t_Que.QueNotes like '%'+@.Keyword5+'%' "
End If

'makes statement into sqlcommand
C.daQueSearch.SelectCommand.CommandText = strQueSearch

Try
C.ndConnection.Open()
C.daQueSearch.SelectCommand.ExecuteNonQuery()
Catch ex As Exception
lblMainError1.Text = err("dbDGQSSearch " + ex.Source, ex.Message, CurUsr)
lblMainError1.Visible = True
Finally
C.ndConnection.Close()
End Try

FillQSDG()

|||you are declaring it as a function...whats the return type? what are you returning ?

Public Function dbDGQSSearch(ByVal BatchID As String, ByVal CreatedBy As Integer, ByVal CreatedFor As Integer, ByVal DateCreatedMod As Integer, ByVal DateCreated As String, ByVal DateCompletedMod As Integer, ByVal DateCompleted As String, ByVal DateStartedMod As Integer, ByVal DateStarted As String, ByVal SearchType As Integer, ByVal Completed As Integer, ByVal PriorityMod As Integer, ByVal Priority As Integer, ByVal RemainingCallsMod As Integer, ByVal RemainingCalls As Integer, ByVal TotalCallsMod As Integer, ByVal TotalCalls As Integer, ByVal Bonus As Integer, ByVal Keyword1 As String, ByVal Keyword2 As String, ByVal Keyword3 As String, ByVal Keyword4 As String, ByVal Keyword5 As String)...?

hth|||what is the question?
if it is because it is not returning anything, that is fine, that isnt what is wrong, it doesnt need to return anything.
if it is because it is excruciatingly long i know
but this doesnt help me a whole lot|||i asked those q's because i didnt understand what you are trying to do in the function...you have a select statement but you say executenonquery... which does not return any records..if you need the resultset you need to say execteReader

hth|||i changed it to executeREADER and it is still only returning one result to the datagrid.
and, if as i said before, i take the statement that it is going to the query and put it into the query analizer it will return the correct results|||sorry
being un observant can be very frustrating, i was filling the dataset with the wrong data adapter

No comments:

Post a Comment