Hi
I have below data . In Dropdown it is showing System.Data.DataRowView
C#
private void GetLocation()
{
try
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn_Live"].ConnectionString; ;
SqlCommand cmd = new SqlCommand("select docentry,locationname from Location", con);
cmd.CommandType = CommandType.Text;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
ddlLocation.DataSource = dt;
ddlLocation.DataBind();
con.Close();
}
}
catch (Exception ex)
{
ShowMessage("", ex.Message, "error");
}
}Thanks

Tahir AnsariRank #366 · 6.8K reputationPosted Sep 1, 2024, 7:55 AM EDT
Hi Ramco,
Dropdown list is displaying "System.Data.DataRowView" instead of the actual values, typically occurs when the DataTextField property of the dropdown list is not set. This property tells the dropdown list which column from the data source to display.
You can fix it by following way,
Set the DataTextField to the column name you want to display (in your case, locationname).
Optionally, set the DataValueField to another column, like docentry, if you need a value associated with each item
private void GetLocation() { try { con.ConnectionString = ConfigurationManager.ConnectionStrings["cnn_Live"].ConnectionString; SqlCommand cmd = new SqlCommand("SELECT docentry, locationname FROM Location", con); cmd.CommandType = CommandType.Text; if (con.State == ConnectionState.Closed) { con.Open(); } SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); if (dt.Rows.Count > 0) { ddlLocation.DataSource = dt; ddlLocation.DataTextField = "locationname"; // Display this column's value ddlLocation.DataValueField = "docentry"; // Value associated with each item ddlLocation.DataBind(); } con.Close(); } catch (Exception ex) { ShowMessage("", ex.Message, "error"); } }The dropdown list (ddlLocation) will now display the locationname values.
Each item in the dropdown will have a value corresponding to the docentry