Number will Increment by code in C#

Vilasgite080
Posted by Vilasgite080 under C# category on | Points: 40 | Views : 3104
Here ITEM_CODE will auto increment
//Code for Add & Save button of shown form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SALE_INVENTORY
{
public partial class PRODUCTS : Form
{
SqlConnection con = new SqlConnection();
DataTable dt;
DataRow dr;
String code;

public PRODUCTS()
{
InitializeComponent();


private void PRODUCTS_Load(object sender, EventArgs e)
{

btnSave.Enabled = false;
btnCancel.Enabled = false;
btnUpdate.Enabled = false;

cmbItemcode.Enabled = false;
cmbItemName.Enabled = false;
txtMP.Enabled = false;
txtSP.Enabled = false;
txtRem.Enabled = false;


// TODO: This line of code loads data into the 's_productsDS.PRODUCTS_TABLE' table. You can move, or remove it, as needed.
this.pRODUCTS_TABLETableAdapter.Fill(this.s_productsDS.PRODUCTS_TABLE);

}

private void btnAdd_Click(object sender, EventArgs e)
{
try
{
btnEdit.Enabled = false;

cmbItemcode.Enabled = true;
cmbItemName.Enabled = true;
txtMP.Enabled = true;
txtSP.Enabled = true;
txtRem.Enabled = true;

btnEdit.Enabled = false;

cmbItemcode.Text = "";
cmbItemName.Text = "";
txtMP.Text = "";
txtSP.Text = "";
txtRem.Text = "";

int ctr, len;
String prod_codeval;
dt = s_productsDS.Tables["PRODUCTS_TABLE"];

len = dt.Rows.Count - 1;
dr = dt.Rows[len];
code = dr["ITEM_CODE"].ToString();
prod_codeval = code.Substring(2, 4);
ctr = Convert.ToInt32(prod_codeval);

if ((ctr >= 1) && (ctr < 9))
{
ctr = ctr + 1;
cmbItemcode.Text= "IM000" + ctr;

}
else if ((ctr >= 9) && (ctr < 99))
{
ctr = ctr + 1;
cmbItemcode.Text = "IM00" + ctr;
}
else if ((ctr >= 99) && (ctr < 999))
{
ctr = ctr + 1;
cmbItemcode.Text = "IM0" + ctr;
}
else if ((ctr >= 999) && (ctr < 9999))
{
ctr = ctr + 1;
cmbItemcode.Text = "IM" + ctr;
}

btnAdd.Enabled = false;
btnSave.Enabled = true;
btnCancel.Enabled = true;
btnUpdate.Enabled = false;
}
catch
{
MessageBox.Show("There are Error");
}
}

private void btnSave_Click(object sender, EventArgs e)
{


try
{
dt = s_productsDS.Tables["PRODUCTS_TABLE"];
dr = dt.NewRow();

dr[0] = cmbItemcode.Text;
dr[1] = cmbItemName.Text;
dr[2] = txtMP.Text;
dr[3] = txtSP.Text;
dr[4] = txtRem.Text;

dt.Rows.Add(dr);
pRODUCTS_TABLETableAdapter.Update(s_productsDS.PRODUCTS_TABLE);

MessageBox.Show("Record Save Successfuly", "Data Entry Operator", MessageBoxButtons.OK, MessageBoxIcon.Information);

cmbItemcode.Enabled = false;
cmbItemName.Enabled = false;
txtMP.Enabled = false;
txtSP.Enabled = false;
txtRem.Enabled = false;


btnAdd.Enabled = true;
btnSave.Enabled = false;
btnCancel.Enabled = false;
btnEdit.Enabled = true;
btnUpdate.Enabled = false;

}
catch
{
MessageBox.Show("Entered Data is not valid. Please enter valid data", "Data Entry Operator", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
}

}

}
}


Before running this code you will need SQL Server 2005 table 'PRODUCTS_TABLE'.

Comments or Responses

Posted by: T.saravanan on: 6/27/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi,

What is the use of your code?
Could you give little bit explain about your code its really useful to others?
Posted by: Jayeshl on: 6/28/2011 Level:Starter | Status: [Member] | Points: 10
hi dear,
thanks to post this code sample,
but can you plz explain me how it relate to Number will Increment by code in C#


thanks

Login to post response