Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
I have 1 table name finalsort which has columns lotno,name,type,size etc.
 
i want to search data by name and display it in gridview based on name.
 
can anyone tell me how to do it?
 
i dont know the code.
 
will be obliged if anyone can help
thanks in advance.
 
sudeshna
Posted 8-Jul-13 0:03am
Comments
jaideepsinh at 8-Jul-13 5:55am
   
Paste you code or something what ever you done that's way we can help you easily and fast.
Maciej Los at 8-Jul-13 6:18am
   
What have you done so far?
Where are you stuck?
sudeshna from bangkok at 8-Jul-13 23:13pm
   
this is my code for search.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
'Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As New DataSet
Dim dt As DataTable
 

'Try
 
cmd.Connection = cn
cn.Open()
 
Dim da As New SqlDataAdapter("select * from finalsort", cn)
dt = New DataTable
 
ds = New DataSet
da.Fill(ds, "finalsort")
 
For i As Integer = 0 To ds.Tables("finalsort").Rows.Count - 1
If ComboBox1.SelectedItem = ds.Tables("finalsort").Rows(i).Item("name").ToString() Then
 
FinalsortDataGridView.DataSource = ds.Tables(0)
 
End If
 
Next
 
cn.Close()
End Sub
sudeshna from bangkok at 8-Jul-13 23:14pm
   
i have only 1 table in which all data are there. now i want on combo box selection of name, all data related to that particular name will display in datagrid view.
 
can any one help me plz.
 
i will be obliged.
thanks in advance
sudeshna from bangkok at 8-Jul-13 23:58pm
   
I have posted my code,.
 
can anyone help me plz
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Suppose you have button by clicking on it data retrieved from database and fill in gridview then write folowing code in button click event.
 
I am using sqlserver database
 
dim str as String="provide name for search"
Dim cn As New SqlConnection("<your connection="" string="">")
       Dim cmd As New SqlCommand("Select * from finalsort where name='" + str + "'", cn)
       cn.Open()
 
       Dim da As New SqlDataAdapter(cmd)
       Dim ds As New DataSet
       da.Fill(ds)
DataGridView1.DataSource=ds.Tables(0)
DataGridView1.Refresh() 'If required
</your>
  Permalink  
Comments
sudeshna from bangkok at 8-Jul-13 6:46am
   
what name will i put for search in
 
dim str as String="provide name for search" // in double quotes
bhuvamehul at 8-Jul-13 8:15am
   
what ever you want. Based on which name you want to retrieve data from database.
sudeshna from bangkok at 8-Jul-13 22:54pm
   
no, i have 1 combo box and on selection from the drop down list, the result will display in gridview.
sudeshna from bangkok at 8-Jul-13 23:15pm
   
the code is nt working. and i want data to be searched and retrieved based on combo box and display in gridview. so need a loop to run also.
bhuvamehul at 9-Jul-13 0:55am
   
So,just change in first line like this:
 
dim str as String=ComboBox1.SelectedText
sudeshna from bangkok at 9-Jul-13 1:18am
   
nothing is displaying.
 
Dim str As String = ComboBox1.SelectedText
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim cmd As New SqlCommand("Select * from finalsort where name='" + str + "'", cn)
cn.Open()
 
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
'Dim tb1 As New DataTable
' tb1 = New DataTable
da.Fill(ds)
FinalsortDataGridView.DataSource = ds.Tables(0)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
        'Dim da As New SqlDataAdapter
        Dim cmd As New SqlCommand
        Dim ds As New DataSet
       
 

        'Try

        cmd.Connection = cn
        cn.Open()
 
        Dim da As New SqlDataAdapter("select * from finalsort where name='" & ComboBox1.SelectedItem & "'", cn)
       
 
        ds = New DataSet
        da.Fill(ds)
 
FinalsortDataGridView.DataSource = ds.Tables(0)
FinalsortDataGridView.Refresh()
        cn.Close()
    End Sub
  Permalink  
v2
Comments
bhuvamehul at 9-Jul-13 1:10am
   
If there is no issue with database connection then this will definitely work.
sudeshna from bangkok at 9-Jul-13 1:24am
   
its not working. database conection is ok.as i am working with other forms also and they are getting updated in database
sudeshna from bangkok at 9-Jul-13 1:43am
   
sir, how this will work? where is a loop? suppose same name different data is there,then it should show all data related to name in gridview
bhuvamehul at 9-Jul-13 3:20am
   
when we assign dataset as datasource of gridview then it will set all rows of table to gridview. There is no need to use for loop because in query we just get all rows which we want and those rows assign to gridview by setting it's datasource property.
other issue is I think with name keyword. try query like:
Select * from finalsort where [name]='" & ComboBox1.SelectedItem & "'"
 
otherwise take ComboBox1.SelectedText
 
May be it works
sudeshna from bangkok at 9-Jul-13 6:56am
   
FinalsortDataGridView.DataSource = ds.Tables(0)
 
why ds.tables(0)? why not
ds.tables("finalsort")
sudeshna from bangkok at 9-Jul-13 6:59am
   
data is not showing,only at run time the column names are showing.
 

'Dim strcon As String = ("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
Dim cn As New SqlConnection("Data Source=.\INSTANCE;initial catalog=record;user=sa;password=gariahat")
'Dim da As New SqlDataAdapter
Dim cmd As New SqlCommand
Dim ds As DataSet
Dim dt As DataTable
 

'Try
 
cmd.Connection = cn
cn.Open()
 

 
Dim da As New SqlDataAdapter("select * from finalsort where [name]='" & ComboBox1.SelectedText & "'", cn)
 
'dt = New DataTable
ds = New DataSet
da.Fill(ds)
 
FinalsortDataGridView.DataSource = ds.Tables(0)
cn.Close()
 

End Sub
 

this is the full search code i have written
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

This code working fine in my case without any problem:
 
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim constr As String = "<your connection="" string="">"
        Dim cn As New SqlConnection(constr)
        cn.Open()
        Dim da As New SqlDataAdapter("Select * from Table1 where name='" & ComboBox1.SelectedItem & "'", cn)
        Dim ds As New DataSet()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        DataGridView1.Refresh()
        cn.Close()
    End Sub</your>
  Permalink  
Comments
sudeshna from bangkok at 9-Jul-13 9:30am
   
in the form, shall i have to bind something? like in the dataadapter or just the code will do?
and why ds.tables(0) what does it indicate?
 
if i write the code under combobox1_selectedindexchanged, then i dont need any button_click,right?
Teja Guntuku at 22-Aug-13 6:48am
   
sir , i want to disply data in gridview and that data access from data base and also which is not shown in grid view fully
ex:
name phonenum add
teja 9490**** hyd***
like this is there any queary for that will you please help me
sudeshna from bangkok at 9-Jul-13 10:58am
   
sir,do u have any other solution or any other way? please help me sir
sudeshna from bangkok at 10-Jul-13 1:28am
   
sir,its not working,can you help please sir
sudeshna from bangkok at 10-Jul-13 1:29am
   
i did like query builder also,but there no value is showing as i clickling on execute query.
 
no data showing based on name. can you tell me the changes that i need to made in table adapter or binding source
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You have to bind gridview on bast of search condition.
if you want to serch by name then your query will be,
 
 
Select * from finalsort where name=@name 
 
@name is a parameter which should be passed from code behind...Smile | :)
 
then you can bind gridview with Datatable.
  Permalink  
v2
Comments
sudeshna from bangkok at 9-Jul-13 6:49am
   
i did like this but still not showing data in gridview.
can you elaborate a bit more and what code to write in vb.net

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 9 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100