-->

Thursday, June 27, 2013

Visual studio 2013 ,start with a familiar environment

In visual studio 2013 , Select your general Development setting using dropdown menu Also select theme option . There are three theme option Blue, Dark and light.

Visual Studio 2013 Start-Up Screen:

 
New in Microsoft platform
  • Windows
  • Windows Azure
  • ASP.NET vNext and Web
  • Windows Phone
  • Microsoft Office
  • Share Point Development
Features
  • Database Table Appear in server explorer without refresh
Download
 

 

 
  •  




Wednesday, June 26, 2013

Cut, Copy and paste operations in windows forms

Introduction

It is impossible for one, specially a Programmer, to imagine his/her life without editing. Editing operations (Cut/Copy and paste etc.) are performed using clipboard, so these are also called Clipboard operation.
Invention

Larry Tesler, who invented Cut/Copy and paste in 1973. He is a computer scientist, working in the field of human-computer interaction. Tesler has worked at Xerox PARC, Apple Computer, Amazon.com and Yahoo! We can find out more about LarryTesler.


In this article I will show how to perform these operations with our own code. As we are just doing editing operations, it doesn't matter which control we are using. Here I am using two textboxes to perform this operation. I created a simple window form with a menu strip and two textboxes. In the menu strip all the operations are added in Edit menu item as shown in below screenshot.



Generate the click event of above three menu items. 
Copy

We are going to copy the selected text of textbox, so we use SelectedText property of textbox. To copy the data in clipboard there is a method in Clipboard class i.e. SetData(), which will clear the clipboard and then add new data in specified format to clipboard.

private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
      TextBox txtBox = this.ActiveControl as TextBox;
      if (txtBox.SelectedText != string.Empty)
         Clipboard.SetData(DataFormats.Text, txtBox.SelectedText);
Cut

Actually, cut operation is a combination of copying and removing that text from that location. So we will copy the text and empty that string after that.

private void cutToolStripMenuItem_Click(object sender, EventArgs e)
{
    TextBox txtBox = this.ActiveControl as TextBox;
    if (txtBox.SelectedText != string.Empty)
       Clipboard.SetData(DataFormats.Text, txtBox.SelectedText);
    txtBox.SelectedText = string.Empty;
Paste


To insert clipboard data to current location is called paste. We can perform a paste operation multiple times with a single clipboard data. To paste the data from clipboard there is a method in Clipboard class i.e. GetText(). First we get actual position of cursor in the textbox and then insert the data to that position using Insert() method as in above code.

private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
    int position = ((TextBox)this.ActiveControl).SelectionStart;
    this.ActiveControl.Text = this.ActiveControl.Text.Insert(position, Clipboard.GetText());
}

Insert() is a method of string class, which return a string in which a specified string is inserted at a specified position. So that is how we perform editing operations with our own code. The same procedure can be used to perform these operation in other controls of windows forms.

Download source.

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


© Copyright 2013 Computer Programming | All Right Reserved