Window Form C#
How to generate Auto Registration no. in Window form c# ado.net???
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
Gopal Mahadak136 reputationPosted Feb 1, 2013, 11:53 PM EST
oledbconnection con=new oledbconnection(string connection);
con.open();
oledbcommand cmd=new oledbcommand("select max(regno) from table",con)
oledbdatareader dr=cmd.executereader();
while(dr.read()==true)
{
text1.text=dr["regno"].tostring();
}
read it in form load gives u autoincremented result
Satyapriya NayakRank #47 · 31.5K reputationPosted Feb 2, 2013, 12:08 AM EST
Create table employee(empid varchar(50) primary key,empname varchar(50),sal int)
Default.aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
Default.aspx.cs code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
str = "select count(*) from employee";
com = new SqlCommand(str, con);
con.Open();
count = Convert.ToInt16(com.ExecuteScalar()) + 1;
txt_empid.Text = "E00" + count;
con.Close();
}
protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "insert into employee values('" + txt_empid.Text.Trim() + "','" + txt_empname.Text.Trim() + "'," + txt_sal.Text.Trim() + ")";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Label4.Text = "Records successfully Inserted";
}
}
Deoraj Sharma14 reputationPosted Feb 2, 2013, 7:43 AM EST