This is a small section of a database front end application written in C#. The database was designed to handle account data for an MMORPG. This form stores information for in game purchases but uses 3 different comboboxes which take data from other entities. The information displayed in each combobox can change depending on the chosen selection.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MySQL_Interface
{
public partial class ingamepurchase_form : Form
{
Form1 firstformRef; //reference to main form
bool insertFlag = false; //are we in update mode?
string updateId = ""; //record update ID if required
//db object
DBConnection mySqlSrc = new DBConnection();
public ingamepurchase_form(ref Form1 form1Handle, bool isInsert, string upId)
{
//set vars from constructor locals
updateId = upId;
insertFlag = isInsert;
firstformRef = form1Handle;
InitializeComponent();
//Bind comboBox to dictionary
//This allows us a key - value pair
//*********-------------CHARACTER COMBOBOX----------------*****
Dictionary<string, string> igpCharIDItem = new Dictionary<string, string>();
//Get a new list for results of CHARACTER table
List<string>[] results = new List<string>[7];
results = mySqlSrc.SelectCharacter();
//count the rows
int rows = results[0].Count;
for (int i = 0; i < rows; ++i)
{
//[column][row]
//add to the dictionary object the ID
igpCharIDItem.Add(results[0][i], results[4][i]);
}
//bind the dictionary object and set Key -> Value
//as id _> name
igp_charID_cb.DataSource = new BindingSource(igpCharIDItem, null);
igp_charID_cb.DisplayMember = "Value";
igp_charID_cb.ValueMember = "Key";
//*********-------------CATAGORY COMBOBOX----------------*****
Dictionary<string, string> igpcatItem = new Dictionary<string, string>();
igpcatItem.Add("1", "Item");
igpcatItem.Add("2", "Weapon");
igpcatItem.Add("3", "Armour");
int c_rows = igpcatItem.Count;
//bind the dictionary object and set Key -> Value
//as id _> name
igp_cat_cb.DataSource = new BindingSource(igpcatItem, null);
igp_cat_cb.DisplayMember = "Value";
igp_cat_cb.ValueMember = "Key";
//*********-------------ITEM COMBOBOX----------------*****
igp_cat_cb.SelectedIndexChanged += new EventHandler(igp_item_cb_SelectedIndexChanged);
igp_item_cb.SelectedIndexChanged += new EventHandler(igp_price_txt_TextChanged);
//get item results
List<string>[] iresults = new List<string>[6];
iresults = mySqlSrc.SelectItem();
//get weapon results
List<string>[] wresults = new List<string>[6];
wresults = mySqlSrc.SelectWeapon();
//get armour results
List<string>[] aresults = new List<string>[6];
aresults = mySqlSrc.SelectArmour();
//count the rows
int arows = aresults[0].Count;
int wrows = wresults[0].Count;
int irows = iresults[0].Count;
//------------------------------if not insert mode (update mode)
if (!insertFlag)
{
//create new results list
List<string>[] upResults = new List<string>[5];
//send ID to get results
upResults = mySqlSrc.SelectInGamePurchaseRow(updateId);
//update combo box
//get CHARACTER ID from the selected record
string temp_charID = upResults[1][0].ToString();
//get TYPE ID from the selected record
string temp_typeID = upResults[2][0].ToString();
//get TYPE ID from the selected record
string temp_name = upResults[3][0].ToString();
//loop through rows of full CHARACTER results
//(to loop through combo box)
for (int i = 0; i < rows; ++i)
{
//get the account_id of the current combo box index
string value = ((KeyValuePair<string, string>)igp_charID_cb.Items[i]).Key;
//if the value matches the current character_id
//set the SelectedIndex to this interation
//To display correct character
if (value == temp_charID)
igp_charID_cb.SelectedIndex = i;
}
//loop through rows of full CATAGORY results
//(to loop through combo box)
for (int i = 0; i < c_rows; ++i)
{
//get the account_id of the current combo box index
string value = ((KeyValuePair<string, string>)igp_cat_cb.Items[i]).Key;
//if the value matches the current CAT ID
//set the SelectedIndex to this interation
//To display correct character
if (value == temp_typeID)
igp_cat_cb.SelectedIndex = i;
}
//loop through rows of full ITEM TYPE results
//(to loop through combo box)
if(((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Item")
{
for (int i = 0; i < irows; ++i)
{
//get the id of the current combo box index
string value = ((KeyValuePair<string, string>)igp_item_cb.Items[i]).Value;
//if the value matches the current ITEM ID
//set the SelectedIndex to this interation
//To display correct character
if (value == temp_name)
igp_item_cb.SelectedIndex = i;
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Armour")
{
for (int i = 0; i < arows; ++i)
{
//get the id of the current combo box index
string value = ((KeyValuePair<string, string>)igp_item_cb.Items[i]).Value;
//if the value matches the current ITEM ID
//set the SelectedIndex to this interation
//To display correct character
if (value == temp_name)
igp_item_cb.SelectedIndex = i;
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Weapon")
{
for (int i = 0; i < wrows; ++i)
{
//get the id of the current combo box index
string value = ((KeyValuePair<string, string>)igp_item_cb.Items[i]).Value ;
//if the value matches the current ITEM ID
//set the SelectedIndex to this interation
//To display correct character
if (value == temp_name)
igp_item_cb.SelectedIndex = i;
}
}
//update text box
igp_price_txt.Text = upResults[4][0].ToString();
}
}
private bool ValidateFields()
{
//valid flag and messagte
bool validFlag = true;
string validMsg = "";
if (igp_charID_cb.Text.ToString() == "")
{
//if not update message and flag
validMsg += "You need to select a character!";
validFlag = false;
}
if (igp_cat_cb.Text.ToString() == "")
{
//if not update message and flag
validMsg += "You need to select a catagory!";
validFlag = false;
}
if (igp_item_cb.Text.ToString() == "")
{
//if not update message and flag
validMsg += "You need to select an Item!";
validFlag = false;
}
if (igp_price_txt.Text.ToString() == "")
{
//if not update message and flag
validMsg += "Price has not been updated!";
validFlag = false;
}
//if invalid show message
if (!validFlag)
MessageBox.Show(validMsg);
//return result
return validFlag;
}
private void ins_igp_btn_Click(object sender, EventArgs e)
{
//check if insert or update mode; send relevant data
//to relevant methods to run queries following
//validation checks
if (insertFlag)
{
if (ValidateFields())
{
mySqlSrc.InsertInGamePurchase(((KeyValuePair<string, string>)igp_charID_cb.SelectedItem).Key.ToString(), //send character ID
((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Key.ToString(), //send itemtype ID
((KeyValuePair<string, string>)igp_item_cb.SelectedItem).Value.ToString(), //send item name
igp_price_txt.Text.ToString()); //send item price
this.Close();
}
}
else
{
if (ValidateFields())
{
mySqlSrc.UpdateInGamePurchase(updateId, ((KeyValuePair<string, string>)igp_charID_cb.SelectedItem).Key.ToString(), //send character ID
((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Key.ToString(), //send itemtype ID
((KeyValuePair<string, string>)igp_item_cb.SelectedItem).Value.ToString(), //send item name
igp_price_txt.Text.ToString()); //send item price
this.Close();
}
}
}
//form closing event
private void ingamepurchase_form_FormClosing(object sender, FormClosingEventArgs e)
{
firstformRef.ingamepurchaseFormIsOpen = false;
firstformRef.SelectInGamePurchaseResults();
}
private void igp_cat_cb_SelectedIndexChanged(object sender, EventArgs e)
{
Dictionary<string, string> igpitemItem = new Dictionary<string, string>();
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Item")
{
//Get a new list for results of ITEM table
List<string>[] iresults = new List<string>[6];
iresults = mySqlSrc.SelectItem();
//count the rows
int irows = iresults[0].Count;
for (int i = 0; i < irows; ++i)
{
//[column][row]
igpitemItem.Add(iresults[0][i], iresults[1][i]);
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Weapon")
{
//Get a new list for results of WEAPON table
List<string>[] wresults = new List<string>[6];
wresults = mySqlSrc.SelectWeapon();
//count the rows
int wrows = wresults[0].Count;
for (int i = 0; i < wrows; ++i)
{
//[column][row]
//add to the dictionary object the ID
igpitemItem.Add(wresults[0][i], wresults[1][i]);
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Armour")
{
//Get a new list for results of ARMOUR table
List<string>[] aresults = new List<string>[6];
aresults = mySqlSrc.SelectArmour();
//count the rows
int arows = aresults[0].Count;
for (int i = 0; i < arows; ++i)
{
//[column][row]
//add to the dictionary object the ID
igpitemItem.Add(aresults[0][i], aresults[1][i]);
}
}
//bind the dictionary object and set Key -> Value
//as id _> name
igp_item_cb.DataSource = new BindingSource(igpitemItem, null);
igp_item_cb.DisplayMember = "Value";
igp_item_cb.ValueMember = "Key";
}
private void igp_item_cb_SelectedIndexChanged(object sender, EventArgs e)
{
//------------------------Get a new list for results of ITEM table
List<string>[] iresults = new List<string>[6];
iresults = mySqlSrc.SelectItem();
List<string>[] wresults = new List<string>[6];
wresults = mySqlSrc.SelectWeapon();
List<string>[] aresults = new List<string>[6];
aresults = mySqlSrc.SelectArmour();
//count the rows
int arows = aresults[0].Count;
int wrows = wresults[0].Count;
int irows = iresults[0].Count;
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Armour")
{
for (int i = 0; i < arows; ++i)
{
//[column][row]
//add to the dictionary object the ID
if (((KeyValuePair<string, string>)igp_item_cb.SelectedItem).Key.ToString() == aresults[0][i])
igp_price_txt.Text = aresults[5][i];
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Weapon")
{
for (int i = 0; i < wrows; ++i)
{
//[column][row]
if (((KeyValuePair<string, string>)igp_item_cb.SelectedItem).Key.ToString() == wresults[0][i])
igp_price_txt.Text = wresults[5][i];
}
}
if (((KeyValuePair<string, string>)igp_cat_cb.SelectedItem).Value.ToString() == "Item")
{
for (int i = 0; i < irows; ++i)
{
//[column][row]
if (((KeyValuePair<string, string>)igp_item_cb.SelectedItem).Key.ToString() == iresults[0][i])
igp_price_txt.Text = iresults[5][i];
}
}
}
private void igp_price_txt_TextChanged(object sender, EventArgs e)
{
}
}
}