2024-02-12 10:06:46 -05:00
|
|
|
|
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;
|
|
|
|
|
|
2024-02-12 10:37:14 -05:00
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
|
2024-02-12 10:06:46 -05:00
|
|
|
|
namespace Database2
|
|
|
|
|
{
|
2024-02-12 10:27:33 -05:00
|
|
|
|
public partial class frmTest : Form
|
2024-02-12 10:06:46 -05:00
|
|
|
|
{
|
2024-02-12 10:27:33 -05:00
|
|
|
|
public frmTest()
|
2024-02-12 10:06:46 -05:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2024-02-12 10:37:14 -05:00
|
|
|
|
|
|
|
|
|
private void cboTest_SelectedIndexChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2024-02-12 11:13:30 -05:00
|
|
|
|
SqlConnection connString = null;
|
|
|
|
|
var myStudentID = "";
|
|
|
|
|
var myQuery = "";
|
|
|
|
|
|
|
|
|
|
if (cboTest.IsHandleCreated && cboTest.Focused)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//retrieve the student id
|
|
|
|
|
myStudentID = cboTest.SelectedValue.ToString().Trim();
|
|
|
|
|
myQuery = "SELECT student_id, fname, lname, acct_bal, dob FROM student WHERE student_id = '" +
|
|
|
|
|
myStudentID + "'";
|
|
|
|
|
|
|
|
|
|
//create the connection string
|
|
|
|
|
connString =
|
|
|
|
|
new SqlConnection(
|
|
|
|
|
"server=74.37.2.53; initial catalog=College2; connect timeout=30; User ID=admin;Password=password;");
|
|
|
|
|
//sql command
|
|
|
|
|
var cmdString = new SqlCommand();
|
|
|
|
|
|
|
|
|
|
connString.Open(); //open the connection
|
|
|
|
|
//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;
|
|
|
|
|
//set the command text
|
|
|
|
|
cmdString.CommandText = myQuery;
|
|
|
|
|
|
|
|
|
|
//adapter and dataset
|
|
|
|
|
var adapter = new SqlDataAdapter();
|
|
|
|
|
adapter.SelectCommand = cmdString;
|
|
|
|
|
var ds = new DataSet();
|
|
|
|
|
adapter.Fill(ds);
|
|
|
|
|
|
|
|
|
|
//check to see if any record is returned
|
|
|
|
|
if (ds.Tables[0].Rows.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
dgvStudents.Visible = true;
|
|
|
|
|
//get data source
|
|
|
|
|
dgvStudents.DataSource = ds.Tables[0];
|
|
|
|
|
//adjust aesthetics
|
|
|
|
|
dgvStudents.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGreen;
|
|
|
|
|
// Set the row and column header styles.
|
|
|
|
|
dgvStudents.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
|
|
|
|
|
dgvStudents.ColumnHeadersDefaultCellStyle.BackColor = Color.Green;
|
|
|
|
|
dgvStudents.ColumnHeadersDefaultCellStyle.Font = new Font(dgvStudents.Font, FontStyle.Bold);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dgvStudents.Visible = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
//close the connection
|
|
|
|
|
connString.Close();
|
|
|
|
|
}
|
2024-02-12 10:37:14 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void dgvStudents_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
2024-02-12 10:53:49 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2024-02-12 11:26:41 -05:00
|
|
|
|
cmdString.CommandText = "SELECT student_id,fname, lname, fname + ' ' + lname as fullname, acct_bal, dob FROM student;"; //query
|
2024-02-12 10:53:49 -05:00
|
|
|
|
|
|
|
|
|
//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();
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-12 10:37:14 -05:00
|
|
|
|
}
|
2024-02-12 10:06:46 -05:00
|
|
|
|
}
|
|
|
|
|
}
|