Hi Micah,
Please refer below Sample.
HTML
<table>
<tr>
<th>Item</th>
<td>
<asp:TextBox runat="server" ID="txtItem" /></td>
</tr>
<tr>
<th>Qty</th>
<td><asp:TextBox runat="server" ID="txtQty" /></td>
</tr>
<tr>
<td >
<asp:Button Text="Update" runat="server" ID="btnUpdate" OnClick="OnUpdate" /></td>
</tr>
</table>
<hr />
<asp:GridView runat="server" ID="GvStockUpdate" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Qty" HeaderText="Qty" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT Item, Qty FROM StockUpdate", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GvStockUpdate.DataSource = dt;
GvStockUpdate.DataBind();
}
}
}
}
}
protected void OnUpdate(object sender, EventArgs e)
{
string availableQuantity = "";
string conn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT Qty FROM StockUpdate WHERE Item = @item", con))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@item", txtItem.Text);
con.Open();
availableQuantity = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
if (string.IsNullOrEmpty(availableQuantity))
{
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("INSER INTO StockUpdate (Item, Qty) VALUES (@item, @Qty)", con))
{
cmd.Parameters.AddWithValue("@item", txtItem.Text);
cmd.Parameters.AddWithValue("@Qty", txtQty.Text);
con.Open();
availableQuantity = Convert.ToString(cmd.ExecuteScalar());
}
}
}
else
{
int qty = Convert.ToInt32(availableQuantity) + Convert.ToInt32(txtQty.Text);
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand("UPDATE StockUpdate SET Qty = @Qty WHERE Item = @Item", con))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@Item", txtItem.Text);
cmd.Parameters.AddWithValue("@Qty", qty);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Private Sub BindGrid()
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT Item, Qty FROM StockUpdate", con)
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GvStockUpdate.DataSource = dt
GvStockUpdate.DataBind()
End Using
End Using
End Using
End Using
End Sub
Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
Dim availableQuantity As String = ""
Dim conn As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("SELECT Qty FROM StockUpdate WHERE Item = @item", con)
cmd.Connection = con
cmd.Parameters.AddWithValue("@item", txtItem.Text)
con.Open()
availableQuantity = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
If String.IsNullOrEmpty(availableQuantity) Then
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("INSER INTO StockUpdate (Item, Qty) VALUES (@item, @Qty)", con)
cmd.Parameters.AddWithValue("@item", txtItem.Text)
cmd.Parameters.AddWithValue("@Qty", txtQty.Text)
con.Open()
availableQuantity = Convert.ToString(cmd.ExecuteScalar())
End Using
End Using
Else
Dim qty As Integer = Convert.ToInt32(availableQuantity) + Convert.ToInt32(txtQty.Text)
Using con As SqlConnection = New SqlConnection(conn)
Using cmd As SqlCommand = New SqlCommand("UPDATE StockUpdate SET Qty = @Qty WHERE Item = @Item", con)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Parameters.AddWithValue("@Item", txtItem.Text)
cmd.Parameters.AddWithValue("@Qty", qty)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Using
End If
End Sub
Screenshot
SQL