-->

Saturday, June 22, 2013

How to change password in windows forms

In context of computer system, once another person acquire your password he/she can use your computer. They can even use your computer to attack another machines. If he/she do this, it will look like you do anything.
The same can be applied to our applications, if one acquire application’s password then he/she can modify or remove something, to harm you. That’s why we should change the password periodically in order to ensure the security of computer/application.

We have to follow some steps to change the password:

Step 1

After creating login control add another form i.e. Change password, and pass the username from login form to this form using constructor.

chnage password

Step 2

Add three textboxes into new form. One for old password, second for new password and third will be used for comparing new password. Add a button and generate click event of that button.

Step 3

Write following code in click event of button

chnage pwd


In above code, check the user is correct and also entered the equal values in confirm password and new password textbox. If both conditions are satisfied then, assign the user a new password that is in new password textbox. And finally save the changes in database.

Download source.

Thursday, June 20, 2013

How to make login control in windows forms

Sometimes anonymous user can harm our application and can change the data stored in our application. That’s why, it is often necessary to permit only authorized users to use our application. There is no predefined method to do this operation in windows forms, so we have to implement it in our way.

If we have username and password in our database then we can implement a better authentication system. So we will create a table in our database named User which will have two columns (Username and Password). Insert some records in that table to check our login control.


In click event of login button write following code:


Here in this code will show a message if user leaves the textboxes empty. First check the username in our database having exactly the same name user entered in textbox. If there is an entry then compare the password.
We can show a message box at each wrong step of user.

Download example.

Thursday, June 13, 2013

How to bind TabControl with database in windows forms

TabControl manages and displays a related collection of tabs that can contain controls and components. We can add multiple pages in a single form and each page can contain its individual set of controls that can displayed when that tab is selected.

When we add a TabControl in our form then it will add three Tab Pages as default pages and tabPage1 will be selected tab. We can remove any tabPage from that control or can add a new tabPage.

Create a tab page

To add a tab page we can use Add method of TabPages property. Following line of code will add three tab pages without any control

tabControl1.TabPages.Add("Page1");
tabControl1.TabPages.Add("Page2");
tabControl1.TabPages.Add("Page3");

Inserting a tabPage with a label control:

Label label = new Label();    //New labal control
label.Text = "This is the control which will added to TabPage";    //set text property of label

TabPage tabPage = new TabPage();    // create a new tabpage
tabPage.Text = "Tab Page 1";    // set text property of tabpage
tabPage.Controls.Add(label);    //add label control to above tabpage

tabControl1.TabPages.Add(tabPage);    //finally add tabpage to our tabcontrol

Remove a tab page 

To remove a tab page we can use Remove method of TabPages property. Using following line of code we can remove selected tab from our tabControl:

tabControl1.TabPages.Remove(tabControl1.SelectedTab);

Load tabpages from Database 

In the following example we will add tabPage according to our departments. Each tab contains a list of employees working in that department. For showing list of employees we used a DataGridView in a UserControl. When we add a tabPage then we will load userControl in that tabPage.

Step 1:
Create two tables i.e. Department and Employee (described in previous article) in our database.

Step 2:
Insert some records in both tables (described in previous article).

Step 3:
Add a UserControl to our project and then add a DataGridView that will contain list of employees. Write following code in constructor of UserControl:
  
public EmployeeUserControl(Department dept)
{
       InitializeComponent();
       DataContext dc = new DataContext();
       var employees = from temp in dc.Employee
                                where temp.Department.Name == dept.Name
                                select new
                                {
                                       temp.Name,
                                       temp.Email,
                                       temp.Mobile
                                 };
       empDataGridView.DataSource = employees.ToList();
 }
As in above code datagridview will bind to the list of employees according to department.

Step 4:
Now finally in our form’s constructor write below line of codes to add pages to tabControl
foreach (var item in dc.Department)
{
        TabPage newTab = new TabPage(item.Name);
         newTab.Controls.Add(new EmployeeUserControl(item));
         tabControl1.TabPages.Add(newTab);
}

Download Source

Thursday, June 6, 2013

How to bind TreeView with database table in c# Windows Form

TreeView displays a hierarchical collection of labelled items to the user that optionally contains an image. Microsoft is also using treeview in left side of windows explorer from the time of windows 98 till now.



It is very simple to add a root node with some child nodes to treeview as:
TreeView with database
Now in this article I am going to bind this treeview through a database table having following properties:
TreeView with database
Create a database having single table i.e. TreeNode1 as I have described in my previous post “How to create database using Entity Framework”. To bind treeview to table there should be some records in our table Treenode1, so let add some entries to database like:

TreeView with database
Now to bind treeview with table TreeNode1 write following code in our Form1’s constructor
TreeView with database
Here in the first statement I got all the records which have Parent key = 0 i.e. they will be our root node. Now one by one we will check if any node in database have this node as its parent node, if any, then create that new node to its child node.
At the last add that root node to treeview and that is it. I have a screenshot also as an example.
TreeView with database
Download source code.

How to bind GridView with search results in c# Windows Forms

There are many cases in which user want to search some records in database that are satisfying a condition and bind them to gridview. So I am writing this post to bind a gridview with search option and in else case all the data from database will bind to gridview.

Now create a new windows form application in visual studio, make a database as I have described in my previous post How to create Database using Entity Framework and add a table i.e. Student having following properties:

bind GridView with search results
Insert some records in Student table as I have entered following list of items:
Add item to the list
In Form1 designer add two controls TextBox and DataGridView.
Now bind DataGridView to a list that have all the records saved in student table in database.
There is an event of textbox i.e. textchanged event that is raised when the value of text property is changed on control.
In this event of textbox write the following code:
binding data from data list
When you run this project it will show default binding of datagridview as
output screen of the binding data from data list

and when you type some text in textbox then:
searching result of  control
so this datagridview has been bind to search result of textbox.
Download Example.
© Copyright 2013 Computer Programming | All Right Reserved