Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Experts,

am working on Asp.net C#, SqlServer 2005.

on my webpage I have Gridview to display data it working fine.

I have Dropdownlist, TextBox, Button To Search the Records based on column selected in Dropdownlist.

I have column names as FirstName,LastName,UserName...These will be in Dropdownlist

User will select FirstName from dropdownlist and enter FirstName in TextBox and clicks on Search button.

This is my code for Search button
=================================
C#
sqlconnection con = new sqlconnection(_connString);
sqlcommand cmd = new sqlCommand("Select * from TableName where FirstName='" + TxtSearch.Text + "'",con);
sqldataadapter da = new sqldataadapter(cmd);
dataset ds = new dataset();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();


So, My Requirement is how to Work with Based on Dropdownlist.

Please help.

Thanks
Posted
Updated 2-Sep-20 9:40am
v2

C#
SqlConnection con = new SqlConnection(_connString);
            SqlCommand cmd = null;
            if (DropDownList1.SelectedItem.Text == "FirstName")
            {
                 cmd = new SqlCommand("Select * from TableName where FirstName='" + TxtSearch.Text + "'", con);
            }
            else if (DropDownList1.SelectedItem.Text == "LastName")
            {
                cmd = new SqlCommand("Select * from TableName where LastName='" + TxtSearch.Text + "'", con);
            }
            else if (DropDownList1.SelectedItem.Text == "UserName")
            {
                cmd = new SqlCommand("Select * from TableName where UserName='" + TxtSearch.Text + "'", con);
            }
         
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.Dispose();
            DataSet ds = new DataSet();
          
            da.fill(ds);
            GVDisplay.Datasource = ds;
            GVDisplay.DataBind();
 
Share this answer
 
v3
Comments
Member239258 31-Aug-14 16:58pm    
Many Thanks.
Its very simple try following code...
C#
sqlconnection con = new sqlconnection(_connString);
sqlcommand cmd = new sqlCommand("Select * from TableName where ["+DropDownList1.SelectedItem.Text+"] = '" + TxtSearch.Text + "'",con);
sqldataadapter da = new sqldataadapter(cmd);
dataset ds = new dataset();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();

I have just changed only one line of your code
C#
sqlcommand cmd = new sqlCommand("Select * from TableName where [" + DropDownList1.SelectedItem.Text + "] = '" + TxtSearch.Text + "'",con);

Hope it works for you...
 
Share this answer
 
Comments
Member239258 31-Aug-14 16:58pm    
Many Thanks.
Hi, is there a way to do this without clicking search button. I mean , if we just select the option from dropdown list(without search button) and data appears in grid view.

In Below code i dont want to keep search button to display data.So What is the correct way to do this as i am getting error for below code.


<asp:dropdownlist id="DropDownList3" runat="server" width="166px" onselectedindexchanged="DropDownList1_SelectedIndexChanged" ontextchanged="DropDownList1_TextChanged" autopostback="True">
<asp:listitem text="Select Action" value="Select Action">
<asp:listitem text="Cancel" value="">
<asp:listitem text="Unroute" value="">
<asp:listitem text="Floor1" value="LYN">
<asp:listitem text="Floor2" value="">
<asp:listitem text="Floor3" value="">
<asp:listitem text="Floor4" value="">

 
Share this answer
 
Comments
CHill60 3-Sep-20 8:38am    
If you have a question then use the red "Ask a Question" link at the top of this page. Do not post questions or comments as solutions to other members posts

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