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

Lecture 9 - windowsform application

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 9 - windowsform application

Uploaded by

anjalee himalki
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Windows Form Application III

Object oriented with visual programming


M. Y Abdur Rahman
Multi Threading

• In multi threading individual program run multiple computations at the


same time.
• Multithreading is used to dived lengthy tasks into different segments.
• Each computational unit is called as thread.
• Advantage of multithreading perform more than one task at a time.
When to use Threads
• Communicating over a network, web server and to a database.
• Performing operation that take a large amount of time.
• Distinguish tasks of varying priority.
• High priority threads manages time critical tasks, low priority threads
perform other tasks.
• Allow the user interface to remain responsive while allocating time to
background tasks.
Thread life time
• When thread object is created it will not run automatically.
• The start() method begins the execution of the thread.
• The stop() method is used to stop the running thread .
• The sleep() method can be used to put thread to put thread to sleep for a
given millisecond period,
• In C# the System.Threading class is used for working with threads. It allows
creating and accessing individual threads in a multithreaded application.
Sample program for thread
• private void Form1_Load(object sender, EventArgs e)
• {
• Control.CheckForIllegalCrossThreadCalls = false;
• }

• public void beta()


• {
• for(int i=0;i<=10;i++)
• {
• listBox1.Items.Add(i);
• }
• }
• public void alpha()
• {
• for (int i = 0; i <= 20; i++)
• {
• listBox2.Items.Add(i);
• }
• }

• private void btn_show_Click(object sender, EventArgs e)


• {
• Thread thread1 = new Thread(new ThreadStart(beta));
• Thread thread2 = new Thread(new ThreadStart(alpha));

• thread1.Start();
• thread2.Start();
• }
Sample program for thread
• private void Form1_Load(object sender, EventArgs e)
• {
• Control.CheckForIllegalCrossThreadCalls = false;
• }
• public void beta()
• {
• for (int i = 0; i <= 10000; i++)
• {
• label1.Text = i.ToString();
• }
• }
• public void alpha()
• {
• for (int i = 10000; i>=1;i--)
• {
• label2.Text = i.ToString();
• }
• }
• private void btn_st_Click(object sender, EventArgs e)
• {
• Thread thread1 = new Thread(new ThreadStart(beta));
• Thread thread2 = new Thread(new ThreadStart(alpha));

• thread1.Start();
• thread2.Start();
• }
Working with multiple forms
• private void btn_show_Click(object sender, EventArgs e)
• {
• Form2 form = new Form2();
• form.Show();
• }

• private void btn_hide_Click(object sender, EventArgs e)


• {
• this.Hide();
• Form2 form2 = new Form2();
• form2.Show();
• }

• private void btn_formdi_Click(object sender, EventArgs e)


• {
• Form2 f2 = new Form2();
• f2.ShowDialog();
• }
• private void btn_close_Click(object sender, EventArgs e)
• {
• this.Close();
• }
• public string passname(string name)
• {
• return "Welcome to the Form 2 Mr. "+ name;
• }
In Form 2 (when the Form 2 is load)

• private void Form2_Load(object sender, EventArgs e)


• {
• Form1 form1 = new Form1();
• textBox1.Text = form1.passname(" Ravindu");
• }
Multiple Document Interface(MDI)

• MDI can display multiple child windows inside them.


• Any windows can become an MDI parent, if you set the IsmdiContainer
property to True.
• IsMdiContainer = true;
• private void mainMenuToolStripMenuItem_Click(object sender, EventArgs
e)
• {
• Form2 form2 = new Form2();
• form2.Show();
• form2.MdiParent = this;
• }
ADO.Net (Database)

• Different applications have different requirements for database access


