hello;
how we can generate serial no like 1,2,3,4,..........n. in c#
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Jan 23, 2012, 11:22 PM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Generate_id._Default" %>
using System;
using System.Collections;
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.IO;
namespace Generate_id
{
public partial class _Default : System.Web.UI.Page
{
protected void generateid_Click(object sender, EventArgs e)
{
TextBox1.Text = GenerateSequenceNumber().ToString();
}
private int GenerateSequenceNumber()
{
string file2 = (Server.MapPath("test.txt"));
FileInfo fiRead = new FileInfo(file2);
string input = string.Empty;
int num = 1;
if (fiRead.Exists)
{
input = File.ReadAllText(file2);
}
if (input != string.Empty)
{
num = Convert.ToInt32(input);
}
File.WriteAllText(file2, (num + 1).ToString());
return num;
}
}
}
Thanks
sumaira manzoor hussain khanPosted Jan 23, 2012, 4:07 PM
thanks very much for your help. but the scenario is: i want to generate serial no of patient just like 0001,0002,0003 and so on. this task will be done behind the button. in your answer, your r inserting record then on the basis of this record your r generate serial no. In my scenario first we will generate serial no and then we will insert record. the insert record is another issue but the real issue is how we can generate serial no. thats it.
regards
sumaira
Satyapriya NayakPosted Jan 23, 2012, 10:59 AM
Try this...
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
using System;
using System.Collections;
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;
namespace WebApplication3
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
int count;
protected void GenerateId_Click(object sender, EventArgs e)
{
autogenerated();
}
void autogenerated()
{
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 = count.ToString();
txt_empid.Enabled = false;
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";
txt_empid.Text = "";
txt_empname.Text = "";
txt_sal.Text = "";
}
}
}
Thanks