Click here to Skip to main content
15,892,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all i have written the foll code..but it doesnt work..i am trying to add search
function to datagrid. the datagrid displays all data on page load..but now i want to
search data from the datagrid based on search text entered.

VB
Sub fill_grid()
        con.Open()
        com = New SqlCommand("proc_search", con)
        com.CommandType = Data.CommandType.StoredProcedure
        com.Parameters.Add(New SqlParameter("@Mode", Data.SqlDbType.VarChar, 20))
        com.Parameters.AddWithValue("@strFeedTitle", cmbtitle.Text)
        com.Parameters.AddWithValue("@strFeedNm", txtname.Text)
        com.Parameters.AddWithValue("@strFeedEmail", txtemail.Text)
        com.Parameters("@Mode").Value = "select1"
        da = New SqlDataAdapter
        da.SelectCommand = com
        da.SelectCommand.Connection = con
        ds = New DataSet
        'ds.Tables.Add("tbl1")
        da.Fill(ds, "tbl1")
        con.Close()
        Try
            If ds.Tables("tbl1").Rows.Count > 0 Then
                gd_feedback.DataSource = ds.Tables("tbl1").DefaultView
                gd_feedback.DataBind()
                gd_feedback.Visible = True
                gd_feedback.EnableViewState = True
            End If
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub
Posted
Updated 26-Jan-11 19:25pm
v2
Comments
DaveAuld 27-Jan-11 1:26am    
Edit: Added code block formtting
justinonday 27-Jan-11 1:26am    
Please put code on code block ..
m@dhu 27-Jan-11 1:39am    
Can you show the query?
debo_india 27-Jan-11 1:46am    
yes the query is as foll:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

ALTER Procedure [dbo].[proc_search]
(
@strFeedTitle varchar(5),
@strFeedNm varchar(20),
@strFeedEmail varchar(20),
@mode varchar(20)
)
AS

declare @strVar1 varchar(1000)
set @strVar1='Select * From ab_feedback ' --'where 1=1'
if @mode='select1'
begin

if @strFeedTitle<>'' --and not @strFeedTitle is null
set @strVar1=@strVar1 --+ 'AND' + @strFeedTitle --+@strFeedTitle

if @strFeedNm<>'' --and not @strFeedNm is null
set @strVar1=@strVar1 --+ 'AND' + @strFeedNm --+@strFeedNm

if @strFeedEmail<>'' --and not @strFeedEmail is null
set @strVar1=@strVar1 --+ 'AND' + @strFeedEmail --+@strFeedEmail

execute(@strVar1)
END
m@dhu 27-Jan-11 3:20am    
Based on your query if the given values are not null then
Select * From ab_feedback executes which gives all the records in the db.(where AND part is commented in the query).

You can make use of the DataView.RowFilter Property.Take a look at this to get some idea

http://authors.aspalliance.com/aspxtreme/sys/data/DataViewClassRowFilter.aspx[^]
 
Share this answer
 
Comments
Sandeep Mewara 27-Jan-11 1:46am    
Good suggestion!
Apart from Rowfilter option as suggested by Anupama, other option is to design a Search page for yourself.

You can put controls in your search panel based on which you need to have a search data. Then based on the values in the search field, modify your query. Basically, WHERE clause would get changed.
Handle the query generically and thus based on the search parameters supplied you get back the data from DB and display in Grid.
 
Share this answer
 
Comments
Anupama Roy 27-Jan-11 2:30am    
OP posted as answer,moving it here - thanx to all..but i have used stored procedure so RowFilter option wont work according to me..pl guide
Sandeep Mewara 27-Jan-11 2:33am    
Based on comment, looks like he was replying you :)
Anupama Roy 27-Jan-11 2:37am    
Right!I posted it under ur answer so that you get a notification & delete the answer,I don't have permission to delete answers :-)
Sandeep Mewara 27-Jan-11 3:36am    
Smart! Thanks... did exactly what you thought of. :)
Sandeep Mewara 27-Jan-11 3:36am    
About deleting power... don't worry... soon you will have!

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