'How to make auto generate number for text box in C#
I want to Insert auto generate number show in textbox when form is loaded (like 000000001) in database that not define primary key and if table number found 0 then it increment every time when form open by user to entry data. Please help me how to do it. I am using C#. Thanks
its working , but I need to from table filed.
public void RetriveWorkRequestNo ()
{
try
{
string query = "SELECT IDENT_CURRENT('WorkRequests')";
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
SqlCommand cmdwr = new SqlCommand(query, conn);
SqlDataReader reader = cmdwr.ExecuteReader();
while (reader.Read())
{
int value = int.Parse(reader[0].ToString()) + 1;
textWorkRequestNo.Text = value.ToString("0000000000");
textWorkRequestNo.ReadOnly = true;
}
}
catch (Exception)
{
throw;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
Solution 1:[1]
Finally I have done it using Sequence in MS SQL Server 2012
private int RetriveWorkRequestID()
{
try
{
string query = "SELECT NEXT VALUE FOR sqWorkRequests";
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (SqlCommand cmd = new SqlCommand(query, conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
int value = int.Parse(reader[0].ToString());
string date = DateTime.Now.ToString("yyMM");
WorkRequestID = Convert.ToInt32(date.ToString() + value.ToString("000"));
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return WorkRequestsID;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |