0% found this document useful (0 votes)
59 views19 pages

Let's Start Now : National University of Computer and Emerging Sciences

This document provides instructions for creating a GUI desktop application in C# to perform basic CRUD operations on a database. It includes steps to install necessary software, create buttons and user controls in Visual Studio, connect to an SQL Server database, and write code for insertion, viewing, updating, and deleting records. It also provides instructions for connecting to a MySQL database using WAMP server and modifying the code accordingly. The document aims to teach how to build responsive desktop and web applications using C# and PHP.

Uploaded by

Muhammad Fahad
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)
59 views19 pages

Let's Start Now : National University of Computer and Emerging Sciences

This document provides instructions for creating a GUI desktop application in C# to perform basic CRUD operations on a database. It includes steps to install necessary software, create buttons and user controls in Visual Studio, connect to an SQL Server database, and write code for insertion, viewing, updating, and deleting records. It also provides instructions for connecting to a MySQL database using WAMP server and modifying the code accordingly. The document aims to teach how to build responsive desktop and web applications using C# and PHP.

Uploaded by

Muhammad Fahad
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/ 19

National University of Computer and Emerging Sciences

Dear All!
My self Khizar Sultan and its really honor for me to be a Assistant of Ma’am Aliza
Saeed. In this manual, I will try my best to teach you the GUI phase with C#(Desktop
Applications) and with Php(Responsive Web Applications)

Before starting this manual, please make sure you have installed the following
softwares:
1. Visual Studio (version >= 13)
https://visualstudio.microsoft.com/downloads/
2. SQL Server
https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-
management-studio-ssms?view=sql-server-2017
3. Wamp Server
https://drive.google.com/open?id=1R4FZDB1AAzhVWbxqbTyudVXBiTtXLlcj
4. Wamp_server Connection File
https://drive.google.com/open?id=1aj_CiA3ylvwSbZXsnB8CZjJ8lvQ8Pp-N
5. MySql_Connector(for wamp server connection)
https://drive.google.com/open?id=1p8YgSkotOMMV-y8_7OJCG4vt8EjtHh6o
6. Sublime text (for web application)
https://www.sublimetext.com/3

let’s start Now😉


National University of Computer and Emerging Sciences

C# GUI Phase
(1) Open Visual Studio
• Press control+shift+n
• Select Visual C# (Window Form Applications)
• Click Ok.

(2) Click Toolbox of left Side


• All window form widgets are here.
National University of Computer and Emerging Sciences

(3) Drag four buttons from toolbox and change their text as Insert, View, Update
and Delete as given follow.

(4) Now Click on the Project name and Select Add and Click “UserControl”
National University of Computer and Emerging Sciences

(5) Select User Control and Change its name as “Insert.cs”

(6) Add three more UserControls and Name them View.cs, Delete.cs and
Update.cs
National University of Computer and Emerging Sciences

(7) Now Re-Build your Project in Build option and Make Sure these usercontrols
are Appearing in Toolbox like this:
National University of Computer and Emerging Sciences

(8) Now Double Click on the “insert.cs” from the solution Explorer and Design this
as given follow.
• Labels, TextBox and Button
National University of Computer and Emerging Sciences

(9) Now Double click on the View.cs and Design it as follow


• Use DataGridView and a button

(10) Now Rebuild Project and drag the insert (user control) from the
toolbox like as follow:
National University of Computer and Emerging Sciences

(11) Now drag View (usercontrol) from toolbox like as follow:

Connection with Sql Server


(12) Now Open SQL Server and Make a database with name “tutorial”
National University of Computer and Emerging Sciences

