How to auto increment a varchar column value in C#
I want to auto increment a column value like P1,P2,P3.......and so on in windows C#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 8, 2013, 3:51 AM EST
Run the attachments.
Satyapriya NayakRank #47 · 31.5K reputationPosted Mar 8, 2013, 3:06 AM EST
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Dynamic_generate_id
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
int count;
public Form1()
{
InitializeComponent();
}
private void btn_save_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "insert into caste(cast_code,cast_desc) values('" + lbl_castecode.Text.Trim() + "','" + txt_castename.Text.Trim() + "')";
com = new OleDbCommand(str, con);
com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Records successfully Inserted");
txt_castename.Text = "";
autogenerated();
}
private void autogenerated()
{
OleDbConnection con = new OleDbConnection(ConnectionString);
str = "select count(*) from caste";
com = new OleDbCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
lbl_castecode.Text = "P" + count.ToString();
lbl_castecode.Enabled = false;
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
autogenerated();
}
}
}
Mustaq Khan29 reputationPosted Mar 8, 2013, 3:38 AM EST
what is lbl_castecode
Mustaq Khan29 reputationPosted Mar 8, 2013, 8:12 PM EST
Mustaq Khan29 reputationPosted Mar 8, 2013, 11:44 PM EST
Gopal Mahadak136 reputationPosted Mar 9, 2013, 12:25 AM EST