Dear Micah,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<table>
<tr>
<td>Product Name:</td>
<td><asp:TextBox runat="server" ID="txtName"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td>Quantity:</td>
<td><asp:TextBox runat="server" ID="txtQty"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td><asp:Button Text="Submit" ID="btnSubmit" runat="server" OnClick="OnSubmit" /></td>
</tr>
</table>
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 OnSubmit(object sender, EventArgs e)
{
int availableQuantity = 0;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT UnitsInStock FROM Products WHERE ProductName = @name";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@name", txtName.Text);
con.Open();
availableQuantity = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
if (availableQuantity >= Convert.ToInt32(txtQty.Text))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('Data Submitted Successfully ... !!')", true);
}
else
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('No of Quantity entered is not available in Stock')", true);
}
}
}
VB.Net
Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
Dim availableQuantity As Integer = 0
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Dim query As String = "SELECT UnitsInStock FROM Products WHERE ProductName = @name"
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Connection = con
cmd.Parameters.AddWithValue("@name", txtName.Text)
con.Open()
availableQuantity = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()
End Using
If availableQuantity >= Convert.ToInt32(txtQty.Text) Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('Data Submitted Successfully ... !!')", True)
Else
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "", "alert('No of Quantity entered is not available in Stock')")
End If
End Using
End Sub
Screenshot
SQL
Output