I have master table called MCaste whose fields are as id int(identiity), cast_code (nvrchar(10)) , cast_desc varchar(50). then I design a C# windows form for this table as follows I want a code like user can only insert caste description and castcode should be generate automatic as like "CS00000001","CS00000002" AND so on. I dont have any idea. Plz help me to achieve this.
Thanks in advance.
Satyapriya NayakPosted Jan 9, 2013, 6:27 AM
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 = "CS00000000" + count.ToString();
lbl_castecode.Enabled = false;
con.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
autogenerated();
}
}
}
Thanks
If this post helps you mark it as answer
venkata kumarPosted Jan 9, 2013, 1:40 AM
u can go through
create procedure [dbo].[InsertCaste]
(
@CasteCode varchar(50),
@CasteName varchar(50)
)
AS
BEGIN
INSERT INTO MCaste(
[CasteName]
)
VALUES(
@CasteName
)
set @Castecode=(select max(num1) from Caste)
update MCaste set Castecode='CS0000000'+@Castecode where CasteCode is null
ND
END