The below sections of code are part of a list administration project I am working on to teach myself C#. This is from a tab that is used for list administration. There is a drop down box that allows selection of list administrators, which populates a dropdown box with available lists.
Once a list is selected, it populates two listboxes with users associated with the list administrators organization (such as a dance school), one has users that are in the list, the others are ones not in the list.
Before I start coding the moving of users between boxes and updating the tables, have I approached the methodology in the most efficient way? I know it works, but is there a method of setting it up I should look at to make things easier as I move forward?
Code for list selection and listbox population:
/*
* DATA MANIPULATION AND FORM ROUTINES FOR THE LISTS TAB
*
*/
private void btnEditList_Click(object sender, EventArgs e)
{
string disp = cmbListAdmins.SelectedValue.ToString();
string listQuery = "SELECT * FROM test.tbl_lists WHERE test.tbl_lists.user_id = " + disp + ";";
dbconn = new MySqlConnection(connString);
MySqlDataAdapter listadapter = new MySqlDataAdapter(listQuery, dbconn);
DataTable dtAvailLists = new DataTable();
try
{
listadapter.Fill(dtAvailLists);
dtAvailLists.Columns.Add("FullName", typeof(string), "list_name + ' ' + list_description");
BindingSource bindsource1 = new BindingSource();
bindsource1.DataSource = dtAvailLists;
cmbListSelect.DataSource = bindsource1;
cmbListSelect.DisplayMember = "FullName";
cmbListSelect.ValueMember = "list_id";
cmbListSelect.Enabled = true;
}
catch (Exception err)
{
File.AppendAllText(logFileName,
string.Format("{0}: Unable to fill list admin combo: Query is {1}, result is {2}.",
DateTime.Now,
listQuery,
err,
System.Environment.NewLine));
}
dbconn.Close();
}
private void cmbListSelect_SelectionChangeCommitted(object sender, EventArgs e)
{
lstExistingMembers.Items.Clear();
lstAvailableMembers.Items.Clear();
string disp = cmbListSelect.SelectedValue.ToString();
string memberQuery = "SELECT lm.list_id, lm.user_id, ua.user_first_name, ua.user_last_name, ua.user_id " +
"FROM test.tbl_listmembers AS lm " +
"LEFT JOIN test.tbl_user_accounts AS ua ON (ua.user_id = lm.user_id) " +
"WHERE (lm.list_id = '" + disp + "')";
dbconn = new MySqlConnection(connString);
MySqlDataAdapter listadapter = new MySqlDataAdapter(memberQuery, dbconn);
DataTable dtListMembers = new DataTable();
try
{
string fullname = "";
string notClause = "";
listadapter.Fill(dtListMembers);
foreach (DataRow dr in dtListMembers.Rows)
{
fullname = dr[2].ToString() + " " + dr[3].ToString();
lstExistingMembers.Items.Add(fullname);
lstExistingMembers.ValueMember = dr[4].ToString();
notClause += "'" + dr[4].ToString() + "',";
}
notClause = notClause.Substring(0, notClause.Length - 1);
string nonMemberQuery = "SELECT concat(user_first_name, ' ', user_last_name) as username, user_id FROM test.tbl_user_accounts " +
"WHERE user_id NOT IN (" + notClause + ");";
dbconntwo = new MySqlConnection(connString);
MySqlDataAdapter listadaptertwo = new MySqlDataAdapter(nonMemberQuery, dbconntwo);
DataTable dtListNonMembers = new DataTable();
try
{
listadaptertwo.Fill(dtListNonMembers);
foreach (DataRow dr2 in dtListNonMembers.Rows)
{
lstAvailableMembers.Items.Add(dr2[0].ToString());
lstAvailableMembers.ValueMember = dr2[1].ToString();
}
}
catch (Exception err)
{
File.AppendAllText(logFileName,
string.Format("{0}: Unable to fill list non members listbox: Query is {1}, result is {2}.",
DateTime.Now,
nonMemberQuery,
err,
System.Environment.NewLine));
}
dbconntwo.Close();
}
catch (Exception err)
{
File.AppendAllText(logFileName,
string.Format("{0}: Unable to fill list members listbox: Query is {1}, result is {2}.",
DateTime.Now,
memberQuery,
err,
System.Environment.NewLine));
}
dbconn.Close();
}
If needed, I can post the code from the database class that I have as well.