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