Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have one text box with name
txtsr
and my client will enter there item code or item name
and i want to perform search according to there choice.
VB
Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngo.Click
        Try
            With fg
                Dim str(1) As String
                ssql = Nothing
                If fg.Col = 0 Then
                    ssql = Nothing

                    ssql = "select iname,itmcode,ivalue,balqty  from vewhelp where itmcode like '%" & Trim(Me.txtsr.Text) & "%'  and site='" & loginsite & "' "
                    'ssql = "select iname,itmcode,ivalue,balqty  from vewhelp where iname like '%" & Trim(Me.txtsr.Text) & "%'  and site='" & loginsite & "' and left(itmcode,2)='" & storeID & "'"
                End If
                fn.readqry(ssql)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                fg.Rows.Count = 1
                fg.Rows.Count = 2
                While dr.Read()
                    .SetData(.Rows.Count - 1, "iname", dr.Item("iname"))
                    .SetData(.Rows.Count - 1, "rate", dr.Item("ivalue"))
                    .SetData(.Rows.Count - 1, "balqty", dr.Item("balqty"))
                    .SetData(.Rows.Count - 1, "icode", dr.Item("itmcode").ToString())
                    .Rows.Count += 1
                End While
                dr.Close()

                fg.Focus()

            End With

            With fg
                Dim str(1) As String
                ssql = Nothing
                If fg.Col = 1 Then
                    ssql = Nothing

                    ssql = "select iname,itmcode,ivalue,balqty  from vewhelp where iname like '%" & Trim(Me.txtsr.Text) & "%'  and site='" & loginsite & "' "
                End If
                fn.readqry(ssql)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                fg.Rows.Count = 1
                fg.Rows.Count = 2
                While dr.Read()
                    .SetData(.Rows.Count - 1, "iname", dr.Item("iname"))
                    .SetData(.Rows.Count - 1, "rate", dr.Item("ivalue"))
                    .SetData(.Rows.Count - 1, "balqty", dr.Item("balqty"))
                    .SetData(.Rows.Count - 1, "icode", dr.Item("itmcode").ToString())
                    .Rows.Count += 1
                End While
                dr.Close()

                fg.Focus()

            End With


        Catch Excep As Exception
            MessageBox.Show(Excep.Message, "Error,Contact OR Send ()", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
Posted
Updated 27-Oct-11 23:25pm
v2
Comments
prince_rumeel 28-Oct-11 5:29am    
fg is the name of my flex grid.
André Kraak 28-Oct-11 5:30am    
So what does not work in the code you posted?
prince_rumeel 28-Oct-11 5:37am    
i am using a flex grid which has 4 column 1st is item code.2nd is item name

i need when a user click 1st col at that time my search button show itemcode wise search.
and when he click on 2nd col and write item name in textbox then it show the item name wise result

Combine the SQL stagtement with an OR operator

Private Sub btngo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btngo.Click
        Try
            
            With fg
                Dim str(1) As String
                ssql = Nothing
                If fg.Col = 1 Then
                    ssql = Nothing
 
                    ssql = "select iname,itmcode,ivalue,balqty  from vewhelp where (iname like '%" & Trim(Me.txtsr.Text) & "%' or itmcode like '%" & Trim(Me.txtsr.Text) & "%') and site='" & loginsite & "' "
                End If
                fn.readqry(ssql)
                Dim dr As SqlDataReader = cmd.ExecuteReader()
                fg.Rows.Count = 1
                fg.Rows.Count = 2
                While dr.Read()
                    .SetData(.Rows.Count - 1, "iname", dr.Item("iname"))
                    .SetData(.Rows.Count - 1, "rate", dr.Item("ivalue"))
                    .SetData(.Rows.Count - 1, "balqty", dr.Item("balqty"))
                    .SetData(.Rows.Count - 1, "icode", dr.Item("itmcode").ToString())
                    .Rows.Count += 1
                End While
                dr.Close()
 
                fg.Focus()
 
            End With
 

        Catch Excep As Exception
            MessageBox.Show(Excep.Message, "Error,Contact OR Send ()", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub


try to repalce paramererised queries to avoid SQL injection
 
Share this answer
 
v2
Comments
prince_rumeel 28-Oct-11 5:35am    
execute reader:command text property has not been initilized

this is the error
prince_rumeel 28-Oct-11 5:44am    
thx alot broooooooooo
Bala Selvanayagam 28-Oct-11 5:56am    
My pleasure
prince_rumeel 28-Oct-11 7:09am    
you are realy a good programmer.you reply me when ever i post here
You can combine the search as follows:

SQL
ssql = "select iname,itmcode,ivalue,balqty from vewhelp where (itmcode like '%" & Trim(Me.txtsr.Text) & "%' OR iname like '%" & Trim(Me.txtsr.Text) & "%')  and site='" & loginsite & "' and left(itmcode,2)='" & storeID & "'"


If you pass the data directly without validation, you are prone to SQL injection. Use parametrized queries. Example:

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/6bdf8b71-1cf1-41c0-848c-4fca2c9e1ea2/[^]
 
Share this answer
 
hi ,
example:

SELECT * FROM TABLE WHERE COLUMN1 LIKE 'TEXTBOX1.TEXT%'
 
Share this answer
 

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