0% found this document useful (0 votes)
4 views

Lecture 10 - Windowsform Application

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 10 - Windowsform Application

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Windows Form Application IV

Object oriented with visual programming


M. Y Abdur Rahman
Update and Delete data
• using System.Data.SqlClient;

• namespace WindowsFormsDB1up
• {
• public partial class Form1 : Form
• {
• public Form1()
• {
• InitializeComponent();
• }

• private void Form1_Load(object sender, EventArgs e)


• {
• con = new SqlConnection("Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True");
• }
• public SqlConnection con;
• public SqlCommand cmd;
• string clientid;
• string tp;
• private void btn_update_Click(object sender, EventArgs e)
• {
• clientid = txt_cid.Text;
• tp = txt_tel.Text;

• 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);

• int line = cmd.ExecuteNonQuery();


• cmd.Dispose();
• con.Close();
• if (line == 1)
• {
• MessageBox.Show("Updated Successfully");
• }
• else
• {
• MessageBox.Show("Failed");
• }
• }
• private void btn_delete_Click(object sender, EventArgs e)
• {
• clientid = txt_cid.Text;

• con.Open();
• cmd = new SqlCommand("delete from client where client_id = @client_id", con);
• cmd.Parameters.AddWithValue("client_id", clientid);

• int line = cmd.ExecuteNonQuery();


• cmd.Dispose();
• con.Close();

• 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();
• }

• private void Form1_Load(object sender, EventArgs e)


• {
• con = new SqlConnection();
• con.ConnectionString = "Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True";
• displayData();

• }

• private void dateTimePicker1_ValueChanged(object sender, EventArgs e)


• {
• stage = DateTime.Today.Year-dateTimePicker1.Value.Year;
• txt_age.Text = stage.ToString();
• }
• private void btn_save_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_stname.Text;
• dob = dateTimePicker1.Value;
• stage = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tele.Text);

• 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);

• int line = cmd.ExecuteNonQuery();


• cmd.Dispose();
• con.Close();
• 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 (SqlException ex)
• {
• MessageBox.Show("Database Error", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
• }
• catch (Exception ex)
• {
• MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
• }

• }
• private void btn_dis_Click(object sender, EventArgs e)
• {
• displayData();


• }

• private void btn_upd_Click(object sender, EventArgs e)


• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_stname.Text;
• dob = dateTimePicker1.Value;
• stage = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tele.Text);
• DialogResult dr = MessageBox.Show("Do you want to update the selected
row?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
• if (dr.ToString() == "Yes")
• {

• con.Open();
• cmd = new SqlCommand("update student set st_name =
@st_name, st_dob = @st_dob, st_age = @st_age, st_tel = @st_tel where st_id
= @st_id", con);

• 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);

• int line = cmd.ExecuteNonQuery();


• cmd.Dispose();
• con.Close();
• if (line == 1)
• {
• MessageBox.Show("Data updated successfully", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
• }
• else
• {

• MessageBox.Show("Data not updated ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


• }
• }
• }
• catch (SqlException ex)
• {
• MessageBox.Show("Database Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• catch (Exception ex)
• {
• MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void btn_del_Click(object sender, EventArgs e)
• {
• try
• {
• DialogResult dr = MessageBox.Show("Do you want to delete the selected
row?", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
• if (dr.ToString() == "Yes")
• {
• stid = dataGridView1.CurrentRow.Cells[0].Value.ToString();

• 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
• {

• MessageBox.Show("Data not deleted", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);


• }
• }
• }
• catch (SqlException ex)
• {
• MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• catch (Exception ex)
• {
• MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void displayData()
• {
• try
• {
• SqlDataAdapter da = new SqlDataAdapter("select * from
student", con);
• DataTable dt = new DataTable();
• da.Fill(dt);
• dataGridView1.DataSource = dt;

• }
• 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();
• }

• private void btn_clear_Click(object sender, EventArgs e)


• {
• txt_stid.Clear();
• txt_stname.Clear();
• txt_age.Clear();
• txt_tele.Clear();
• }
Windows reporting & creating set up
Reports
• Create a report windows form and add a report viewer using reporting
tool(It is always better t0 create a folder first and add forms and reports to
that folder, then it is very easy to identify reports and other forms
separately).
• Right click on the project(solution) and add a Data set using add new item.
• After opening the Data set File (xsd file) drag and drop your table on to the
data set
• Go to the form where your report inserted and on the report choose ”Design
new report” . Then choose the data set which you create as data source.
Reports cont.,

• 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

• Do the same steps again for create a report and dataset.


• On the dataset go to Table Adapter and edit the SQL query with a where
condition to pass some parameters. As example if you want to see the
students who has AGE between 18 to 25
• SELECT *
• FROM student
• WHERE (age BETWEEN @ a AND @b)
• Then on the form where your report inserted. Put two text boxes and one
button. Now you can pass value to those two textboxes.
• You must edit the auto generated code in the report to pass the parameters.
As an example.

• private void button1_Click(object sender, EventArgs e)


• {
• this.studentTableAdapter.Fill(this.Dataset1.student,
• convert.ToInt32(textBox1.Text), Convert.ToInt32
• (textBox2.Text));
• this.reportViewr1.RefreshReport();
• }
How to create crystal report
Steps to follow arranging the code:
• https://www.youtube.com/watch?v=aIdV8yY6Lc0
Steps to follow including crystal report :
• https://www.tektutorialshub.com/crystal-reports/how-to-create-crystal-
report-using-visual-studio/
Creating the set up File

• 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();

• string query = "update student1 set st_name = '"+stname+"',st_dob = '"+dob+"',st_age =


'"+age+"',st_tel = '"+tp+"' where st_id = '"+stid+"'";
• int line = db.save_del_update(query);
• if (line == 1)
• {
• MessageBox.Show("Data updated successfully", "Information", MessageBoxButtons.OK,
MessageBoxIcon.Information);
• }
• else
• {
• MessageBox.Show("Data not updated ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• }
• catch (Exception)
• {
• MessageBox.Show("Please check the fields ", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
• }
• }
• private void btn_close_Click(object sender, EventArgs e)
• {
• this.Close();
• }
• private void btn_clear_Click(object sender, EventArgs e)
• {
• txt_stid.Clear();
• txt_name.Clear();
• txt_age.Clear();
• txt_tel.Clear();
• }
• private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
• {
• txt_stid.Text = dataGridView1.CurrentRow.Cells["st_id"].Value.ToString();
• txt_name.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_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);
• }
• }

You might also like