-->

Thursday, September 17, 2015

Edit menu bar with cut copy and paste operations in Windows Form C#

Introduction

In this article i will show you how to do cut copy and paste operation on text which is entered in the TextBox. I will give you an example of it. 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.

Wednesday, September 16, 2015

Bind CheckBoxList from Database in ASP.Net C# Example

Introduction:
In this article we will learn how to bind CheckBoxList in asp.net c#. I will give you an example of CheckBoxlist binding. 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, September 14, 2015

Calculate number of records in Gridview when it bind with SqlDataSource

Introduction

In this article i will show you how to calculate number of records in database table. This is possible through SqlDataSource class. You can call AffectedRows property  of the SqlDataSource. So lets take a example of count numbers of records in database table.


Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default17.aspx.cs" Inherits="Default17" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
                <asp:BoundField DataField="Emp_Name" HeaderText="Emp_Name" SortExpression="Emp_Name" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" OnSelected="SqlDataSource1_Selected" SelectCommand="SELECT * FROM [Employee]"></asp:SqlDataSource>
        Total Number of records:
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

Code Behind Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default17 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        Label1.Text = e.AffectedRows.ToString();
    }
Code generate the following output

Calculate number of records in Gridview when it bind with SqlDataSource


Friday, September 11, 2015

Retrieving Data Using a DataReader in ASP.NET C#

Introduction
In this article i will show you, how to retrieve data from database table using DataReader. I will give a example of data fetching from database table using SqlDataReader class, also bind the GridView with the DataReader.
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


September 22 Microsoft Office 2016 launch date fixed

Microsoft proclaimed this morning the official launch date for the long-anticipated remake of Microsoft office. office 2016 are going to be loosely out there beginning on September 22, the corporate says. Meanwhile, office Customers with volume licensing agreements are going to be ready to download the package on October 1.


The updated version of office includes variety of latest options for desktop customers, together with the power to co-edit documents at identical time, synchronize files to One Drive within the cloud, and more.

Alongside the announcement of the launch date, Microsoft noted a number of different new options and choices for IT admins and businesses wanting to deploy the software package.

For starters, the corporate noted that office 365 Pro Plus customers WHO square measure paying for the subscription version of workplace for corporations, also will receive the “Current Branch” – that means the foremost up-to-date feature unleash and security updates – on September 22. This unleash also will embody the new office 2106 app updates.

But supported client feedback, Microsoft says it’s introducing a brand new update model referred to as “Current Branch for Business,” which is able to offer 3 additive feature updates annually, whereas continued to supply monthly security updates. this can be designed for organizations that wish to check new releases of office 2016 before rolling out the new options, a Microsoft journal post explains.

Thursday, September 10, 2015

BulletedList Web Server Control Overview, binding with Sql Server in ASP.NET C#

Introduction
In this article example i will show you how to bind bulleted list web server control with sql server database. In previous article i explained use of it control using visual studio like how to add value in it at compile time. Lets see the example of bulleted list, which is bind with sql datasource control.


Following some steps for binding bulleted list control using SqlDataSource

Step-1 : Open visual studio
Step-2:  Click  File-->New-->WebSite

starting screen

Step-3: Right click on website name in solution explorer, Add-->Add new item 

web form


Step-4: Make Database table 
Step-5: Drag Bulleted list from toolbox and drop on design window.
Step-6: Select Show smart tag of Bulleted list

show smart tag

Step-7: On Bulledted list task click choose data source link
Step-8: Select New DataSource from Data Source configuration wizard.

choose data source

Step-9: Select SQL Database from Data Source Configuration wizard. and click ok button

data configuration wizard

Step-10 : Select existing database from configure data source.

connection string property

Step-11 : Change Connection name according to you and click "ok".

web.config file

Step-12 : Select table from name ,select table field name and press ok button

table view

Step-13: Click test query button and finish the process
Step-14: Select a data field to display in the bulleted list.
Step-15: Select a data field for the value of the bulleted list. and press ok button.
select one those field in bulleted list

Run your application

how to bind bulleted list in asp.net

Finally source of the code are:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:BulletedList ID="BulletedList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name " DataValueField="Url">
        </asp:BulletedList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [mytab]"></asp:SqlDataSource>
    
    </div>
    </form>
</body>
</html>
Top Related Article

Check, if value exists in Database ASP.NET C#

Introduction
In this article i will show you availability of data in the database table, if exist then return true otherwise return false. In this article we will use linear search algorithm to search the data. So lets see the example of it.

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 = false;

        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 = true;
                        break;
                        

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

                                
            }
        }

    }
}

Out put of the program
data exists
not exists

The same article we can do with different method check this article in different style.
© Copyright 2013 Computer Programming | All Right Reserved