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
dotnetom
  • 24,551
  • 9
  • 51
  • 54
Manisha Bano
  • 1,853
  • 2
  • 22
  • 34
  • possible duplicate of [DropDownList's SelectedIndexChanged event not firing](http://stackoverflow.com/questions/4905406/dropdownlists-selectedindexchanged-event-not-firing) – emerson.marini Aug 16 '14 at 07: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 '14 at 07: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 – Manisha Bano Aug 16 '14 at 07: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..:) – Manisha Bano Aug 16 '14 at 09:44
  • @Manisha: I glad that it helped and waiting for a green tick :) – HaMi Aug 16 '14 at 10:09

3 Answers3

1

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

Sushmit Patil
  • 1,335
  • 1
  • 14
  • 26
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>
HaMi
  • 539
  • 6
  • 23
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.

davidallyoung
  • 1,302
  • 11
  • 15