First off I would like to apologize as I am very new to C#. This may sound like a stupid question; however, I have not been able to find any examples anywhere on the internet or within the few books I have purchased on C#
I have a form that pulls quite a bit of data on load from a SQL Database. The First and Last names are inserted into two seperate comboBoxes one that will display the information as "First Last" and the other "Last, First" for easier browsing of names.
Thanks to some help on here the other day I learned how to populate a single textbox with the selected value of the comboBox. Thanks again for the assistance.
What I need to do is when a user is selected from the comboBox, all the information for that user will be displayed within certain textBoxes on the form. Example would be,
LastName = textBox1
FirstName = textBox2
MiddleName = textBox3
Do I need to write a seperate string for each of these fields, or can that data be pulled from one string that queries the SQL database for all data?
Any assistance you can provide would be greatly appreciated.
Here is what I have so far
enter code here
namespace Tempus.Menus
{
public partial class Employees : Form
{
public Employees()
{
InitializeComponent();
//Connect to database for Employees Table Headers
SqlConnection myConnection = new SqlConnection(@"Server=Server4\INSTANCE;Integrated Security=true;" +
"user id=userID;password=password;" +
"Trusted_Connection=yes;" +
"Database=Database;" +
"connection timeout=30");
try
{
myConnection.Open();
string SqlDataPull = String.Format("SELECT * FROM Employees WHERE Lname IS NOT NULL {0}", (checkBox1.Checked ? "AND Active='Y'" : ""));
//string SqlDataPull2 = String.Format("SELECT * FROM Employees WHERE Fname IS NOT NULL {0} ORDER By Fname", (checkBox1.Checked ? "AND Active='Y'" : ""));
SqlCommand cmd = new SqlCommand(SqlDataPull, myConnection);
cmd.CommandType = CommandType.Text;
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string strEmployee = String.Format("{0} {1}", dr["Fname"], dr["Lname"], dr["Mname"], dr["DOH"], dr["DOT"], dr["Active"], dr["DoNotRehireReason"], dr["PTO-balance"], dr["SNP-balance"], dr["Cred"]);
comboBox1.Items.Add(strEmployee);
string strEmployee2 = String.Format("{0}, {1}", dr["Lname"], dr["Fname"]);
comboBox2.Items.Add(strEmployee2);
int Fname = dr.GetInt32(0);
string firstName = String.Format("{0}", dr["Fname"]);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
finally
{
if (myConnection != null)
{
myConnection.Dispose();
}
}
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox2.SelectedIndexChanged += comboBox2_SelectedIndexChanged;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox1.SelectedItem.ToString();
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox2.SelectedIndex == -1)
{
textBox1.Text = string.Empty;
}
else
{
textBox1.Text = comboBox2.SelectedItem.ToString();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void Employees_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Main myNewForm = new Main();
myNewForm.Show();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
Reports myNewForm = new Reports();
myNewForm.Show();
this.Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void textBox8_TextChanged(object sender, EventArgs e)
{
}
private void textBox9_TextChanged(object sender, EventArgs e)
{
}
private void textBox7_TextChanged(object sender, EventArgs e)
{
}
private void textBox10_TextChanged(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
// logic here for if the box has now been checked
}
else
{
// what to do if the box has been unchecked
}
}
}
}