C#.Net uses ADO.Net (Active X Data objects) as it’s data access and
manipulation protocol which also enables us to work with data on the
internet.
• When an application interacts with the database, the connection is opened
to serve the request of the application and is closed as soon as the request is
completed. Likewise if a database is updated, the connection is opened long
enough to complete the update operation and is closed.
ADO.Net (Database) cont..,
• By keeping connections open for only a minimum period of ADO.Net
conserves system resources and provides maximum security for databases
and also has less impact on system performance.
• Also, ADO.Net when interacting with the database uses XML and converts
all the data into XML format for database related operations making them
more efficient.
Benefits of ADO.Net
• Interoperability
• Can use XML to read and write and more data.
• Scalability through the disconnected dataset.
• Connections are not maintained for long periods.
• Database locking does not occur.
• Maintainability
• Separation of data logic and user interface.
• Programmability
• ADO.NET data components in Visual Studio encapsulate data access functionality in
various ways that help you program more quickly and with fewer mistakes.
ADO.NET Data Architecture
Provider Objects Common Object

.NET Framework Data Provider Data Set

Data Table Collection

Data Adapter Data Table


Connection
Transaction Select Command Data Row Collection

Insert Command Data Column Collection


Command
Parameters Constraint Collection
Update Command

Data Reader Delete Command Date Relation Collection


Data Provider
• A data provider is used for connecting to a database, executing commands
and retrieving data, storing it in a dataset reading the retrieved data and
updating the database.
• ADO.Net comes with data providers including
• OleDb
• SqlClient
• Both OleDb and SqlClient has it’s own set of classes but they have the same
concepts.
ADO.Net Data providers Hierarchy

System Data
Common contains
classes shared by both
SQLClient .OleDb

SQL command OleDbCommand


SQL Connection OleDbConnection
SQL Data Reader OleDbDataReader
SQL Data Adapter OleDbDataAdapter
• The Data provider in ADO .NET consists of the four objects.
• Connection: This component is used to set up a connection with a data
source. Contains all of the information required to open a connection to the
database.
• Command: A command is a SQL statement or a stored procedure used to
retrieve, insert, delete or modify data in a data source.
• Execute Non Query: Executes commands that have no return values such as Insert,
Update or Delete.
• Execute scalar: Returns a single value from a database query.
• Execute Reader: Returns a results set by way of a Data Reader object.
• Data Reader:
Data reader is used to retrieve data from a data source in a read-only and
forward-only mode.
The Data Reader is reader is returned as the result of the command object’s
Executive Reader Method.
• Data Adapter:
Middle man facilitating at communication between the database and a
dataset.
The Data Adapter is used either to fill a Data Table or Data set with data from
the database with it’s fill method.
Data set
• Data Set is an in memory representation of data. It is a disconnected,
cached set of records that are retrieved from a database.
• The dataset works as a virtual database containing table, rows, and
columns.
• The data is persisted in memory and the data in it can be manipulated and
updated independent of the database.
• When the use of this Data Set is finished, changes can be made back to the
central database for updating.
SQL Database with C#
• Library: Using System.Data.SqlClient.
• Classes:
• SqlConnection()
• SqlCommand()
• SqlDataAdapter()
• SqlDataReader()
Sample Program For Insert Data
using System.Data.SqlClient;
• namespace WindowsFormsDB1
• {
• public partial class Form1 : Form
• {
• public SqlConnection con;
• public SqlCommand cmd;
• string clientid;
• string name;
• string address;
• string tp;

• public Form1()
• {
• InitializeComponent();
• }
• private void Form1_Load(object sender, EventArgs e)
• {
• con = new SqlConnection();
• con.ConnectionString = "Data Source=DESKTOP-QC7BTE6;Initial
Catalog=esoft2022;Integrated Security=True";

• }
• private void btn_insert_Click(object sender, EventArgs e)
• {

• clientid = txt_cid.Text;
• name = txt_cname.Text;
• address = txt_add.Text;
• tp = txt_tel.Text;
• con.Open();
• cmd = new SqlCommand("insert into client values (@client_id, @client_name,
@client_Address, @client_tel)", con);
• cmd.Parameters.AddWithValue("client_id", clientid);
• cmd.Parameters.AddWithValue("client_name", name);
• cmd.Parameters.AddWithValue("client_Address", address);
• cmd.Parameters.AddWithValue("client_tel", tp);

• int line=cmd.ExecuteNonQuery();
• cmd.Dispose();
• con.Close();

• if(line==1)
• {
• MessageBox.Show("Success");
• }
• else
• {
• MessageBox.Show("Failed");
• }

• }

You might also like