This question is locked. New answers and comments are not allowed.
Dear ...,
My problem is:
In my database, I have "CLASS" table. This table has 3 column: ClassID, ClassName and ManagerID
Now, I want to show all ClassIDs that have the same ManagerID on Combobox.
My code:
Can you help me write an example or help me fix my code.
I want to get all ClassIDs that have the same ManagerID from database to Combobox.
In my code, cbClass is my combobox.
Thanks and best regards,
Pham Son Truong
My problem is:
In my database, I have "CLASS" table. This table has 3 column: ClassID, ClassName and ManagerID
Now, I want to show all ClassIDs that have the same ManagerID on Combobox.
My code:
public class Classes
{
private int id;
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public int ID
{
get { return id; }
set { id = value; }
}
public override string ToString()
{
return id.ToString();
}
}
private void LoadDataTocbClass()
{
cbClass.FindItemExact("hello");
for (int i = 0; i <
PrepareDataForcbClass
().Count; i++)
{
cbClass.Items.Add(PrepareDataForcbClass()[i]); // ERROR, WHY?
}
cbClass.DisplayMember = ("Name");
}
private List<Classes> PrepareDataForcbClass()
{
List<
Classes
> list = new List<
Classes
>();
string sql = "select ClassID FROM [AttendanceDB].[dbo].[CLASS]" +
"where ManagerID=@ManagerID";
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myCommand.CommandText = sql;
myCommand.Parameters.AddWithValue("@ManagerID", ManagerID);
myConnection.Open();
SqlDataReader myDataReader = myCommand.ExecuteReader();
Classes cl;
while (myDataReader.Read())
{
cl = new Classes();
cl.Name = myDataReader.GetString(0).ToString();
list.Add(cl);
}
myDataReader.Close();
myConnection.Close();
return list;
}
Can you help me write an example or help me fix my code.
I want to get all ClassIDs that have the same ManagerID from database to Combobox.
In my code, cbClass is my combobox.
Thanks and best regards,
Pham Son Truong