Update ASP.Net Label after inserting record in Database on Button Click in C# and VB.Net

Last Reply 9 months ago By pandeyism

Posted 9 months ago

On page i have a dropdown item, textbox quantity, label nd submit button.

the dropdown when selected shows the last quantity update on the lable display, after i key in quantity in textbox and submit it will minus the inserted quantity from the displayed quanity, when i submit i want the lable to show the updated quantity.

what i actaully mean by updating label after button submit is if i have a lable that shows qauntity of items like example below

label, item qauntity: 500

then i have a textbox for inserting quantity by customer, so if i insert 50 by cutomer and submit, it will deduct it from the 500 items by label and the label will be updated with 450.

    protected void btnsubmit_Click(object sender, EventArgs e)
    {
        int inserted = 0;
        using (SqlConnection con = new SqlConnection())
        {
            con.ConnectionString = str;
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                
                cmd.CommandText = "INSERT INTO MainWareHouse (ItemName,Category,CartonQty) VALUES(@ItemName,@Category,@RowQty,@CartonQty)";
                cmd.Parameters.AddWithValue("@ItemName", ddlitem.SelectedItem.Text.Trim());
                cmd.Parameters.AddWithValue("@Category",ddlcategory.SelectedItem.Text.Trim());
                //cmd.Parameters.AddWithValue("@RowQty", txtRowqty.Text);
                cmd.Parameters.AddWithValue("@CartonQty", txtCartonqty.Text);
               // cmd.Parameters.AddWithValue("@ItemName", ddlitem.SelectedIndex);
                con.Open();
                inserted = cmd.ExecuteNonQuery();
                con.Close();
                string message = string.Empty;
                {
                    lblMessage2.Visible = true;
                    lblMessage2.Text = "Data Submitted Successfully";
                }

 

Posted 9 months ago

Hi micah,

Please refer below sample.

HTML

<div>
    <asp:DropDownList ID="ddlItemName" runat="server">
        <asp:ListItem Text="A" />
        <asp:ListItem Text="B" />
    </asp:DropDownList>
    <asp:DropDownList ID="ddlCategory" runat="server">
        <asp:ListItem Text="Alpha" />
    </asp:DropDownList>
    <asp:TextBox runat="server" ID="txtCountry" />
    <asp:Label ID="lblMessage" runat="server" />
    <asp:Button Text="Insert" runat="server" OnClick="btnsubmit_Click" />
</div>

Namespaces

C#

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

VB.Net

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        lblMessage.Text = GetQuantity().ToString();
    }
}

private int GetQuantity()
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT CartonQty FROM StockDemo", con))
        {
            con.Open();            
            return Convert.ToInt32(cmd.ExecuteScalar());
            con.Close();
        }
    }
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
    int prequt = Convert.ToInt32(lblMessage.Text);
    int currentqut = Convert.ToInt32(txtCountry.Text);
    int totalqut = prequt - currentqut;
    int inserted = 0;
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    cmd.CommandText = "INSERT INTO MainWareHouse (ItemName,Category, CartonQty) VALUES(@ItemName,@Category,@CartonQty)";
    cmd.Parameters.AddWithValue("@ItemName", ddlItemName.SelectedItem.Text.Trim());
    cmd.Parameters.AddWithValue("@Category", ddlCategory.SelectedItem.Text.Trim());
    cmd.Parameters.AddWithValue("@CartonQty", totalqut);
    con.Open();
    inserted = cmd.ExecuteNonQuery();
    con.Close();

    cmd = new SqlCommand();
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    cmd.CommandText = "UPDATE StockDemo SET CartonQty=@CartonQty WHERE ItemName=@ItemName";
    cmd.Parameters.AddWithValue("@ItemName", ddlItemName.SelectedItem.Text.Trim());
    cmd.Parameters.AddWithValue("@CartonQty", totalqut);
    con.Open();
    inserted = cmd.ExecuteNonQuery();
    con.Close();
    lblMessage.Text = GetQuantity().ToString();
}

VB.Net

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Me.IsPostBack Then
            lblMessage.Text = GetQuantity().ToString()
        End If
    End Sub

    Private Function GetQuantity() As Integer
        Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)

            Using cmd As SqlCommand = New SqlCommand("SELECT CartonQty FROM StockDemo", con)
                con.Open()
                Return Convert.ToInt32(cmd.ExecuteScalar())
                con.Close()
            End Using
        End Using
    End Function

    Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim prequt As Integer = Convert.ToInt32(lblMessage.Text)
        Dim currentqut As Integer = Convert.ToInt32(txtCountry.Text)
        Dim totalqut As Integer = prequt - currentqut
        Dim inserted As Integer = 0
        Dim con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("constr").ConnectionString)
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        cmd.CommandText = "INSERT INTO MainWareHouse (ItemName,Category, CartonQty) VALUES(@ItemName,@Category,@CartonQty)"
        cmd.Parameters.AddWithValue("@ItemName", ddlItemName.SelectedItem.Text.Trim())
        cmd.Parameters.AddWithValue("@Category", ddlCategory.SelectedItem.Text.Trim())
        cmd.Parameters.AddWithValue("@CartonQty", totalqut)
        con.Open()
        inserted = cmd.ExecuteNonQuery()
        con.Close()
        cmd = New SqlCommand()
        cmd.CommandType = CommandType.Text
        cmd.Connection = con
        cmd.CommandText = "UPDATE StockDemo SET CartonQty=@CartonQty WHERE ItemName=@ItemName"
        cmd.Parameters.AddWithValue("@ItemName", ddlItemName.SelectedItem.Text.Trim())
        cmd.Parameters.AddWithValue("@CartonQty", totalqut)
        con.Open()
        inserted = cmd.ExecuteNonQuery()
        con.Close()
        lblMessage.Text = GetQuantity().ToString()
    End Sub

Screenshot