1

i am trying to get data from the stored procedure into the dropdownlist when the event onSelectIndexChanged is fired. But after putting the break point i get to know that the event generated is not working i.e. the control doesnot even goes into that code.

 <tr>
        <td><asp:Label ID="Label5" runat="server" Text="Book Category"></asp:Label></td>
        <td>
            <asp:DropDownList ID="ddlBookCategory" runat="server" Height="20px" Width="250px" OnSelectedIndexChanged="ddlBookCategory_SelectedIndexChanged"  >
            </asp:DropDownList>
        </td>
    </tr>
    <tr>
        <td><asp:Label ID="Label4" runat="server" Text="Book Subject"></asp:Label></td>
        <td>
            <asp:DropDownList ID="ddlBookSubject" runat="server" Height="20px" Width="250px" >
            </asp:DropDownList>
        </td>
    </tr>

and the backend cs file coding is::

 protected void Page_Load(object sender, EventArgs e)
{


     connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ToString();
     lblmsg.Visible = false;

     OleDbConnection con = new OleDbConnection(connectionString);
     OleDbCommand cmd = new OleDbCommand();

     try
     {
         con.Open();
         cmd.CommandType = System.Data.CommandType.StoredProcedure;

         cmd.CommandText = "ShowBookCategory";
         cmd.Connection = con;



         OleDbDataReader reader = cmd.ExecuteReader();


         if (reader.HasRows)
         {
             while (reader.Read())
             {
                 ddlBookCategory.Items.Add(new ListItem(reader["CategoryName"].ToString()));
             }
         }

     }
     catch (OleDbException ex)
     {
         ex.Data.ToString();
     } 
}


 protected void ddlBookCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    ddlBookCategory.AutoPostBack = true;
    OleDbConnection con = new OleDbConnection(connectionString);
    OleDbCommand cmd2 = new OleDbCommand();
    MbERPLibraryBookSubjectProperty foc = new MbERPLibraryBookSubjectProperty();

    try
    {
        con.Open();
        cmd2.CommandType = System.Data.CommandType.StoredProcedure;
        cmd2.CommandText = "ShowBookSubjectWithCategory";
        cmd2.Connection = con;

        foc.CategoryName = ddlBookCategory.Text.ToString();
        cmd2.Parameters.Add(new OleDbParameter("@CategoryName", foc.CategoryName));
        cmd2.Parameters["@CategoryName"].Direction = System.Data.ParameterDirection.Input;

        OleDbDataReader reader2 = cmd2.ExecuteReader();
        if (reader2.HasRows)
        {
            while (reader2.Read())
            {
                ddlBookSubject.Items.Add(new ListItem(reader2["SubjectName"].ToString()));
            }
        }

    }
    catch (OleDbException ex)
    {
        ex.Data.ToString();
    } 
}

And my stored Procedure is:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[ShowBookSubjectWithCategory](
    @CategoryName varchar(50)
    )
AS
BEGIN
select * from BookSubjects Where CategoryName = @CategoryName
END
5
  • possible duplicate of DropDownList's SelectedIndexChanged event not firing Aug 16, 2014 at 7:05
  • 2
    Why you put ddlBookCategory.AutoPostBack = true; in your event handler?! You should put autopostback attribute as true in your DropDownList declaration
    – HaMi
    Aug 16, 2014 at 7:13
  • what change will AutoPostBack=true is going to do in attribute. also setting it attribute will make it static and i am making it dynamic Aug 16, 2014 at 7:25
  • @HaMi Thanks for your answer... it worked... but don give your ans in comment.. ans me so that i can give you a green tick..:) Aug 16, 2014 at 9:44
  • @Manisha: I glad that it helped and waiting for a green tick :)
    – HaMi
    Aug 16, 2014 at 10:09

3 Answers 3

1

Instead of declaring autoPostBack as true in your event handler, try to set it in DropDownList declaration like bellow:

<asp:DropDownList ID="ddlBookCategory" ... autopostback="true" ... >
...
</asp:DropDownList>
1

If you want to set ddlBookCategory.AutoPostBack = true; dynamically then set that in pageload event.

0

You need to add AutoPostBack =true to the control in order for the control to signal back to the server that an event has occurred. Once you do that you can set an event handler to a method in your code behind for the selected index changed event on that control.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.