Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have a textbox and a dropdown list,the ddllist has two values Id and Name,now if the user enters
1 and selects Id in ddlist then data of Id 1 should be displayed in gridview and if a user enters Name ie. myname and selects Name in ddlist then data based on myname should be displayed.

I have achieved this on two button clicks but unable to achieve this on ddl.
Code for Achieved

ASP.NET
<table cellpadding="0" cellspacing="0" class="style1">
        <tr>
            <td colspan="2">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
                <td><asp:ImageButton ID="ImageButton1" runat="server" Height="19px" 
        ImageUrl="~/images/calendar.png" onclick="ImageButton1_Click" Width="33px" /> </td>
        </tr>
        <tr>
            <td>
     <asp:Button ID="ById" runat="server" Text="Get Product by Id" 
        onclick="Button3_Click" /></td>
            <td>
             <asp:Button ID="ByName" runat="server" Text="Get Product by Name" 
        onclick="Button2_Click" /></td>
        <td> 
            <asp:Button ID="Button4" runat="server" Text="Get Product by Date" 
                onclick="Button4_Click" /></td>
        </tr>
    </table>

Button Click
C#
protected void ById_Click(object sender, EventArgs e)
    {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cn))
        {

            SqlCommand command = new SqlCommand("spgetbyid", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@id", TextBox1.Text));
            con.Open();
            GridView1.DataSource = command.ExecuteReader();
            GridView1.DataBind();
        }
protected void ByName_Click(object sender, EventArgs e)
    {
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection con = new SqlConnection(cn))
        {

            SqlCommand command = new SqlCommand("spgetpro", con);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@name",TextBox1.Text));
            con.Open();
            GridView1.DataSource = command.ExecuteReader();
            GridView1.DataBind();
        }
    }

Store Procedure
SQL
create precedure spgetbyid
@id int
as
begin
select * from tblproducts where id=@id
end

create precedure spgetpro
@name varchar(10)
as
begin
select * from tblproducts where name=@name
end


Want to Achieve the same functionality in Dropdown
ASP.NET
<asp:TextBox ID="txttwo" runat="server"></asp:TextBox>  
      <asp:DropDownList ID="ddllist" runat="server" Height="16px" AutoPostBack="true"
          onselectedindexchanged="ddllist_SelectedIndexChanged" >
          <asp:ListItem Value="0">Select</asp:ListItem>
          <asp:ListItem Value="1">Id</asp:ListItem>
          <asp:ListItem Value="2">Name</asp:ListItem>
          <asp:ListItem Value="3">Date</asp:ListItem>
      </asp:DropDownList>


I know how to Display data in gridview based on selected value in Dropdown
Eg
C#
protected void Page_Load(object sender, EventArgs e)
  {
      if(!IsPostBack)
      {
          string cn=ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
          using(SqlConnection connection =new SqlConnection(cn))
          {
              SqlCommand command = new SqlCommand("spgetcount", connection);
              command.CommandType = CommandType.StoredProcedure;
              connection.Open();
              SqlDataReader dr = command.ExecuteReader();
              ddl.DataTextField = "country";
              ddl.DataValueField = "country";
              ddl.DataSource=dr;
              ddl.DataBind();
              ddl.Items.Insert(0,new ListItem("Please Select"));
          }


      }

  }
  protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
  {
      string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
      using (SqlConnection connection = new SqlConnection(cn))
      {

          SqlCommand command = new SqlCommand("select * from country", connection);
          command.Parameters.AddWithValue("@country", ddl.SelectedValue);
          SqlDataAdapter da = new SqlDataAdapter(command);
          DataSet ds = new DataSet();
          da.Fill(ds);
          GridView1.DataSource = ds;
          GridView1.DataBind();

      }
  }
Posted
Updated 25-Jun-13 6:37am
v4

Your Stored Procedure should be like this
It will accept 2 Parameters
1. Flag - Which type of value passed
2. Value- What Value passed

SQL
Create Procedure dsp_GridData  
(
@flag VARCHAR(10),
@Value VARCHAR(10)
)
BEGIN
SET NOCOUNT ON
IF @flag = 'ID'
BEGIN
SELECT desired_field FROM YourTable where id = CONVERT(int,@value) /* AS your id is int*/
END

IF @flag = 'Country'
BEGIN
SELECT desired_field FROM YourTable where country = @value /* AS your id is int*/
END

AND so on..........

SET NOCOUNT OFF
END


In your C# code
C#
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        string flag;
        if(ddlList.SelectedValue == "ID")
       {
         flag = "ID";
        
       }
       and so on for all other flag
        string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
        using (SqlConnection connection = new SqlConnection(cn))
        {
            
            SqlCommand command = new SqlCommand("dsp_GridData", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@flag", flag);
            command.Parameters.AddWithValue("@value", TextBox1.Text);
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GridView1.DataSource = ds;
            GridView1.DataBind();
           
        }
    }

Remember following points
1. Never Use Select * in any stored procedures
2. Instead of creating 4 - 5 stored procedure include all in 1
 
Share this answer
 
v2
Comments
Member 9176543 26-Jun-13 0:34am    
Hi Madhav Hatwalne,
I get a error in the C# code
'string' does not contain a definition for 'text' and no extension method 'text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Store Procedure
Create Procedure dsp_GridData

@flag VARCHAR(10),
@Value VARCHAR(10)
As
BEGIN
SET NOCOUNT ON
IF @flag = 'ID'
BEGIN
SELECT id,Name,Description FROM tblproducts where id = CONVERT(int,@value) /* AS your id is int*/
END

IF @flag = 'Name'
BEGIN
SELECT Name,Description FROM tblproducts where Name = @value /* AS your id is int*/
END


SET NOCOUNT OFF
END

C#
protected void ddllist_SelectedIndexChanged(object sender, EventArgs e)
{
string flag;
if (ddllist.SelectedValue.text == "ID")
{
flag = "ID";

}
else if (ddllist.SelectedValue.text == "Name")
{
flag = "Name";

}
// and so on for all other flag
string cn = ConfigurationManager.ConnectionStrings["connectivity"].ConnectionString;
using (SqlConnection connection = new SqlConnection(cn))
{

SqlCommand command = new SqlCommand("dsp_GridData", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("@flag", flag);
command.Parameters.AddWithValue("@value", TextBox1.Text);
SqlDataAdapter da = new SqlDataAdapter(command);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

}
}
Madhav Hatwalne 26-Jun-13 0:56am    
Try to sort this issue you won't get spoon feeding entire your life
Madhav Hatwalne 26-Jun-13 2:08am    
I made mistake by writing ddl.SelectedValue.Text but it's ddl.SelectedValue Sorry!
you can use sqldatasource for the purpose of binding gridview with condition of parameter ,it would be easy if you want to do that much only.

http://www.c-sharpcorner.com/uploadfile/raj1979/working-with-sqldatasource-control-in-Asp-Net-3-5/[^]

Using the ASP.NET SQLDATASOURCE[^]
 
Share this answer
 
Replace with the following
ddl.selecteditem.value
 
Share this answer
 
Comments
Member 9176543 25-Jun-13 13:11pm    
Vipin kumar
The value should come from the textbox
The above code just binds dropdown with gridview.

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