0

I have 3 dropdownlist: select year, select make, select model. Upon select model the results should appear in a gridview. My tables are: Makes with [(pk)MakeID, MakeName]; Models with [(pk)ModelID, Make_ID, ModelYear, ModelName]; and Wipers with [(pk)WiperID, Model_ID, Description, Emplacement, Price]. When I step through debug, I see 6 counts of records found, but I do not see it in gridview

I've looked at these for help, but no answers

ASP.net DropDownList populates GridView

Binding gridview with arraylist asp.net/c#

My Default.aspx

    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <div id="wrapper" align="center">
    <asp:DropDownList ID="ddlYear" runat="server" 
     AutoPostBack="true" OnSelectedIndexChanged="Year_Changed">
    </asp:DropDownList>
    <asp:DropDownList ID="ddlMake" runat="server"
    AutoPostBack="true" OnSelectedIndexChanged="Make_Changed">
    </asp:DropDownList>
    <asp:DropDownList ID="ddlModel" runat="server" 
    AutoPostBack="true" 
    OnSelectedIndexChanged="Get_Wipers_By_Model">
    </asp:DropDownList>
    </div>
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:GridView ID="grdWiperList" runat="server">
    </asp:GridView>

   </form>

My Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Year drop down list is populated on page load
        string query = "SELECT DISTINCT ModelYear FROM Models";
        string connectionString = ConfigurationManager.ConnectionStrings["wiperConnectionString"].ToString();
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection conn = new SqlConnection(connectionString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = conn;
                conn.Open();
                ddlYear.DataSource = cmd.ExecuteReader();
                ddlYear.DataValueField = "ModelYear";
                ddlYear.DataBind();
                conn.Close();
            }
        }

        ddlYear.Items.Insert(0, new ListItem("Select Year", "0"));
        ddlMake.Enabled = false;
        ddlModel.Enabled = false;
        ddlMake.Items.Insert(0, new ListItem("Select Make", "0"));
        ddlModel.Items.Insert(0, new ListItem("Select Model", "0"));

    }
}

private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
    string connectionString = ConfigurationManager.ConnectionStrings["wiperConnectionString"].ToString();
    SqlCommand cmd = new SqlCommand(query);
    using (SqlConnection conn = new SqlConnection(connectionString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = conn;
            conn.Open();
            ddl.DataSource = cmd.ExecuteReader();
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            conn.Close();
        }
    }

    ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}

protected void Year_Changed(object sender, EventArgs e)
{
    //the Makes drop down list is populated on OnSelectedIndexChange event of the Year_Changed event
    ddlMake.Enabled = false;
    ddlModel.Enabled = false;
    ddlMake.Items.Clear();
    ddlModel.Items.Clear();
    ddlMake.Items.Insert(0, new ListItem("Select Make", "0"));
    ddlModel.Items.Insert(0, new ListItem("Select Model", "0"));

    int yearId = int.Parse(ddlYear.SelectedItem.Value);
    if (yearId > 0)
    {
        string query = string.Format("SELECT DISTINCT MakeID, MakeName FROM Makes JOIN Models ON Make_ID = MakeID WHERE ModelYear = {0}", yearId);
        BindDropDownList(ddlMake, query, "MakeName", "MakeID", "Select Make");
        ddlMake.Enabled = true;
    }
}

protected void Make_Changed(object sender, EventArgs e)
{
    ddlModel.Enabled = false;
    ddlModel.Items.Clear();
    ddlModel.Items.Insert(0, new ListItem("Select Model", "0"));

    int yearID = int.Parse(ddlYear.SelectedItem.Value);
    int makeId = int.Parse(ddlMake.SelectedItem.Value);
    if (makeId > 0)
    {
        string query = string.Format("SELECT ModelID, ModelName FROM Models WHERE ModelYear = {0} AND Make_ID = {1}", yearID, makeId);
        BindDropDownList(ddlModel, query, "ModelName", "ModelID", "Select Model");
        ddlModel.Enabled = true;
    }
}

protected void Get_Wipers_By_Model(object sender, EventArgs e)
{
    grdWiperList.DataSource = Connection.GetWipersByModel
    (!IsPostBack ? "%" : ddlModel.SelectedValue);
    grdWiperList.DataBind();

    }
}

My Connection.cs

public static ArrayList GetWipersByModel(string modelType)
    {
        ArrayList listResults = new ArrayList();
        string query = string.Format
        ("SELECT * FROM Wipers WHERE Model_ID LIKE '{0}'", modelType);

        try
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = query;
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    int wiperID = reader.GetInt32(0);
                    int model_id = reader.GetInt32(1);
                    string description = reader.GetString(2);
                    string itemNo = reader.GetString(3);
                    string emplacement = reader.GetString(4);
                    decimal price = reader.GetDecimal(5);
                    Wipers wipers = new Wipers(wiperID, model_id, description, itemNo, emplacement, price);
                   listResults.Add(wipers);
                }
            }
            finally
            {
                conn.Close();
            }
        return listResults;

    }

1 Answer 1

0

I think this because the updatePanel control try to add trigger between UpdatePanel control and DropDownList Control

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I commented out the UpdatePanel tags, and I am now able to see records in the gridview. I was using that since I had 3 dropdownlist dependant on the previous selection, will check to see if there are postback problems if selections are changed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.