From 0ec7dede32c4ffb83477d8b9f148da9f01f3d316 Mon Sep 17 00:00:00 2001 From: EggMan20339 <99349302+EggMan20339@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:53:49 -0500 Subject: [PATCH] added SQL connection on load --- Database2/frmTest.Designer.cs | 1 + Database2/frmTest.cs | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/Database2/frmTest.Designer.cs b/Database2/frmTest.Designer.cs index d6414aa..f9467fd 100644 --- a/Database2/frmTest.Designer.cs +++ b/Database2/frmTest.Designer.cs @@ -105,6 +105,7 @@ this.Controls.Add(this.cboTest); this.Name = "frmTest"; this.Text = "Form1"; + this.Load += new System.EventHandler(this.frmTest_Load); ((System.ComponentModel.ISupportInitialize)(this.dgvStudents)).EndInit(); this.ResumeLayout(false); } diff --git a/Database2/frmTest.cs b/Database2/frmTest.cs index 0150014..9f58007 100644 --- a/Database2/frmTest.cs +++ b/Database2/frmTest.cs @@ -24,11 +24,73 @@ namespace Database2 private void cboTest_SelectedIndexChanged(object sender, EventArgs e) { + + } private void dgvStudents_CellContentClick(object sender, DataGridViewCellEventArgs e) { + + + } + + private void frmTest_Load(object sender, EventArgs e) + { + + SqlConnection connString = null; + + try + { + //hide the data grid + dgvStudents.Visible = false; + + //create the connection string + connString = new SqlConnection("server=74.37.2.53; initial catalog=College2; connect timeout=30; User ID=admin;Password=password;"); + SqlCommand cmdString = new System.Data.SqlClient.SqlCommand(); + + //open the connection + connString.Open(); + cmdString.Connection = connString; + + //clear any parameters + cmdString.Parameters.Clear(); + //link the connection + cmdString.Connection = connString; + //create the command type + cmdString.CommandType = CommandType.Text; + //set the timeout + cmdString.CommandTimeout = 1500; + + //create and execute SQL statement + cmdString.CommandText = "SELECT student_id,fname, lname, ' ' + lname as fullname, acct_bal, dob FROM student;"; //query + + //create data adapter + SqlDataAdapter adapter = new SqlDataAdapter(); + adapter.SelectCommand = cmdString; + + //create and fill dataset + DataSet ds = new DataSet(); + adapter.Fill(ds); + + //get data source + //load combo boxes + cboTest.DataSource = ds.Tables[0]; + cboTest.ValueMember = "STUDENT_ID"; + cboTest.DisplayMember = "fullname"; + cboTest.SelectedIndex = -1; //Do not select any item + + + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + finally + { + connString.Close(); + } + } } } \ No newline at end of file