---
title: "error cannot convert from 'object' to 'string' when filling a asp:DropDownList dynamically"  
description: "error cannot convert from 'object' to 'string' when filling a asp:DropDownList dynamically"  
author: "Anonymous User"  
published: 2014-12-22  
updated: 2014-12-23  
canonical: https://www.mindstick.com/forum/12809/error-cannot-convert-from-object-to-string-when-filling-a-asp-dropdownlist-dynamically  
category: "asp.net"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# error cannot convert from 'object' to 'string' when filling a asp:DropDownList dynamically

I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to fill my droplist with [data from sql](https://www.mindstick.com/forum/12885/how-to-retrieving-data-from-sql-server-and-adding-it-to-arraylist) but when I try to add the list items I get the error in the title.

[SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) cmd = new SqlCommand(@"SELECT RLS.RoleName [RoleName],

URS.UserID [UserID],

USRS.[UserName](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username)[UserName],

USRS.[FirstName](https://www.mindstick.com/interview/33973/how-to-split-the-full-name-into-firstname-and-lastname-in-sql-server)[FirstName],

USRS.LastName[LastName]

FROM [Roles] RLS

[Inner JOIN](https://www.mindstick.com/forum/33572/sql-inner-join-keyword) [Users] USRS

[LEFT JOIN](https://www.mindstick.com/blog/304465/difference-between-inner-join-left-join-right-join-and-full-outer-join-in-sql-server) [UserRoles] URS ON USRS.[UserID] = URS.[UserID] ON RLS.[RoleID] = URS.[RoleID]

WHERE RLS.[RoleName] = 'Blog Editors'",conn);

conn.Open();

using (SqlDataReader reader1 = cmd.ExecuteReader())

{

while (reader1.Read())

{

int numUserID = reader1.GetInt32(1);

string strFirstName = reader1.GetString(3);

string strLastName = reader1.GetString(4);

string newUserName = strFirstName + " " + strLastName;

[SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter) da = new SqlDataAdapter(cmd);

[DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) dt = new DataTable();

da.Fill(dt);

foreach (DataRow row in dt.Rows)

{

ddlCreatedBy.Items.Add(new [ListItem](https://www.mindstick.com/forum/23267/insert-a-listitem-to-a-dropdownlist)(row["newUserName"], row["numUserId"]));

}

}

}

## Replies

### Reply by Pravesh Singh

Let me briefly dissect your code here:

```
using (SqlDataReader reader1 = cmd.ExecuteReader()){    while (reader1.Read())    {        int numUserID = reader1.GetInt32(1);        string strFirstName = reader1.GetString(3);        string strLastName = reader1.GetString(4);        string newUserName = strFirstName + " " + strLastName;         SqlDataAdapter da = new SqlDataAdapter(cmd);        DataTable dt = new DataTable();        da.Fill(dt);        foreach (DataRow row in dt.Rows)        {            ddlCreatedBy.Items.Add(new ListItem(row["newUserName"], row["numUserId"]));        }    }}
```

You read from the reader record by record just to execute the same statement over and over for each record to fill a data set? This means that for every record you fill a new dataset with all the records - that's pretty redundand, isn't it?

Also, your SQL statement never selects a newUserName or numUserId column, so you can't access either column in any row of the table.

Don't you actually want to do the following? This reads the user data line by line, assembles the new user name and then creates a new entry in the list.

```
using (SqlDataReader reader1 = cmd.ExecuteReader()){    while (reader1.Read())    {        int numUserID = reader1.GetInt32(1);        string strFirstName = reader1.GetString(3);        string strLastName = reader1.GetString(4);        string newUserName = strFirstName + " " + strLastName;         ddlCreatedBy.Items.Add(new ListItem(newUserName, numUserId.ToString()));    }}
```


---

Original Source: https://www.mindstick.com/forum/12809/error-cannot-convert-from-object-to-string-when-filling-a-asp-dropdownlist-dynamically

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
