Click here to Skip to main content
16,004,574 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello fellow developer. Got a few tweakings I like to ask. So im using auto complete extender for my project, but getting an error based on selected criteria (basically I want to 'Where')
So here is the background of my project:
I have a dropdown list where the text is string and the value is integer
now on my auto complete extender I want 2 where
here is my trial code
<WebMethod()> _
   Public Function Discipline() As String()

        Dim sqlConn As New SqlConnection(_connString)
        Dim count As Integer = 10
        Dim prefixText As String = Nothing
        Dim DisciplineID As Integer = Nothing
        Dim sql As String = "Select Distinct CourseName, CourseCode from Course Where DisciplineID=@DisciplineID And ([CourseCode] like @prefixText or [CourseName] like @prefixText)"
        Dim da As New SqlDataAdapter(sql, sqlConn)
        da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = "%" + prefixText + "%"
        da.SelectCommand.Parameters.Add("@DisciplineID", SqlDbType.VarChar, 50).Value = DisciplineID.ToString
        Dim dt As New DataTable()

        da.Fill(dt)

        Dim items As String() = New String(dt.Rows.Count - 1) {}
        Dim i As Integer = 0
        For Each dr As DataRow In dt.Rows
            items.SetValue(dr("CourseCode").ToString() & " | " & dr("CourseName").ToString(), i)

            i += 1
        Next
        Return items

    End Function

Using break points I see that my error is on this on
Dim prefixText As String = Nothing
Dim DisciplineID As Integer = Nothing

Cause it says on the parameters nothing.

My question is can I use two where cause on an autocomplete extender where the primary clause will be based on dropdownlist.selectedvalue??

Thanks and more power.
Posted

1 solution

Hi,

You have may have to create a function to return reading of your data row (dr) an empty string in case your data row (dr) contents null.

Try to change your code for loop as sample below:
C#
For Each dr As DataRow In dt.Rows
   Items.SetValue(EmptyIfNull(dr("CourseCode")) & " | " & EmptyIfNull(dr("CourseName")), i)
   i += 1
Next


and here is the function:

C#
Public Shared Function EmptyIfNull(ByVal p As Object) As String
       Dim retValue As String = String.Empty
       Try
           If Not (p.Equals(DBNull.Value)) Then
               'retValue = DirectCast(p, String)
               retValue = CType(p, String)
           End If
       Catch ex As Exception
           retValue = String.Empty
       End Try
       Return retValue
End Function



You may also use such query as example: ( This is the recomended solution...)

C#
Select Distinct case when CourseName = null  then '' else CourseName end, 
 case when CourseCode = null then '' else CourseCode end from Course 
Where DisciplineID=@DisciplineID And ([CourseCode] like @prefixText or [CourseName] like @prefixText)


Hope this could help...

Regards,
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900