Windows Form Application IV
Object oriented with visual programming
M. Y Abdur Rahman
Update and Delete data
• using [Link];
• 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;
• [Link]();
• cmd = new SqlCommand("update client set client_tel = @client_tel where
client_id = @client_id", con);
• [Link]("client_id", clientid);
• [Link]("client_tel", tp);
• int line = [Link]();
• [Link]();
• [Link]();
• if (line == 1)
• {
• [Link]("Updated Successfully");
• }
• else
• {
• [Link]("Failed");
• }
• }
• private void btn_delete_Click(object sender, EventArgs e)
• {
• clientid = txt_cid.Text;
• [Link]();
• cmd = new SqlCommand("delete from client where client_id = @client_id", con);
• [Link]("client_id", clientid);
• int line = [Link]();
• [Link]();
• [Link]();
• if (line == 1)
• {
• [Link]("Deleted Successfully");
• }
• else
• {
• [Link]("Failed");
• }
• }
Sample Program for Database
• using [Link];
• 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)
• {
• [Link]();
• }
• private void Form1_Load(object sender, EventArgs e)
• {
• con = new SqlConnection();
• [Link] = "Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True";
• displayData();
• }
• private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
• {
• stage = [Link];
• txt_age.Text = [Link]();
• }
• private void btn_save_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_stname.Text;
• dob = [Link];
• stage = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tele.Text);
• [Link]();
• cmd = new SqlCommand("insert into student values(@st_id, @st_name,@st_dob,
@st_age, @st_tel)", con);
• [Link]("st_id", stid);
• [Link]("st_name", stname);
• [Link]("st_dob", dob);
• [Link]("st_age", stage);
• [Link]("st_tel", tp);
• int line = [Link]();
• [Link]();
• [Link]();
• if (line == 1)
• {
• [Link]("Data inserted successfully", "Information",
[Link], [Link]);
• }
• else
• {
• [Link]("Data not inserted", "Error", [Link],
[Link]);
• }
• }
• catch (SqlException ex)
• {
• [Link]("Database Error", "Error", [Link],
[Link]);
• }
• catch (Exception ex)
• {
• [Link]([Link](), "Error", [Link],
[Link]);
• }
• }
• 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 = [Link];
• stage = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tele.Text);
• DialogResult dr = [Link]("Do you want to update the selected
row?", "Information", [Link], [Link]);
• if ([Link]() == "Yes")
• {
•
• [Link]();
• 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);
• [Link]("@st_name", stname);
• [Link]("@st_dob", dob);
• [Link]("@st_age", stage);
• [Link]("@st_tel", tp);
• [Link]("@st_id", stid);
• int line = [Link]();
• [Link]();
• [Link]();
• if (line == 1)
• {
• [Link]("Data updated successfully", "Information", [Link],
[Link]);
• }
• else
• {
• [Link]("Data not updated ", "Error", [Link], [Link]);
• }
• }
• }
• catch (SqlException ex)
• {
• [Link]("Database Error", "Error", [Link], [Link]);
• }
• catch (Exception ex)
• {
• [Link]([Link](), "Error", [Link], [Link]);
• }
• }
• private void btn_del_Click(object sender, EventArgs e)
• {
• try
• {
• DialogResult dr = [Link]("Do you want to delete the selected
row?", "Information", [Link], [Link]);
• if ([Link]() == "Yes")
• {
• stid = [Link][0].[Link]();
• [Link]();
• cmd = new SqlCommand("delete from student where st_id = @stid",
con);
• [Link]("@stid", stid);
•
• int line = [Link]();
• [Link]();
• [Link]();
• if (line == 1)
• {
• [Link]("Data Deleted successfully", "Information", [Link],
[Link]);
• }
• else
• {
• [Link]("Data not deleted", "Error", [Link], [Link]);
• }
• }
• }
• catch (SqlException ex)
• {
• [Link]([Link](), "Error", [Link], [Link]);
• }
• catch (Exception ex)
• {
• [Link]([Link](), "Error", [Link], [Link]);
• }
• }
• private void displayData()
• {
• try
• {
• SqlDataAdapter da = new SqlDataAdapter("select * from
student", con);
• DataTable dt = new DataTable();
• [Link](dt);
• [Link] = dt;
• }
• catch (SqlException ex)
• {
• [Link]("Database Error", "Error",
[Link], [Link]);
• }
• }
• private void dataGridView1_CellContentClick_1(object sender,
DataGridViewCellEventArgs e)
• {
• txt_stid.Text = [Link]["st_id"].[Link]();
• txt_stname.Text =
[Link]["st_name"].[Link]();
•
• [Link] =
[Link]["st_dob"].[Link]();
• txt_age.Text = [Link]["st_age"].[Link]();
• txt_tele.Text =
[Link]["st_tel"].[Link]();
• }
• 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)
• {
• [Link]([Link],
• convert.ToInt32([Link]), Convert.ToInt32
• ([Link]));
• [Link]();
• }
How to create crystal report
Steps to follow arranging the code:
• [Link]
Steps to follow including crystal report :
• [Link]
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
[Link]
• using [Link];
• using [Link];
• namespace WindowsFormsDBad
• {
•
• internal class Database
• {
• private SqlConnection con;
• private SqlCommand cmd;
• public Database()
• {
• con = new SqlConnection();
• [Link] = "Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True";
• }
• public void opencon()
• {
• [Link]();
• }
• public void closecon()
• {
• [Link]();
• }
• public int save_del_update(string query)
• {
• int rows;
• try
• {
• opencon();
• }
• catch(SqlException es)
• {
•
• [Link]([Link]());
• }
• cmd = new SqlCommand(query, con);
• rows = [Link]();
• [Link]();
• closecon();
• return rows;
• }
• public DataTable GetData(string query)
• {
• try
• {
• opencon();
• }
• catch (SqlException es)
• {
•
• [Link]([Link]());
• }
• SqlDataAdapter adapter = new SqlDataAdapter(query,con);
• DataTable dt = new DataTable();
• [Link](dt);
• closecon();
• return dt;
• }
• using [Link];
[Link]
• 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 = [Link];
• txt_age.Text = [Link]();
• }
• private void btn_save_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_name.Text;
• dob = [Link];
• 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)
• {
• [Link]("Data inserted
successfully","Information",[Link],[Link]);
• }
• else {
• [Link]("Data not inserted ", "Error",
[Link], [Link]);
• }
• }
• catch(Exception)
• {
• [Link]("Please check the fields ", "Error",
[Link], [Link]);
• }
• }
• private void btn_upd_Click(object sender, EventArgs e)
• {
• try
• {
• stid = txt_stid.Text;
• stname = txt_name.Text;
• dob = [Link];
• age = Convert.ToInt32(txt_age.Text);
• tp = Convert.ToInt32(txt_tel.Text);
• DialogResult dr = [Link]("Do you want to update the select rows?", "Information",
[Link], [Link]);
• if ([Link]() == "Yes")
• {
• stid = [Link][0].[Link]();
• 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)
• {
• [Link]("Data updated successfully", "Information", [Link],
[Link]);
• }
• else
• {
• [Link]("Data not updated ", "Error", [Link], [Link]);
• }
• }
• }
• catch (Exception)
• {
• [Link]("Please check the fields ", "Error", [Link], [Link]);
• }
• }
• private void btn_close_Click(object sender, EventArgs e)
• {
• [Link]();
• }
• 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 = [Link]["st_id"].[Link]();
• txt_name.Text = [Link]["st_name"].[Link]();
• [Link] = [Link]["st_dob"].[Link]();
• txt_age.Text = [Link]["st_age"].[Link]();
• txt_tel.Text = [Link]["st_tel"].[Link]();
• }
• private void btn_display_Click(object sender, EventArgs e){
• [Link] = [Link]("select * from student1");
• }
• private void btn_del_Click(object sender, EventArgs e)
• {
• try
• {
• DialogResult dr = [Link]("Do you want to Delete the select rows?",
"Information", [Link], [Link]);
• if ([Link]() == "Yes")
• {
• stid = [Link][0].[Link]();
• string query = "delete from student1 where st_id = '" + stid + "'";
• int line = db.save_del_update(query);
• if (line == 1)
• {
• [Link]("Data delete successfully", "Information",
[Link], [Link]);
• }
• else
• {
• [Link]("Data not deleted ", "Error", [Link], [Link]);
• }
• }
• }
• catch (Exception)
• {
• [Link]("Please check the fields ", "Error", [Link], [Link]);
• }
• }
• private void btn_search_Click(object sender, EventArgs e)
• {
• try
• {
• if(txt_search.[Link] > 0)
• {
• if(rbn_id.Checked == true)
• {
• [Link] = [Link]("select * from student1 where st_id = '" +
txt_search.Text+ "'");
• }
•
• }
•
• if (txt_search1.[Link] > 0)
• {
•
• if (rbn_name.Checked == true)
• {
• [Link] = [Link]("select * from student1
where st_name = '" + txt_search1.Text + "'");
• }
• }
• else
• {
• [Link] = [Link]("select * from student1");
• }
• }
• catch (Exception)
• {
• [Link]("Please check the fields ", "Error",
[Link], [Link]);
• }
• }