0% found this document useful (0 votes)
13 views3 pages

Database App-WPS Office

The document contains three C# programs: a Windows Forms application for managing products in a MySQL database, a console program that performs basic arithmetic with exception handling, and a Windows Forms application for managing a list of students with various functionalities. Each program demonstrates different aspects of C# programming, including database interaction, exception handling, and user interface elements. The code snippets include event handling for user actions such as saving data, adding or deleting items, and sorting lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Database App-WPS Office

The document contains three C# programs: a Windows Forms application for managing products in a MySQL database, a console program that performs basic arithmetic with exception handling, and a Windows Forms application for managing a list of students with various functionalities. Each program demonstrates different aspects of C# programming, including database interaction, exception handling, and user interface elements. The code snippets include event handling for user actions such as saving data, adding or deleting items, and sorting lists.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

// 1. Database Application with MySQL using System; using System.Data; using System.Windows.

Forms;
using MySql.Data.MySqlClient;

public class ProductForm : Form { private TextBox txtProductNumber, txtName, txtPrice; private Button
btnSave, btnExit; private MySqlConnection conn;

public ProductForm()

conn = new MySqlConnection("server=localhost;database=yourdb;user=root;password=");

txtProductNumber = new TextBox();

txtName = new TextBox();

txtPrice = new TextBox();

btnSave = new Button { Text = "Save" };

btnExit = new Button { Text = "Exit" };

btnSave.Click += SaveProduct;

btnExit.Click += (s, e) => Application.Exit();

private void SaveProduct(object sender, EventArgs e)

string query = "INSERT INTO Product (ProductNumber, Name, Price) VALUES (@num, @name,
@price)";

MySqlCommand cmd = new MySqlCommand(query, conn);

cmd.Parameters.AddWithValue("@num", txtProductNumber.Text);

cmd.Parameters.AddWithValue("@name", txtName.Text);

cmd.Parameters.AddWithValue("@price", txtPrice.Text);

conn.Open();
cmd.ExecuteNonQuery();

conn.Close();

MessageBox.Show("Product Saved");

// 2. Console Program with Exception Handling using System; class Program { static void Main() { try
{ Console.Write("Enter first number: "); int num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter second number: "); int num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine($"Sum: {num1 + num2}"); Console.WriteLine($"Division: {(num2 != 0 ? (num1 /
(double)num2).ToString() : "Cannot divide by zero")}"); } catch (Exception ex) { Console.WriteLine("Error:
" + ex.Message); } } }

// 3. Windows Forms ListBox Program using System; using System.Windows.Forms;

public class StudentForm : Form { private ListBox listBox; private TextBox txtStudent; private Button
btnAdd, btnDelete, btnCount, btnSort, btnClear;

public StudentForm()

listBox = new ListBox { Items = { "John", "Tindo", "Chipo", "Munya", "Melody" } };

txtStudent = new TextBox();

btnAdd = new Button { Text = "Add Student" };

btnDelete = new Button { Text = "Delete Student" };

btnCount = new Button { Text = "Count Items" };

btnSort = new Button { Text = "Sort Desc" };

btnClear = new Button { Text = "Clear List" };


btnAdd.Click += (s, e) => listBox.Items.Add(txtStudent.Text);

btnDelete.Click += (s, e) => listBox.Items.Remove(listBox.SelectedItem);

btnCount.Click += (s, e) => MessageBox.Show("Total Students: " + listBox.Items.Count);

btnSort.Click += (s, e) => {

var list = listBox.Items.Cast<string>().OrderByDescending(i => i).ToList();

listBox.Items.Clear();

list.ForEach(i => listBox.Items.Add(i));

};

btnClear.Click += (s, e) => listBox.Items.Clear();

You might also like