DataTable _dt = new DataTable();
using (SqlConnection _cs = new SqlConnection(\"Data Source=COMNAME; Initial Catalog=DATABASE; Integrated Security=True\"))
{
st
I think the problem is that you are trying to insert multiple columns into a comboBox (which can accept only one column). Adjust your SELECT statement so that it combines all of the data you need into one column:
Example: ´SELECT Name + ' ' LastName + ' '+ ID AS 'DoctorData' FROM Doctor´
By using the + operator you can combine multiple columns from the database, and represent it as a single piece of data. I think that your code should work after that (you can even comment the foreach loop, I think that adding data source to your comboBox will be enough)
You could also specify DisplayMember property of combobox to any of the column name. For example if you want to display Name field, you could do
Combobox1.DisplayMember="Name";
I'm not yet sure what is the exact error in your code, but if you're ok with not using DataTable
, you can do it this way:
using (SqlConnection sqlConnection = new SqlConnection("connstring"))
{
SqlCommand sqlCmd = new SqlCommand("SELECT * FROM Doctor", sqlConnection);
sqlConnection.Open();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
while (sqlReader.Read())
{
cbDoctor.Items.Add(sqlReader["name"].ToString());
}
sqlReader.Close();
}
For more information take a look at SqlDataReader reference on MSDN.
In orer to find the issue in the original code you posted, please provide information in which line you get the exception (or is it an error that prevents application from compiling?) and what is its whole message.