Torna al Thread

[CODE] using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using MySql; using MySql.Data; using MySql.Data.MySqlClient; namespace Magazzino { public partial class frmMain : Form { MySqlConnection CN = new MySqlConnection("uid=root; password=root; database=sample"); MySqlCommand Com = new MySqlCommand(); MySqlDataReader reader; string saveStats; public frmMain() { Com.Connection = CN; InitializeComponent(); } private void btnSave_Click(object sender, EventArgs e) { try { if (txtName.Text == "") { MessageBox.Show("Input name please.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtName.Focus(); } else if (txtAddress.Text == "") { MessageBox.Show("Input address please.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtName.Focus(); } else { if (saveStats == "new") { Com.CommandText = "INSERT INTO sampTbl(name_,address_) VALUES('" + txtName.Text + "','" + txtAddress.Text + "')"; Com.ExecuteNonQuery(); Com.Dispose(); MessageBox.Show("Record Saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information); } else if (saveStats == "edit") { Com.CommandText = "UPDATE sampTbl SET name_='" + txtName.Text + "', address_='" + txtAddress.Text + "' WHERE id_num='" + listView1.Items[listView1.FocusedItem.Index].Text + "'"; Com.ExecuteNonQuery(); MessageBox.Show("Record Updated.", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information); } loadvars(); btnClear.PerformClick(); } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } void loadvars() { try { listView1.Items.Clear(); if (txtSearch.Text == "") { Com.CommandText = "SELECT * FROM sampTbl ORDER BY name_ ASC"; } else { Com.CommandText = "SELECT * FROM sampTbl WHERE name_ LIKE '" + txtSearch.Text + "%' ORDER BY name_ ASC"; } reader = Com.ExecuteReader(); while (reader.Read()) { ListViewItem list = new ListViewItem(reader[0].ToString()); list.SubItems.Add(reader[1].ToString()); list.SubItems.Add(reader[2].ToString()); listView1.Items.AddRange(new ListViewItem[] { list }); } reader.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void listView1_Click(object sender, EventArgs e) { try { txtName.Text = listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text; txtAddress.Text = listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text; FrmPopup fp = new FrmPopup(); fp.IsName = listView1.Items[listView1.FocusedItem.Index].SubItems[1].Text; fp.IsAddress = listView1.Items[listView1.FocusedItem.Index].SubItems[2].Text; fp.Show(); saveStats = "edit"; } catch (Exception) { MessageBox.Show("No records in the list.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void bttnDelete_Click(object sender, EventArgs e) { try { Com.CommandText = "DELETE FROM sampTbl WHERE id_num='" + listView1.Items[listView1.FocusedItem.Index].Text + "'"; Com.ExecuteNonQuery(); MessageBox.Show("Record Deleted.", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Warning); loadvars(); btnClear.PerformClick(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnRefresh_Click(object sender, EventArgs e) { txtSearch.Text = ""; loadvars(); } private void txtSearch_TextChanged(object sender, EventArgs e) { loadvars(); } private void frmMain_Load(object sender, EventArgs e) { try { CN.Open(); saveStats = "new"; loadvars(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void btnClear_Click(object sender, EventArgs e) { txtName.Text = ""; txtAddress.Text = ""; txtName.Focus(); saveStats = "new"; } private void btnClose_Click(object sender, EventArgs e) { Application.ExitThread(); } } } [/CODE]
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5