0

I am an ASP .NET beginner, so please bear with me.

I have a database named Product which contains columns ProductName and ProductID.

I want to display all ProductName values in dropdownlist. For this, I first put all the values in a dataset and from there, I bind them to dropdownlist. But in the output, instead of ProductName values, I am getting the following values in dropdownlist:

System.Data.DataRowView

(Repeated equal to the number of rows in database)

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;

public partial class Demo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Database2.mdb;Persist Security Info=True");
            DataSet ds = new DataSet();
            string query = "SELECT ProductName FROM Product";
            OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
            conn.Open();
            adapter.Fill(ds, "Table1");
            conn.Close();
            DropDownList1.DataSource = ds;
            DropDownList1.DataBind();
        }
    }
}

Any idea what I am missing here? Your help will will be appreciated. Thanks.

1 Answer 1

2

Looks like you need to specify your DataValueField and DataTextField properties like;

DropDownList1.DataValueField = "ProductName";
DropDownList1.DataTextField = "ProductName";
Sign up to request clarification or add additional context in comments.

Comments

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.