All I would like is for help with putting this code into separate classes as I cant seem to figure it out. I would just like to understand how this would be possible with this set of code.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace FinalDatabaseConnection {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
//if statment dealing with sql queries once button hit
private void button1_Click(object sender, EventArgs e) {
lblCompleteItemInfo.Text = ("");
String source = @"Data Source=WIN10-LAP->HJP\MSSQLSERVER1;Initial Catalog=VisualStudioConnect;Integrated Security=True";
SqlConnection con = new SqlConnection(source);
con.Open();
if ((string) cbfunctionSelection.SelectedItem == "Find" && checkBoxPCSearch.Checked) {
String sqlSelectQuery = "SELECT * FROM Items Where PartCode = @SelectedPartCode";
var cmd = new SqlCommand(sqlSelectQuery, con);
SetInsertParameters(cmd);
SqlDataReader dr = cmd.ExecuteReader();
SetResponse(dr);
AllVisible();
}
else if ((string)cbfunctionSelection.SelectedItem == "Find" && checkBoxNameSearch.Checked) {
checkBoxPCSearch.Checked = false;
String sqlSelectQuery = "SELECT * FROM Items Where Name = @SelectedName";
var cmd = new SqlCommand(sqlSelectQuery, con);
SetInsertParameters(cmd);
SqlDataReader dr = cmd.ExecuteReader();
SetResponse(dr);
AllVisible();
}
if ((String) cbfunctionSelection.SelectedItem == "Add") {
checkBoxNameSearch.Checked = false;
var cmd = new SqlCommand(
"INSERT INTO items(Description,Colour,Manufacturer,StockLevel,Name) " +
"VALUES ( @Description, @Colour, @Manufacturer, @StockLevel,@Name)", con);
SetInsertParameters(cmd);
cmd.ExecuteReader();
}
if ((string) cbfunctionSelection.SelectedItem == "Delete") {
var sqlDeleteQuery = "Delete from items where PartCode = @SelectedPartCode" ;
SqlCommand cmd = new SqlCommand(sqlDeleteQuery, con);
SetInsertParameters(cmd);
SqlDataReader myreader;
myreader = cmd.ExecuteReader();
myreader.Read();
}
if ((string) cbfunctionSelection.SelectedItem == "Edit") {
var cmd = new SqlCommand("update items SET Description = @Description ,Colour =@Colour ,Manufacturer = @Manufacturer,StockLevel = @StockLevel , Name = @Name where partcode = @SelectedPartCode", con);
SetInsertParameters(cmd);
cmd.ExecuteReader();
}
else {
MessageBox.Show("PLease refrence the helper text box for help i");
}
con.Close();
}
//setting the response for the find function
private void SetResponse(SqlDataReader dr) {
if (dr.Read()) {
string itemDescription = (dr["Description"].ToString());
txtColour.Text = itemDescription;
string itemColour = (dr["Colour"].ToString());
txtColour.Text = itemColour;
string itemManufacturer = (dr["Manufacturer"].ToString());
txtColour.Text = itemManufacturer;
string itemStockLevel = (dr["StockLevel"].ToString());
txtColour.Text = itemStockLevel;
string itemName = (dr["Name"].ToString());
txtColour.Text = itemName;
string itemPartCode = (dr["PartCode"].ToString());
txtColour.Text = itemPartCode;
lblCompleteItemInfo.Text = ($"The items description is: {itemDescription} " +
$"The items colour is: {itemColour} " +
$"The items Manufacturer is: {itemManufacturer} " +
$"The Items Stock Level is :{itemStockLevel} " +
$"The Items Name is: {itemName} " +
$"The items PartCode is: {itemPartCode} ");
}
else {
MessageBox.Show("error");
}
}
//applying the values for sql queries through the input in tbox
private void SetInsertParameters(SqlCommand cmd) {
cmd.Parameters.AddWithValue("@Description", txtDescription.Text);
cmd.Parameters.AddWithValue("@Colour", txtColour.Text);
cmd.Parameters.AddWithValue("@Manufacturer", txtManufacturer.Text);
cmd.Parameters.AddWithValue("@StockLevel", txtStockLevel.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@PartCode", txtPartCode.Text);
cmd.Parameters.AddWithValue("@SelectedPartCode", txtPartCodeEnter.Text);
cmd.Parameters.AddWithValue("@SelectedName", txtNameEnter.Text);
}
private void AllVisible() {
//txtboxes
txtDescription.Visible = true;
txtColour.Visible = true;
txtManufacturer.Visible = true;
txtStockLevel.Visible = true;
txtName.Visible = true;
txtPartCode.Visible = true;
txtPartCodeEnter.Visible = true;
txtNameEnter.Visible = true;
//lbls
lblDescription.Visible = true;
lblColour.Visible = true;
lblManufacturer.Visible = true;
lblStockLevel.Visible = true;
lblName.Visible = true;
lblPartCode.Visible = true;
lblNameEnter.Visible = true;
lblPartCode.Visible = true;
checkBoxNameSearch.Visible = true;
checkBoxPCSearch.Visible = true;
}
private void cbfunctionSelection_SelectedIndexChanged(object sender, EventArgs e) {
if ((String) cbfunctionSelection.SelectedItem == "Add" ) {
AllVisible();
checkBoxNameSearch.Visible = false;
checkBoxPCSearch.Visible = false;
txtPartCodeEnter.Visible = false;
txtNameEnter.Visible = false;
txtPartCode.Visible = false;
lblPartCode.Visible = false;
lblNameEnter.Visible = false;
lblPartCode.Visible = false;
HelpLabel.Text = ("input the details for the item to be added.");
}
else if ((String) cbfunctionSelection.SelectedItem == "Find" ) {
AllVisible();
txtDescription.Visible = false;
txtColour.Visible = false;
txtManufacturer.Visible = false;
txtStockLevel.Visible = false;
txtName.Visible = false;
txtPartCode.Visible = false;
lblDescription.Visible = false;
lblColour.Visible = false;
lblManufacturer.Visible = false;
lblStockLevel.Visible = false;
lblName.Visible = false;
lblPartCode.Visible = false;
HelpLabel.Text = ("input the Partcode of the item and the information will be displayed.");
}
else if ((String) cbfunctionSelection.SelectedItem == "Edit") {
AllVisible();
lblPartCode.Visible = false;
txtPartCode.Visible = false;
txtNameEnter.Visible = false;
lblNameEnter.Visible = false;
checkBoxNameSearch.Visible = false;
checkBoxPCSearch.Visible = false;
HelpLabel.Text = ("Input the partcode of the item you would like to edit. Then the Information.");
}
else if ((String) cbfunctionSelection.SelectedItem == "Delete") {
AllVisible();
txtDescription.Visible = false;
txtColour.Visible = false;
txtManufacturer.Visible = false;
txtStockLevel.Visible = false;
txtName.Visible = false;
txtPartCode.Visible = false;
txtNameEnter.Visible = false;
checkBoxNameSearch.Visible = false;
checkBoxPCSearch.Visible = false;
lblDescription.Visible = false;
lblColour.Visible = false;
lblManufacturer.Visible = false;
lblStockLevel.Visible = false;
lblName.Visible = false;
lblPartCode.Visible = false;
lblNameEnter.Visible = false;
HelpLabel.Text = ("Input the partcode of the item you would like to delete.");
}
}
private void button1_Click_1(object sender, EventArgs e) {
btnDisplayItemTable.Text = ("Refresh items table");
//This is database connection string
String source = @"Data Source=WIN10-LAP->HJP\MSSQLSERVER1;Initial Catalog=VisualStudioConnect;Integrated Security=True";
//this is defining source as variable "con" to be used for SQL connection
SqlConnection con = new SqlConnection(source);
//opens connection
con.Open();
// just message box
MessageBox.Show("connected");
SqlDataAdapter sqlDa = new SqlDataAdapter("Select * From Items",source);
DataTable dtb1 = new DataTable();
sqlDa.Fill(dtb1);
dgv1.DataSource = dtb1;
}
private void button1_Click_2(object sender, EventArgs e)
{
new Form3().Show();
}
}
}
Form1.Designer.cs
file as well? \$\endgroup\$ – Henrik Hansen Oct 18 at 11:17