i'm using C# and database MS Access..i want to do a search by using dropdownlist and view in gridview..can anyone tell me or suggest me any link that can solve my problem..

Dani AI

Generated

A compact, Access-specific pattern that answers ’s request (filter a GridView by a DropDownList and show a count). correctly suggested using the DropDownList SelectedIndexChanged event; pointed to a QuickStart sample. The notes below show a minimal, safe implementation and common pitfalls to avoid when using MS Access (OleDb).

Populate the DropDownList once on first load, set AutoPostBack to true, then run a parameterized query in SelectedIndexChanged and bind the resulting DataTable to the GridView. Count rows with the DataTable.Rows.Count (more reliable than GridView.Rows.Count when paging is involved).

<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged"></asp:DropDownList>
<asp:GridView ID="gvResults" runat="server" AutoGenerateColumns="True"></asp:GridView>
<asp:Label ID="lblCount" runat="server"></asp:Label>
private string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\MyDb.accdb;Persist Security Info=False;";

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindCategories();        // SELECT DISTINCT Category ...
        BindGrid(null);          // initial unfiltered view
    }
}

protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
    BindGrid(ddlCategory.SelectedValue);
}

private void BindGrid(string category)
{
    string sql = string.IsNullOrEmpty(category)
        ? "SELECT * FROM MyTable"
        : "SELECT * FROM MyTable WHERE Category = ?";

    using (OleDbConnection cn = new OleDbConnection(connString))
    using (OleDbCommand cmd = new OleDbCommand(sql, cn))
    {
        if (!string.IsNullOrEmpty(category))
            cmd.Parameters.AddWithValue("?", category);

        using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
        {
            DataTable dt = new DataTable();
            da.Fill(dt);
            gvResults.DataSource = dt;
            gvResults.DataBind();
            lblCount.Text = dt.Rows.Count.ToString();
        }
    }
}

Troubleshooting notes:

  • Do not rebind the DropDownList on every postback (use if (!IsPostBack)).
  • Ensure AutoPostBack="True" so selection triggers the server event.
  • Use parameterized queries (OleDb uses positional ? placeholders).
  • For numeric fields, add a typed parameter (e.g., Add("?", OleDbType.Integer).Value = intVal).
  • Match provider to file type: ACE for .accdb, Jet for older .mdb files.

This follows ’s event-based approach and complements ’s sample by showing the Access/OleDb specifics and a simple counting method.

Recommended Answers

All 8 Replies

Let me get this straight.

You want to load up a GridView based on a Dropdownlist right?

Can you show what you have done so far or give me some pseudo code?

i don't know to start from where..can u give me some example..i try to do when i choose in dropdownlist, it will show what i choose..and also i want to count..

Read this

And then add the call on DropDownList Changed Index event.

i didn't understand what are u trying to tell me..can u give another example..

Can you tell me what exactly you didn't understand so I can clarify it to you.

Regards.

i'm still new in C# and asp.net..but the example u shown to me using SQL database..i'm using MS Access..i just want to have a dropdownlist which when i select from DDL it will show in gridview..did u get what i'm trying to find here?

I understand that.

But what exactly are you having trouble doing. The point of an example is to help you do something. MS Access will work in a very similar way.

Can you show me what you have so far.

Please see the link below:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.