Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Comments
jaideepsinh 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 8-Jul-13 6:18am    
What have you done so far?
Where are you stuck?
sudeshna from bangkok 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 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 8-Jul-13 23:58pm    
I have posted my code,.

can anyone help me plz

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

VB
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>
 
Share this answer
 
Comments
sudeshna from bangkok 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 8-Jul-13 8:15am    
what ever you want. Based on which name you want to retrieve data from database.
sudeshna from bangkok 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 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 9-Jul-13 0:55am    
So,just change in first line like this:

dim str as String=ComboBox1.SelectedText
VB
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
 
Share this answer
 
v2
Comments
bhuvamehul 9-Jul-13 1:10am    
If there is no issue with database connection then this will definitely work.
sudeshna from bangkok 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 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 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 9-Jul-13 6:56am    
FinalsortDataGridView.DataSource = ds.Tables(0)

why ds.tables(0)? why not
ds.tables("finalsort")
This code working fine in my case without any problem:

VB
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>
 
Share this answer
 
Comments
sudeshna from bangkok 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 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 9-Jul-13 10:58am    
sir,do u have any other solution or any other way? please help me sir
sudeshna from bangkok 10-Jul-13 1:28am    
sir,its not working,can you help please sir
sudeshna from bangkok 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
You have to bind gridview on bast of search condition.
if you want to serch by name then your query will be,

SQL
Select * from finalsort where name=@name 


@name is a parameter which should be passed from code behind...:)

then you can bind gridview with Datatable.
 
Share this answer
 
v2
Comments
sudeshna from bangkok 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)



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