Insert generated number in database and display in ASP.Net TextBox using C# and VB.Net

micah
 
on Mar 23, 2021 06:22 AM
936 Views

How do i generate numbers insert it in table and display the number in txtcode at the same time.

I have this example that selects numbers in table and display it on textbox, but i want the button btncode to insert the numbers in table at the same time display it on textbox, numbers start from 100.

<div class=" pull-right"  style="margin-bottom:12px">
    <asp:LinkButton ID="btncode" runat="server" CssClass="btn btn-success" OnClick="btncode_Click">Generat Receipt</asp:LinkButton>
</div>

 

protected void btncode_Click(object sender, EventArgs e)
{
    string constr = ConfigurationManager.ConnectionStrings["Test".ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        SqlCommand command = new SqlCommand("SELECT numbers FROM ItemOrdered WHERE ID = @ID", con);
        command.Parameters.AddWithValue("@ID", txtcode.Text);
        con.Open();
        SqlDataReader sdr = command.ExecuteReader();
        if (sdr.Read())
        {
            txtcode.Text = sdr["Numbers"].ToString();
           // txtname.Text = sdr["Name"].ToString();
        }
        sdr.Close();
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 24, 2021 01:28 AM
on Jan 04, 2022 10:18 AM

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