Display auto generated incremented number in TextBox using C# and VB.Net in ASP.Net

micah
 
on Oct 06, 2021 07:40 AM
Sample_336800.zip
599 Views

This example in this link stores the generated recipt by button click but its been diplayed on gridvew but i want to display it on textbox when each time the button recipt is clicked.

Generate and save auto incremented number in database using C# in ASP.Net

the current generated recipt should be displayed each time the button recipt is clicked. Here i modified it but the recipt is generated and stored into database but its not displaying on the textbox

protected void btnrecipt_Click(object sender, EventArgs e)
{
    string previousIdQuery = "SELECT MAX(Receipt) UniqueId FROM TransID";
    string newId = GenerateNewId(connStr, previousIdQuery);
    txtrecipt.Text = newId;
   // txtrecipt2.Text = newId;
    string insertQuery = "INSERT INTO TransID VALUES(" + newId + ")";
    using (SqlConnection con = new SqlConnection(connStr))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(insertQuery, con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
}
private string GenerateNewId(string connection, string query)
{
    string newId = string.Empty;
    using (SqlConnection con = new SqlConnection(connection))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            string i = dr[0].ToString();
            if (string.IsNullOrEmpty(i))
            {
                newId = "1";
            }
            else
            {
                int j = Convert.ToInt32(i);
                j = j + 1;
                newId = j.ToString();
            }
        }
        con.Close();
    }
 
    return string.Concat(newId);
}

 

<asp:DropDownList runat="server" ID="store" CssClass="btn btn-custon-four btn-default dropdown-toggle">
</asp:DropDownList>
<asp:TextBox ID="txtrecipt" runat="server" CssClass="form-control" placeholder="Recipt" ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblrecipt" runat="server" ></asp:Label>
<asp:LinkButton ID="btnrecipt" runat="server" CssClass="btn btn-default" OnClick="btncallrecipt_Click" >Call Receipt</asp:LinkButton>

Here is my challenge, i supposed to add the recipt ID and the store

txtrecipt.Text = newId;
// txtrecipt2.Text = newId;
string insertQuery = "INSERT INTO TransID VALUES(" + newId + ")";

 

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Oct 07, 2021 01:39 AM

hi micah, 

   kindly refer below Sample.

HTML

<asp:TextBox ID="txtrecipt" runat="server" CssClass="form-control" placeholder="Recipt" ReadOnly="True"></asp:TextBox>
<asp:Label ID="lblrecipt" runat="server"></asp:Label>
<asp:LinkButton ID="btnrecipt" runat="server" CssClass="btn btn-default" OnClick="SetId">Call Receipt</asp:LinkButton>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

protected void SetId(object sender, EventArgs e)
{
    string connStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    string previousIdQuery = "SELECT MAX(CustomerId) FROM CustomersInfo";
    txtrecipt.Text = GenerateNewId(connStr, previousIdQuery);
    string insertQuery = "INSERT INTO CustomersInfo (CustomerId) VALUES(" + GenerateNewId(connStr, previousIdQuery) + ")";
    using (SqlConnection con = new SqlConnection(connStr))
    {
        SqlCommand cmd = new SqlCommand(insertQuery, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
}

private string GenerateNewId(string connection, string query)
{
    string newId = string.Empty;
    using (SqlConnection con = new SqlConnection(connection))
    {
        con.Open();
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            string i = dr[0].ToString();
            if (string.IsNullOrEmpty(i))
            {
                newId = "1";
            }
            else
            {
                int j = Convert.ToInt32(i);
                j = j + 1;
                newId = j.ToString();
            }
        }
        con.Close();
    }

    return string.Concat(newId);
}

VB.Net

Protected Sub SetId(ByVal sender As Object, ByVal e As EventArgs)
    Dim connStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Dim previousIdQuery As String = "SELECT MAX(CustomerId) FROM CustomersInfo"
    txtrecipt.Text = GenerateNewId(connStr, previousIdQuery)
    Dim insertQuery As String = "INSERT INTO CustomersInfo (CustomerId) VALUES(" & GenerateNewId(connStr, previousIdQuery) & ")"
    Using con As SqlConnection = New SqlConnection(connStr)
        Dim cmd As SqlCommand = New SqlCommand(insertQuery, con)
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Using
End Sub

Private Function GenerateNewId(ByVal connection As String, ByVal query As String) As String
    Dim newId As String = String.Empty
    Using con As SqlConnection = New SqlConnection(connection)
        con.Open()
        Dim cmd As SqlCommand = New SqlCommand(query, con)
        Dim dr As SqlDataReader = cmd.ExecuteReader()
        If dr.Read() Then
            Dim i As String = dr(0).ToString()
            If String.IsNullOrEmpty(i) Then
                newId = "1"
            Else
                Dim j As Integer = Convert.ToInt32(i)
                j = j + 1
                newId = j.ToString()
            End If
        End If
        con.Close()
    End Using

    Return String.Concat(newId)
End Function

 Screenshot