hi alhamd,
Please refer below sample.
HTML
<asp:TextBox runat="server" ID="txtInvoice"></asp:TextBox>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.VoucherNo();
}
}
protected void VoucherNo()
{
string lastInvoice = "";
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT MAX(InvoiceNo) FROM PurchaseOrder", con))
{
con.Open();
lastInvoice = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
if (!string.IsNullOrEmpty(lastInvoice))
{
lastInvoice = "PO-" + (Convert.ToInt32(lastInvoice.Split('-')[1]) + 1).ToString();
}
else
{
lastInvoice = "PO-1";
}
txtInvoice.Text = lastInvoice;
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.VoucherNo()
End If
End Sub
Protected Sub VoucherNo()
Dim lastInvoice As String = ""
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT MAX(InvoiceNo) FROM PurchaseOrder", con)
con.Open()
lastInvoice = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
If Not String.IsNullOrEmpty(lastInvoice) Then
lastInvoice = "PO-" & (Convert.ToInt32(lastInvoice.Split("-"c)(1)) + 1).ToString()
Else
lastInvoice = "PO-1"
End If
txtInvoice.Text = lastInvoice
End Sub
Screenshots