(13) And create a table with the name “crud_table” `


National University of Computer and Emerging Sciences

(14) Click on the Data source in Visual Studio and click Add new Source

(15) copy server name from Sql server and Paste it at New Connection.
National University of Computer and Emerging Sciences
National University of Computer and Emerging Sciences

(16) Select Database and Click ok now.


National University of Computer and Emerging Sciences

(17) Now double click on the Insert Button and View button and write the
following code there

(18) Now create a Class named as Sql_Db_Connection.


• Add the following Code there
National University of Computer and Emerging Sciences

class Sql_Db_Connection
{
public SqlConnection connection;
public SqlCommand command;
public SqlDataAdapter sqlDataAdapter;

public Sql_Db_Connection() // constructor to connect with database;


{
connection = new SqlConnection(@"Data Source=DESKTOP-9G16DON\SQLSERVER;
Initial Catalog=Crud;Integrated Security=True");
connection.Open();
}
public bool insert_query(string query)
{
command = new SqlCommand(query, this.connection);
if (command.ExecuteNonQuery() == 1)
{
return true;
}
else return false;
}
public BindingSource Select_Query(string query)
{
sqlDataAdapter = new SqlDataAdapter();
command = new SqlCommand(query, this.connection);
sqlDataAdapter.SelectCommand = command;
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);

BindingSource bindingSource = new BindingSource();


bindingSource.DataSource = dataTable;

return bindingSource;
}
}

(19) Double Click the “insert” button on the Insert.cs and do code there like
as follow:

private void button1_Click(object sender, EventArgs e)


{
string Id, Fname, Lname, contact, semester;
double gpa;
Id = txt_id.Text;
Fname = txt_Fname.Text;
Lname = txt_Lname.Text;
contact = txt_Contact.Text;
semester = txt_Semester.Text;
gpa = Convert.ToDouble(txt_Gpa.Text);

Sql_Db_Connection connection = new Sql_Db_Connection(); // my db class


National University of Computer and Emerging Sciences

string query = "INSERT INTO Crud_Table VALUES('" + Id + "','" + Fname + "','" + Lname + "','" + semester + "','" + gpa + "','" +
contact + "') ";
if(connection.insert_query(query))
{
MessageBox.Show("Data Inserted Successfully");
}

(20) Double click “Refresh” Button in the View.cs and do code there like as
follow:

Sql_Db_Connection sql_Db = new Sql_Db_Connection();


string query = "SELECT * FROM Crud_Table";
BindingSource bindingSource = sql_Db.Select_Query(query);
dataGridView1.DataSource = bindingSource;

Assignment: 01
You have to complete Delete and Update Operations

1) In update.cs UserControl do the following Operations:


• Drag the six Text Boxes and a dataGridView and Label them with Roll
number,Fname,Lname and so on.
• Show all data in DatagridView
• If User double click at any row of DataGridView, then corresponding
to that particular row(roll number) data will be displayed in the text-
boxes.
• Then User can change the values in the textboxes and Click Update
button.
• A Message will be show if the Data has been Updated or not.

2) In Delete.cs UserControl do the following Operations:


• Drag the six Text Boxes and a dataGridView and Label them with Roll
number,Fname,Lname and so on.
• Show all data in DatagridView
National University of Computer and Emerging Sciences

• If User double click at any row of DataGridView, then correcponding


to that particular row(roll number) data will be displayed in the text-
boxes.
• Then User can View these Values and Click Delete button
• A Message will be show if the Data has been Deleted or not.

Connection with Wamp Server

(1) Install Msql-connector First :.

(2) Install wamp Server


National University of Computer and Emerging Sciences

(3) Click in the References in the Visual Studio and Click Add New Reference
and search “mysql” make all the reference and click OK

(4) Copy DbConnection File and Paste in Solution Explorer


• Change the namespace “projectNameHere” in DbConnection File
• Change the database name in the Initialize() Function.

• public void InsertData()


• {
• try
• {
• string query = "Insert into signup()
values('" + this.GetFirstName() + "','" +
this.GetlastName() + "','" + this.GetEmail() + "','" +
this.GetPassword() + "','" + this.GetDate() + "','" +
this.GetGender() + "')";
• Console.WriteLine(query); //will print
the Query on Console.
• Connection.select(query);
• MessageBox.Show("Data inserted
Successfully");

• }
National University of Computer and Emerging Sciences

• catch (Exception ex)


• {
• MessageBox.Show(ex.Message);

• }
}

public bool UpdateQuery()


{
try
{
string query = "update signup set Password =
'"+this.GetPassword()+ "' where Email = '"+this.GetEmail()+"'and
FirstName = '"+this.GetFirstName() +"' and LastName =
'"+this.GetlastName()+"'";
Connection.select(query);
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
Public void Select_data()
{
DataSet dataSet = new DataSet();
string query = "select * from signup where Email = '" +
UserName + "' and Password = '" + Userpassword + "'";
dataSet = db.select(query);
SqlDataAdapter ad = new SqlDataAdapter();
//ad.Fill(dataSet);
if (dataSet.Tables.Count == 0)
{
MessageBox.Show("This User Does Not Exist");
}
foreach (DataRow r in dataSet.Tables[0].Rows)
{

this.txtfname.Text = r["FirstName"].ToString();
National University of Computer and Emerging Sciences

this.txtlname.Text = r["LastName"].ToString();
this.txtpassw.Text = r["Email"].ToString();
this.textpass.Text = r["Password"].ToString();
this.dataUser.Text = r["DOB"].ToString();
}
}

You might also like