Kiritish (Form1.cs) - Kompyuter injiniringi” fakulteti,,Kompyuter injiniring at-servis” yo'nalishi 106-21 guruh...

Kiritish (Form1.cs)
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 System.Data.SqlClient;
namespace WindowsFormsApplication7
{
public partial class PassengerMaster : Form
{
public PassengerMaster()
{
InitializeComponent();
populate();
}
private SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\User\Documents\RailwaysDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
private void populate()
{
Con.Open();
string query = "select * from PASSENGERTBL";
SqlDataAdapter sda = new SqlDataAdapter(query, Con);
var ds = new DataSet();
sda.Fill(ds);
PassengerDGV.DataSource = ds.Tables[0];
Con.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string Gender = "";
if (PnameTb.Text == "" || PPhoneTb.Text == ""||PaddressTb.Text == "")
{
MessageBox.Show("Missing information");
}
else
{
if (MaleRd.Checked == true)
{
Gender = "Male";
}
else if (FemaleRd.Checked == true)
{
Gender = "Female";
}
try
{
Con.Open();
string Query = "insert into PASSENGERTBL values('" + PnameTb.Text + "'," + PaddressTb.Text + ",'" + Gender + "','"+NatCb.SelectedItem.ToString()+"','"+PPhoneTb.Text+"')";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Passenger Added Successfully");
Con.Close();
populate();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
int key = 0;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = PassengerDGV.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = PassengerDGV.SelectedRows[0].Cells[2].Value.ToString();
NatCb.SelectedItem = PassengerDGV.SelectedRows[0].Cells[4].Value.ToString();
textBox3.Text = PassengerDGV.SelectedRows[0].Cells[5].Value.ToString();
if (PnameTb.Text == "")
{
key = 0;
}
else
{
key = Convert.ToInt32(PassengerDGV.SelectedRows[0].Cells[0].Value.ToString());
}
}
private void Reset()
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
MaleRd.Checked = false;
FemaleRd.Checked = false;
NatCb.SelectedIndex = -1;
key = 0;
}
private void button4_Click(object sender, EventArgs e)
{
Reset();
}
private void button3_Click(object sender, EventArgs e)
{
if (key == 0)
{
MessageBox.Show("Select The Passenger To Be Deleted");
}
else
{
try
{
Con.Open();
string Query = "Delete from PASSENGERTBL where PId=" + key + "";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Passenger Deleted Successfully");
Con.Close();
populate();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string Gender = "";
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("Missing information");
}
else
{
if (MaleRd.Checked == true)
{
Gender = "Male";
}
else if (FemaleRd.Checked == true)
{
Gender = "Female";
}
try
{
Con.Open();
string Query = "update PASSENGERTBL set Pname='" + textBox1.Text + "',PAdd="+textBox2.Text + ", PGender='" + Gender + "',PNat='"+NatCb.SelectedItem.ToString()+"',PPhone ='"+textBox3.Text+"' where PId=" + key + ";";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Passenger Updated Successfully");
Con.Close();
populate();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void MaleRd_CheckedChanged(object sender, EventArgs e)
{
}
private void label7_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button5_Click(object sender, EventArgs e)
{
Form2 Main = new Form2();
Main.Show();
this.Hide();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
}
A
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;
namespace WindowsFormsApplication7
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void pictureBox5_Click(object sender, EventArgs e)
{
CancellationMaster Cancel = new CancellationMaster();
Cancel.Show();
this.Hide();
}
private void label6_Click(object sender, EventArgs e)
{
CancellationMaster Cancel = new CancellationMaster();
Cancel.Show();
this.Hide();
}
private void pictureBox4_Click(object sender, EventArgs e)
{
ReservationMaster Res = new ReservationMaster();
Res.Show();
this.Hide();
}
private void label4_Click(object sender, EventArgs e)
{
ReservationMaster Res = new ReservationMaster();
Res.Show();
this.Hide();
}
private void pictureBox3_Click(object sender, EventArgs e)
{
TravelMaster Tr = new TravelMaster();
Tr.Show();
this.Hide();
}
private void label5_Click(object sender, EventArgs e)
{
TravelMaster Tr = new TravelMaster();
Tr.Show();
this.Hide();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
PassengerMaster Ps = new PassengerMaster();
Ps.Show();
this.Hide();
}
private void label3_Click(object sender, EventArgs e)
{
PassengerMaster Ps = new PassengerMaster();
Ps.Show();
this.Hide();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
TrainMaster TM = new TrainMaster();
TM.Show();
this.Hide();
}
private void label2_Click(object sender, EventArgs e)
{
TrainMaster TM = new TrainMaster();
TM.Show();
this.Hide();
}
private void label11_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label10_Click(object sender, EventArgs e)
{
Login log = new Login();
log.Show();
this.Hide();
}
}
}
B
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 System.Data.SqlClient;
namespace WindowsFormsApplication7
{
public partial class TravelMaster : Form
{
public TravelMaster()
{
InitializeComponent();
populate();
FillTCode();
}
private SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\User\Documents\RailwaysDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
private void populate()
{
Con.Open();
string query = "select * from TRAVELTBL";
SqlDataAdapter sda = new SqlDataAdapter(query, Con);
var ds = new DataSet();
sda.Fill(ds);
TravelDGV.DataSource = ds.Tables[0];
Con.Close();
}
private void FillTCode()
{
string TrStatus="Busy";
Con.Open();
SqlCommand cmd = new SqlCommand("select TrainId from TRAINTBL where TrainStatus='"+ TrStatus+"'", Con);
SqlDataReader rdr;
rdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("TrainId", typeof(int));
dt.Load(rdr);
TCode.ValueMember = "TrainId";
TCode.DataSource = dt;
Con.Close();
}
private void label1_Click_1(object sender, EventArgs e)
{
Application.Exit();
}
private void ChangeStatus()
{
string TrStatus = "Busy";
try
{
Con.Open();
string Query = "update TRAINTBL set TrainStatus='" + TrStatus + "' where TrainId=" + key + ";";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Train Updated Successfully");
Con.Close();
populate();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
private void TravelMaster_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
Form2 Main=new Form2();
Main.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
if (TCostTb.Text == "" || TCode.SelectedIndex == -1 || SrcCb.SelectedIndex == -1 || DestCb.SelectedIndex == -1)
{
MessageBox.Show("Missing information");
}
else
{
try
{
Con.Open();
string Query = "insert into TRAVELTBL values('" + TravDate.Value + "','" + TCode.SelectedValue.ToString() + "','" + SrcCb.SelectedItem.ToString() + "','" + DestCb.SelectedItem.ToString() + "'," + TCostTb.Text + ")";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Travel Added Successfully");
Con.Close();
ChangeStatus();
populate();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void Reset()
{
SrcCb.SelectedIndex = -1;
DestCb.SelectedIndex = -1;
//TCode.SelectedIndex = -1;
TCostTb.Text = "";
}
private void button4_Click(object sender, EventArgs e)
{
Reset();
}
private void button2_Click(object sender, EventArgs e)
{
if (SrcCb.SelectedIndex == -1 || DestCb.SelectedIndex == -1 || TCostTb.Text == "")
{
MessageBox.Show("Missing information");
}
else
{
try
{
Con.Open();
string Query = "update TRAVELTBL set TravDate='" + TravDate.Text + "',Train=" + TCode.SelectedValue.ToString() + ", Src='" + SrcCb.SelectedItem.ToString() + "',Dest='" + DestCb.SelectedItem.ToString() + "',Cost =" + TCostTb.Text + "' where TravCode=" + key + ";";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Travel Updated Successfully");
Con.Close();
populate();
Reset();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
int key;
private void TravelDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
TravDate.Text = TravelDGV.SelectedRows[0].Cells[1].Value.ToString();
TCode.SelectedValue = TravelDGV.SelectedRows[0].Cells[2].Value.ToString();
SrcCb.SelectedItem = TravelDGV.SelectedRows[0].Cells[3].Value.ToString();
DestCb.Text = TravelDGV.SelectedRows[0].Cells[4].Value.ToString();
TCostTb.Text = TravelDGV.SelectedRows[0].Cells[5].Value.ToString();
if (TCode.SelectedIndex==-1)
{
key = 0;
//TCostTb.Text = "";
// SrcCb.SelectedIndex = -1;
// DestCb.SelectedIndex = -1;
}
else
{
key = Convert.ToInt32(TravelDGV.SelectedRows[0].Cells[0].Value.ToString());
}
}
private void TravDate_ValueChanged(object sender, EventArgs e)
{
}
}
}
C
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 System.Data.SqlClient;
namespace WindowsFormsApplication7
{
public partial class TrainMaster : Form
{
public TrainMaster()
{
InitializeComponent();
populate();
}
private SqlConnection Con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\User\Documents\RailwaysDb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
private void populate()
{
Con.Close();
Con.Open();
string query = "select * from TRAINTBL";
SqlDataAdapter sda = new SqlDataAdapter(query, Con);
var ds = new DataSet();
sda.Fill(ds);
TrainDGV.DataSource = ds.Tables[0];
Con.Close();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void reset()
{
textBox1.Text = "";
textBox2.Text = "";
BusyRd.Checked = false;
FreeRd.Checked = false;
key = 0;
}
private void button4_Click(object sender, EventArgs e)
{
reset();
}
private void button1_Click(object sender, EventArgs e)
{
string TrStatus = "";
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Missing information");
}
else
{
if (BusyRd.Checked == true)
{
TrStatus = "Busy";
}
else if (FreeRd.Checked == true)
{
TrStatus = "Available";
}
try
{
Con.Open();
string Query = "insert into TRAINTBL values('" + textBox1.Text + "','" + textBox2.Text + "','" + TrStatus + "')";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Train Added Successfully");
Con.Close();
populate();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label6_Click(object sender, EventArgs e)
{
Application.Exit();
}
int key = 0;
private void TrainDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
if (key == 0)
{
MessageBox.Show("Select The Train To Be Deleted");
}
else
{
try
{
Con.Open();
string Query = "Delete from TRAINTBL where TrainId="+key+"";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Train Deleted Successfully");
Con.Close();
populate();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string TrStatus = "";
if (textBox1.Text == "" || textBox2.Text == "")
{
MessageBox.Show("Missing information");
}
else
{
if (BusyRd.Checked == true)
{
TrStatus = "Busy";
}
else if (FreeRd.Checked == true)
{
TrStatus = "Available";
}
try
{
Con.Open();
string Query = "update TRAINTBL set TrainName='" + textBox1.Text + "',TrainCap='" + textBox2.Text + "', TrainStatus='" + TrStatus + "' where TrainId=" + key + ";";
SqlCommand cmd = new SqlCommand(Query, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Train Updated Successfully");
Con.Close();
populate();
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void TrainDGV_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = TrainDGV.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = TrainDGV.SelectedRows[0].Cells[2].Value.ToString();
if (TrNameTb.Text == "")
{
key = 0;
}
else
{
key = Convert.ToInt32(TrainDGV.SelectedRows[0].Cells[0].Value.ToString());
}
}
private void TrainMaster_Load(object sender, EventArgs e)
{
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button5_Click(object sender, EventArgs e)
{
Form2 Main = new Form2();
Main.Show();
this.Hide();
}
}
}
D
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 System.Data.SqlClient;
namespace WindowsFormsApplication7
{
public partial class ReservationMaster : Form
{
private void PIdCb_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PIdCb_SelectionChangeCommitted(object sender, EventArgs e)
{
GetPName();
}
private void TravelCb_SelectionChangeCommitted(object sender, EventArgs e)
{
GetTravel();
}
private void button4_Click(object sender, EventArgs e)
{
Form2 Main = new Form2();
Main.Show();
this.Hide();
}
}
}
Xulosa
Kurs ishida ayrim ko’p qo’llanadigan boshqaruv elementlari va ularning xususiyatlari ko’rib o’tildi. Ulardan TextBox elementlari matnli ma’lumotlar bilan ishlasa, comboBox elementi esa oldindan keltirilgan ma’lumotlardan tanlash imkonini beradi. DataGirdView esa ma’lumotlar bazasida joylashgan ma’lumotlarni C# dasturlash tilida namoyon qilishda qo’l keladi. Kurs ishini ishlash davomida ko’proq ma’lumotlar bazasi bilan C# ni bog’lash vazifalari berildi. Tuzilgan ilovaning ham ko’p qismi ma’lumotlar bazasi ya’ni Microsoft SQL Server ni Windows ilovaga bog’lash bo’ldi. Bog’lashda boshida ko’plab xatolarga duch keldim va bu xatolarni qanday qilib bartaraf qilish mumkin ekanligini o’qituvchilarimni ko’magi bilan bartaraf qildim
|