SIGN UP MEMBER LOGIN:    
ARTICLE

Working With AutoIncrement Property of DataColumn Class in ADO.NET

Posted by Alok Pandey Articles | ADO.NET in C# January 02, 2012
In this article you will learn to work with the AutoIncrement property of the DataColumn class in ADO.NET.
Reader Level:

Introduction

The column's value of a new row can be incremented by the AutoIncrement property of the DataColumn class. At first, set the AutoIncrement property to true. The two other properties used with the AutoIncrement property are AutoIncrementSeed and AutoIncrementStep.

  • AutoIncrementSeed : It is used to set initial value for increment.
  • AutoIncrementStep : It is used to set value to be increment.

Now we use these properties in our application. I am creating a simple application in which I am sending the value from textbox to DataTable's row and showing the value of DataTable into DataDridView. Open Visual Studio 2010 and take a Window Form Application. Write the following code.

using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

namespace
parameterclass
{
public partialclass Form1 :Form
{
public Form1()
{
InitializeComponent();
}

private void btnok_Click(object sender,EventArgs e)
{
// Creating new row and assigning value to column
DataRow dr = dtable.NewRow();
dr[1]=textBox1.Text;
dr[2] = int.Parse(textBox2.Text);
dr[3] = textBox3.Text;
// Adding DataRow to DataTable
dtable.Rows.Add(dr);
// Binding DataGridView to DataTable
dataGridView1.DataSource = dtable;
}
DataTable dtable;
private void Form1_Load(object sender,EventArgs e)
{ // Creating Intance of DataTable
dtable = new System.Data.DataTable();
// Creating Instance to DataColumns
DataColumn dcid =new System.Data.DataColumn("ID",typeof(int));
DataColumn dcname =new System.Data.DataColumn("NAME",typeof(string));
DataColumn dcage =new System.Data.DataColumn("AGE",typeof(int));
DataColumn dccity =new System.Data.DataColumn("CITY");
// Adding Columns to DataTable
dtable.Columns.Add(dcid);
dtable.Columns.Add(dcname);
dtable.Columns.Add(dcage);
dtable.Columns.Add(dccity);
// Set AutoIncrement property to true
dcid.AutoIncrement =true;
// Set starting value for increment
dcid.AutoIncrementSeed = 11;
// Set increment value
dcid.AutoIncrementStep = 2;
// Binding DataGridView to DataTable
dataGridView1.DataSource = dtable;
}
}
}

Run the application.

Output

autoincrement in .net

Fill the textboxes

autoincrement propety

Click the "ok" button. You will note that you are not assigning any value for "ID". But when you click the button, It shows 11 as "ID" DataGridView. Because the
AutoIncrementSeed property is set to 11. Look at the following figure.

autoincrement

Again fill the textboxes with a value.

autoincrement in ado.net

Click the button. You will note that "ID" will be incremented by 2. Because the AutoIncrementStep property is set to 2. Which means it is incremented by 2. Look at the following figure.

autoincrement in ado.net

Here are some related resources.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor