-->

Tuesday, June 25, 2013

How to Bind CheckBoxList in ASP.NET

Introduction:
you can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes. The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a datasource to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these controls to a data source.


Public Properties of the CheckBoxList class
Cellpadding : Obtains the distance between the border and data of the cell in pixels.

CellSpacing : Obtains the distance between cells in pixels.

RepeatColumns : Obtains the number of columns to display in the CheckBoxList control.

RepeatDirection : Obtain a value that indicate , whether the control displays vertically or horizontally.

RepeatLayout : Obtains the layout of the check boxes.

TextAlign : Obtains the text placement for the check boxes within the group.


STEP-1 :  Create Database Table  in visual studio 2012.



STEP-2 :   Select CheckBoxList from ToolBox and drop onto Design window

checkboxlist

STEP-3 : Bind CheckBoxList on PageLoad method.






using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class checkboxlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
        {
            using (SqlConnection con=new SqlConnection ())
            {
con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
                con.Open ();
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * from [Table]";
                    cmd.Connection = con;
                    using (DataSet ds = new DataSet())
                    {
                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {
                            da.Fill(ds);
                            CheckBoxList1.DataSource = ds;
                            CheckBoxList1.DataTextField = "name";
                            CheckBoxList1.DataValueField = "Employeeid";
                            CheckBoxList1.DataBind();
                        }
                    }
                }

                
            }
        }
    }
}



OutPut Screen

output screen





Monday, June 24, 2013

How to create MDI container form and open child forms in it



MDI, stands for Multiple Document Interface, is a user interface that enable user to work with more than one document at the same time. Each document is displayed in a separate window called child window. A child window cannot appear outside the main window because it is confined in main window. 

To create MDI parent form:



  • Add a new form "MainForm"
  • In properties window set IsMdiContainer property to true
  • Drag a Menustrip from the toolbox and create some menus like New, Open and Save etc. This menu bar can be used by all child windows because it is in parent window
Run the project and see the form something like below form


To create MDI child form:

  • Add new form in the project i.e. Form2
  • In the click event of “New” menu item write following code to open a child window
    Form2 childForm = new Form2();
    childForm.MdiParent = this;
    childForm.Show();
Run the project and when we click on new menu item a child window will open

MDI form

To get title of Active MDI child window

  • Add a new menu item “Get Active Form”
  • In the click event of this menu item write following code
    Form activeForm = this.ActiveMdiChild as Form;
    string title = activeForm.text;
    MessageBox.Show(title);
Run the project and open some child forms. Click on “Get Active Form” menu button and a message box will appear having title of currently active child form.

MDI active child form
So this is how we make MDI container form with a child form. We can open more child form using the same method.

How to use HasRows property of SqlDataReader class

Use HasRows property of SqlDataReader class 
Gets a value that indicates whether the SqlDataReader contains one or more rows (msdn.microsoft.com)



Step-1 : Create a database table with empty data


empty data




Step-2 : Check data in database using HasRows property of  SqlDataReader class

Step-3 :  Write code on page_load method



using System;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class duplicatevalue : System.Web.UI.Page
{
    SqlDataReader rd;

    protected void Page_Load(object sender, EventArgs e)
    {

        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString =ConfigurationManager .ConnectionStrings ["ConnectionString"].ToString ();
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "checkdata";
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                rd = cmd.ExecuteReader();
                if (rd.HasRows)
                {
                   result.Text="Row Exisit";  //here result is the id of the label control
                }
                else
                {
                    result.Text = "no row found";

                  
                }
                                
            }

       
        }

    }

OutPut Screen
EmptyData





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.

Friday, June 21, 2013

what is the use of SqlDataReader in ASP.NET

SqlDataReader 
Provides a way of reading a forward-only stream of rows from a SQL Server database ( source from msdn.microsoft.com)

Example How to bind gridview using SqlDataReader class

Step-1 : Take a Gridview onto design window. 
Step-2 :  Develop code for binding gridview using SqlDataReader.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class datareadercl : System.Web.UI.Page
{
    SqlDataReader rd;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
                con.Open ();
                using(SqlCommand cmd=new SqlCommand ())
{
                    cmd.CommandText ="select * from [Table]";
                    cmd.Connection =con;
                    rd=cmd.ExecuteReader();
                    GridView1 .DataSource =rd;
                    GridView1 .DataBind ();                  
 
}

            }
        }
    }
}

OutPut of the program


bindgridview
Read() method of SqlDataReader class  : read next record


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.

Wednesday, June 19, 2013

How to check data in database in ASP.NET

follow some steps for checking data into database
Step 1: Create table in database  http://dotprogramming.blogspot.in/2013/06/how-to-make-database-table-in-visual.html

database table

table value




Step 2:  Take a web form name as "duplicatevalue.aspx"
Step 3:  Design a Webform

design form

Step 4: Make Connection string in web.config file

Step-5 : Create procedure for retrieving data 

CREATE PROCEDURE [dbo].[checkdata]

AS
SELECT * from [Table] 
RETURN 0



Step 6: Double click on "Check Existing" Button and make code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class duplicatevalue : System.Web.UI.Page
{
    SqlDataReader rd;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        bool flag = true;

        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString =ConfigurationManager .ConnectionStrings ["ConnectionString"].ToString ();
            con.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "checkdata";
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                rd = cmd.ExecuteReader();
                while (rd.Read ())
                {
                    if (rd["EmployeeId"].ToString().Equals(empid.Text))
                    {
                        flag = false;
                        break;
                        

                    }
                }
                if (flag == false)
                    result.Text = "ALREADY EXISTS";
                else
                    result.Text = "not existes";

                                
            }
        }

    }
}

Out put of the program
data exists
not exists


© Copyright 2013 Computer Programming | All Right Reserved