Lecture 10 - Windowsform Application
Lecture 10 - Windowsform Application
• namespace WindowsFormsDB1up
• {
• public partial class Form1 : Form
• {
• public Form1()
• {
• InitializeComponent();
• }
• con.Open();
• cmd = new SqlCommand("update client set client_tel = @client_tel where
client_id = @client_id", con);
• cmd.Parameters.AddWithValue("client_id", clientid);
• cmd.Parameters.AddWithValue("client_tel", tp);
• con.Open();
• cmd = new SqlCommand("delete from client where client_id = @client_id", con);
• cmd.Parameters.AddWithValue("client_id", clientid);
• if (line == 1)
• {
• MessageBox.Show("Deleted Successfully");
• }
• else
• {
• MessageBox.Show("Failed");
• }
• }
Sample Program for Database
• using System.Data.SqlClient;
• namespace WindowsFormsDBFinal
• {
• public partial class Form1 : Form
• {
• public Form1()
• {
• InitializeComponent();
• }
• public SqlConnection con;
• public SqlCommand cmd;
• string stid;
• string stname;
• DateTime dob;
• int stage;
• int tp;
• private void btn_close_Click(object sender, EventArgs e)
• {
• this.Close();
• }
• }
• con.Open();
• cmd = new SqlCommand("insert into student values(@st_id, @st_name,@st_dob,
@st_age, @st_tel)", con);
• cmd.Parameters.AddWithValue("st_id", stid);
• cmd.Parameters.AddWithValue("st_name", stname);
• cmd.Parameters.AddWithValue("st_dob", dob);
• cmd.Parameters.AddWithValue("st_age", stage);
• cmd.Parameters.AddWithValue("st_tel", tp);
• }
• private void btn_dis_Click(object sender, EventArgs e)
• {
• displayData();
•
•
• }
• cmd.Parameters.AddWithValue("@st_name", stname);
• cmd.Parameters.AddWithValue("@st_dob", dob);
• cmd.Parameters.AddWithValue("@st_age", stage);
• cmd.Parameters.AddWithValue("@st_tel", tp);
• cmd.Parameters.AddWithValue("@st_id", stid);
• con.Open();
• cmd = new SqlCommand("delete from student where st_id = @stid",
con);
• cmd.Parameters.AddWithValue("@stid", stid);
•
• int line = cmd.ExecuteNonQuery();
• cmd.Dispose();
• con.Close();
• if (line == 1)
• {
• MessageBox.Show("Data Deleted successfully", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
• }
• else
• {
• }
• catch (SqlException ex)
• {
• MessageBox.Show("Database Error", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void dataGridView1_CellContentClick_1(object sender,
DataGridViewCellEventArgs e)
• {
• txt_stid.Text = dataGridView1.CurrentRow.Cells["st_id"].Value.ToString();
• txt_stname.Text =
dataGridView1.CurrentRow.Cells["st_name"].Value.ToString();
•
• dateTimePicker1.Text =
dataGridView1.CurrentRow.Cells["st_dob"].Value.ToString();
• txt_age.Text = dataGridView1.CurrentRow.Cells["st_age"].Value.ToString();
• txt_tele.Text =
dataGridView1.CurrentRow.Cells["st_tel"].Value.ToString();
• }
• Then select fields from the table which you want to show in the report.
• Then on the report viewer choose the report name which you created now.
• Run the application and see the report.
Report with passing parameters
• Right click on the windows application which you want to create the set up
file and go to properties.
• In the properties view choose publish Tab from the right hand side
• Then give the location to create the setup file and if it is needed give
prerequisites.
• Go to the option tab and give the short cut name and other details.
Advance Program using a class object
Database.cs
• using System.Data.SqlClient;
• using System.Data;
• namespace WindowsFormsDBad
• {
•
• internal class Database
• {
• private SqlConnection con;
• private SqlCommand cmd;
• public Database()
• {
• con = new SqlConnection();
• con.ConnectionString = "Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True";
• }
• public void opencon()
• {
• con.Open();
• }
• public void closecon()
• {
• con.Close();
• }
• public int save_del_update(string query)
• {
• int rows;
• try
• {
• opencon();
• }
• catch(SqlException es)
• {
•
• Console.WriteLine(es.ToString());
• }
• cmd = new SqlCommand(query, con);
• rows = cmd.ExecuteNonQuery();
• cmd.Dispose();
• closecon();
• return rows;
• }
• public DataTable GetData(string query)
• {
• try
• {
• opencon();
• }
• catch (SqlException es)
• {
•
• Console.WriteLine(es.ToString());
• }
• SqlDataAdapter adapter = new SqlDataAdapter(query,con);
• DataTable dt = new DataTable();
• adapter.Fill(dt);
• closecon();
• return dt;
• }
• using System.Data.SqlClient;
Form1.cs
• namespace WindowsFormsDBad
• {
• public partial class Form1 : Form
• {
•
• Database db = new Database();
• string stid;
• string stname;
• DateTime dob;
• int age;
• int tp;
• public Form1()
• {
• InitializeComponent();
• }
• private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
• {
• age = DateTime.Today.Year-dateTimePicker1.Value.Year;
• txt_age.Text = age.ToString();
• }
• private void btn_save_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_name.Text;
• dob = dateTimePicker1.Value;
• age = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tel.Text);
• string query = "insert into student1
values('"+stid+"','"+stname+"','"+dob+"','"+age+"','"+tp+"')";
• int line = db.save_del_update(query);
• if(line==1)
• {
• MessageBox.Show("Data inserted
successfully","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
• }
• else {
• MessageBox.Show("Data not inserted ", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• catch(Exception)
• {
• MessageBox.Show("Please check the fields ", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void btn_upd_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_name.Text;
• dob = dateTimePicker1.Value;
• age = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tel.Text);
• DialogResult dr = MessageBox.Show("Do you want to update the select rows?", "Information",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
• if (dr.ToString() == "Yes")
• {
• stid = dataGridView1.CurrentRow.Cells[0].Value.ToString();
• dateTimePicker1.Text = dataGridView1.CurrentRow.Cells["st_dob"].Value.ToString();
• txt_age.Text = dataGridView1.CurrentRow.Cells["st_age"].Value.ToString();
• txt_tel.Text = dataGridView1.CurrentRow.Cells["st_tel"].Value.ToString();
• }
• private void btn_display_Click(object sender, EventArgs e){
• dataGridView1.DataSource = db.GetData("select * from student1");
• }
• private void btn_del_Click(object sender, EventArgs e)
• {
• try
• {
• DialogResult dr = MessageBox.Show("Do you want to Delete the select rows?",
"Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
• if (dr.ToString() == "Yes")
• {
• stid = dataGridView1.CurrentRow.Cells[0].Value.ToString();
• string query = "delete from student1 where st_id = '" + stid + "'";
• int line = db.save_del_update(query);
• if (line == 1)
• {
• MessageBox.Show("Data delete successfully", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
• }
• else
• {
• MessageBox.Show("Data not deleted ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• }
• catch (Exception)
• {
• MessageBox.Show("Please check the fields ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void btn_search_Click(object sender, EventArgs e)
• {
• try
• {
• if(txt_search.Text.Length > 0)
• {
• if(rbn_id.Checked == true)
• {
• dataGridView1.DataSource = db.GetData("select * from student1 where st_id = '" +
txt_search.Text+ "'");
• }
•
• }
•
• if (txt_search1.Text.Length > 0)
• {
•
• if (rbn_name.Checked == true)
• {
• dataGridView1.DataSource = db.GetData("select * from student1
where st_name = '" + txt_search1.Text + "'");
• }
• }
• else
• {
• dataGridView1.DataSource = db.GetData("select * from student1");
• }
• }
• catch (Exception)
• {
• MessageBox.Show("Please check the fields ", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }