Hi micah,
Refer below code.
HTML
<asp:TextBox ID="txtcode" runat="server" />
<asp:LinkButton ID="btncode" runat="server" OnClick="btncode_Click">Generat Receipt</asp:LinkButton>
Code
C#
protected void btncode_Click(object sender, EventArgs e)
{
int counter;
if (ViewState["Count"] != null)
{
counter = Convert.ToInt32(ViewState["Count"]);
}
else
{
counter = 0;
}
counter = counter + 1;
ViewState["Count"] = counter;
string constr = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
SqlCommand command = new SqlCommand("INSERT INTO TransID(Receipt)VALUES(@Receipt);", con);
command.Parameters.AddWithValue("@Receipt", counter.ToString().PadLeft(3, '0'));
con.Open();
int i = command.ExecuteNonQuery();
con.Close();
if (i > 0)
{
txtcode.Text = counter.ToString().PadLeft(3, '0');
}
}
}
VB.Net
Protected Sub btncode_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim counter As Integer
If ViewState("Count") IsNot Nothing Then
counter = Convert.ToInt32(ViewState("Count"))
Else
counter = 0
End If
counter = counter + 1
ViewState("Count") = counter
Dim constr As String = ConfigurationManager.ConnectionStrings("db").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim command As SqlCommand = New SqlCommand("INSERT INTO TransID(Receipt)VALUES(@Receipt);", con)
command.Parameters.AddWithValue("@Receipt", counter.ToString().PadLeft(3, "0"c))
con.Open()
Dim i As Integer = command.ExecuteNonQuery()
con.Close()
If i > 0 Then
txtcode.Text = counter.ToString().PadLeft(3, "0"c)
End If
End Using
End Sub