Implementation of Page Index or Paging in Gridview Control In asp.net (c# & Vb.net).

Introduction:

Pagination is useful technique in case of large data. When data is large , it’s not possible to show whole data in gridview. It is not user-friendly. User has to scroll to see a particular record. Paging is helpful in this case. We can show particular no of record in single page. By default gridview show “10” records per page.

In this article I will explain how to implement Paging in gridview.

How to Populate or Fill Dataset with data from Database in asp.net using c# & Vb.net?

In this article we will learn how to fill or populate dataset with data from database in asp.net using c# and vb.net. First of we will fetch data from Sql database using Sql query then  fill data in Dataset using SqlDataAdapter.

Implementation: 
1.     Create a database i.e. “Blog”.  Then create a table “Student_Info”.
Column Name
Datatype
Student_id
Int(Primary Key. So set Is Identity=True)
Student_Name
Varchar(500)
Age
int
Class
Varchar(50)

2.     Now insert some data in this table using “insert” command.

INSERT INTO student_info(Student_Name,Age,Class) VALUES('Akhsay',12, 'first')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('Raghav',25, 'M.Sc')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('aksht', 23, 'Bsc')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('ankit', 23, 'MBA')
INSERT INTO student_info(Student_Name,Age,Class) VALUES('anil', 23, 'MCA')

3.     Create Connection: Now create connection in  webcofig file as given below.

<connectionStrings>
    <add name="con" connectionString="Data Source=localhost; Initial Catalog= Blog; Integrated Security=true;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

4.     ASP.NET code behind File using C#:
In code behind, add following code:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Now Fetch data and Fill Dataset:

    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e) 
    {
        if (!IsPostBack)
        {
            Fill_dataset();
        }
    }
    //Fetch data from database
    public void Fill_dataset()
    {     
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
        DataSet ds = new DataSet();
        SqlDataAdapter dataadapater = new SqlDataAdapter("Select * from student_info", con);
        dataadapater.Fill(ds);      
    }


VB.NET code behind file:
In code behind, add following code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Now Fetch data and Fill Dataset:

Partial Public Class gridview
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            Fill_dataset ()
        End If
    End Sub
    'Fetch data from database
    Public Sub Fill_dataset ()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("con").ConnectionString)
        If con.State = ConnectionState.Open Then
            con.Close()
        End If
        con.Open()
        Dim ds As New DataSet()
        Dim dataadapater As New SqlDataAdapter("Select * from student_info", con)
        dataadapater.Fill(ds)
    End Sub
End Class

Demo:



Use this code, create your own application and check result. You can use this code in any application by customizing it. If you have any doubt, you can ask by commenting in comment section.

ASP.NET: Use Template field to display data in Gridview in asp.net(c# & vb.net).

We can use template fields to display data in gridview. In this article I will explain how to get data from database and display in gridview using Template Fields in gridview.

Display data in Gridview using boundField in asp.net(c#, VB.net)

Introduction:
In this article I will explain how to get data from database and display in gridview using BoundField in gridview.
Steps to follow:
1.       Add webpage to your application. Now drag and drop gridview control to your webpage.
2.       Right click on gridview and select Edit columns option.
3.       Now add bound fields from options. And set Header Text and Data Field.
4.       In code file, bind gridview with data from database using data table.

Delete record from gridview control in asp.net(c#&vb.net) with confirmation.

Introduction:
It is good practice and user friendly to confirm before deleting record from gridview. In this article we will learn how to confirm from user before actually deleting the record in gridview .  

Delete record from gridview on RowCommand Event in asp.Net(c#& vb.net).

Introduction:
In this article I will explain how to delete row in gridview on Row_Command Event of gridview .

Add a gridview on webpage and add columns which we want to show on the webpage using boundfields of gridview. Now add Template field and add one link button inside template field. Set command-name and command argument. Bind gridview with data from database.
Now use GridView1_RowCommand event of the grid view and also fire RowDeleteing event to handle delete command. Write the code in RowCommand event to delete record.

How to insert data into database in ADO.NET( or asp.net ) using C# and vb.net.

Introduction:
I have written this article for beginners. In this article I have explained how to insert data from asp.net web page to database. I have used query to insert data to database.

In this article I have created a webpage and place 3 text box controls and save these values to database. I have created a database and a table to explain this concept. Then I have created connection in webconfig file of application. Then using code in code behind file on button click, saved data to database.

JavaScript: Find position of first occurrence of sub-string or value using IndexOf method in JavaScript.

Introduction:
Some time we need to find the position of the first occurrence of a specified value in given string. In this article I will explain how to find position of the first occurrence of a specified value using Javascript.

Description:
I have used “string.indexOf()” method of JavaScript to find the position of the first occurrence of a specified substring or value. The indexOf() method searches a string for a specified value, and returns the the position of the first occurrence of a specified substring or value. This method return -1 if no match is found.

Find Position of sub-string in string using Search() method in JavaScript.

Introduction:
Some time we need to find the position of substring or word in given string. We can find position of substring by using code in asp.net or by using JavaScript.
In this article I will explain how to find potion of substring using Javascript.
ASP.NET: Introduction to Validation Techniques or validations Controls in asp.net.

ASP.NET: Introduction to Validation Techniques or validations Controls in asp.net.

Validation in Asp.NET

In this article I will explain different type of validations in asp.net. We will learn why we need validations in website development. The main motive of using validations is to validate input data from users. We validate controls so that they accept only correct data or data in valid format.


Why we need Validations:

Validation is required when we want input in particular format. Suppose we want to enter numeric no in TextBox control. So in that case we require validation. Suppose we want to enter email address in textbox then in that we will have to validate that particular textbox. We can validate controls for data type, empty control, special format, range etc.

Types of validations in Asp.NET:
   1.      Client side validator
   2.      Server Side validator

Server side validations: Server side validations are more secure. We use code in code behind to validate the data. So it is bit slow when compared to client side.

Client side validations: Client side validations are fast and easy to implement. Asp.Net Provides some validation controls to implement client side validations.

Validations controls in Asp.NET

  •         RequiredFieldValidator
  •         RangeValidator
  •         CompareValidator
  •         RegularExpressionValidator
  •         CustomValidator
  •         ValidationSummary
Main three properties are common in most of the controls:

ControlToValidate: Id of the control which we want to validate
Error Message: Error message which will be shown when error occurs
Text: Text which will be shown.

1.     Required Field Validator:
Required field validator is used to when we want to check whether control is empty or not. It ensures that the control is empty or not. We generally validate textbox with this control. We can also validate dropdownlist control.

Syntax to validate TextBox using Requiredfieldvalidator:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter Name" ControlToValidate="txtName">*</asp:RequiredFieldValidator>

2.            RangeValidator Control

The RangeValidator control verifies that the input value falls within a predetermined range.

Syntax for Range validator:
  <asp:RangeValidator ID="rvValue" runat="server" ControlToValidate="txtValue"
   ErrorMessage="Enter Value between (10 - 20)" MaximumValue="20"
   MinimumValue="10" Type="Integer">

3.     CompareValidator Control

The CompareValidator control compares a value in one control with a fixed value or a value in another control.

Syntax  CompareValidator Control:
       <asp:CompareValidator ID="CompareValidator1" runat="server"
            ErrorMessage="CompareValidator" ControlToCompare="txtConfirmPassword"
            ControlToValidate="txtName"></asp:CompareValidator>

4.     RegularExpressionValidator

The RegularExpressionValidator is used in case of validating the input text by matching against a pattern of a regular expression. The regular expression is set in the ValidationExpression property.
We can use other regular expression to validate data which are not listed in ValidationExpression property.

Syntax for regular expressions:
<asp:RegularExpressionValidator
                ID="RegularExpressionValidator1" runat="server"
            ErrorMessage="Enter Valid Email" ControlToValidate="txtEmail"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>

5.     CustomValidator:

The CustomValidator control is used to write custom validation for both the client side and the server side validation.
For client side validation we use ClientValidationFunction property. For server side we use onservervalidate property. We call function created on server side.

Syntax For custom Validator:
<asp:CustomValidator runat="server" id="customname" controltovalidate="txtName" onservervalidate="function_name" errormessage="The text must be exactly 10 characters long!" />

6.     Validation Summary: Validation summary is not used to validate controls. It is used to show all error message in  one place.

Main prioperties are:

ShowMessageBox: it will show error in message box.
ShowSummary:It will show messages as a list.


Kategori

Kategori