将文本框文本传递给C#中的存储过程 [英] Pass Textbox Text to Stored Procedure in C#

查看:84
本文介绍了将文本框文本传递给C#中的存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,所有!



我正在整合一个销售点系统,该系统将与我们这里已有的电子商务网站一起使用。有一个SQL Server表(产品),其中包含我们销售的项目的记录。表中的一列是SKU,其中包含我们独特的产品编号。



当有人在销售屏幕上的文本框中输入SKU编号时,我想执行查询Product表的存储过程,并告诉我SKU是否存在。如果是,它将被添加到客户的收据中;如果不是,它会回复一个错误,告诉该人再次尝试进入SKU。



如果我在SQL中执行存储过程,它可以正常工作。如果SKU存在,则返回值10,否则返回30。它完成它应该做的事情。如果我使用SKU从我的脚本执行存储过程,用户进入文本框,它总是返回30(SKU不存在)。如果我将一个好的SKU硬编码到我的脚本中它返回10并且工作正常。它似乎没有将文本输入我的文本框。



非常感谢您的帮助!



我的存储过程:



Hello, all!

I'm putting together a point-of-sale system that will work in conjunction with an already working eCommerce website we have here. There is a SQL Server table (Product) that contains records of items that we sell. One of the columns in the table is SKU which contains the our unique product number.

When someone enters a SKU number into a textbox on the sale screen I want to execute a stored procedure to query the Product table and tell me whether the SKU exists or not. If it does it will be added to the customer's receipt; if it isn't it will kick back an error telling the person to try entering the SKU again.

If I execute the stored procedure within SQL it works fine. It returns a value of 10 if the SKU exists and 30 if it doesn't. It does just what it's supposed to do. If I execute the stored procedure from my script using the SKU the user enters into the textbox it always returns 30 (SKU doesn't exist). If I hard-code a good SKU into my script it returns 10 and works fine. It doesn't seem to be taking the text entered into my textbox.

Thanks so much for any help!

My stored procedure:

USE [SchoolStoreWeb]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE  PROCEDURE [dbo].[ochs_Validate_SKU]
	@EnteredSKU NVARCHAR(50)
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @SKU NVARCHAR(50)

	SELECT @SKU = SKU FROM PRODUCT WHERE SKU = @EnteredSKU

	IF @SKU IS NOT NULL
		BEGIN
		INSERT INTO ochs_InvoiceDetails (SKU) VALUES (@SKU)
		SELECT 10 --SKU Does Exist in main product table
		END
	ELSE
		BEGIN
		SELECT 30 --SKU Does NOT Exist in any table
		END
END	
GO







我的代码:






My Code:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="SaleScreen.aspx.cs" Inherits="SaleScreen" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h2>Sale</h2>
    <p style="text-align: left">
        <asp:Label ID="lblInvoiceNumber" runat="server" Text=""></asp:Label>
        <asp:HiddenField ID="hfInvoiceNumber" runat="server" Value="" />
    </p>
    <table class="sellscreen-main-table">
        <tr>
            <td style="width:25px"> </td>
            <td style="width:75px">SKU</td>
            <td style="width:425px">Description</td>
            <td style="width:30px">Qty</td>
            <td style="width:50px">Price</td>
            <td style="width:50px">Total</td>
            <td style="width:25px"> </td>
        </tr>

        <asp:Repeater ID="ItemDetailRepeater" runat="server">
            <ItemTemplate>

            </ItemTemplate>
        </asp:Repeater>
        <tr>
            <td></td>
            <td><asp:TextBox ID="txtSKU" runat="server" Text="" CssClass="sku-textbox"></asp:TextBox><asp:Button ID="btnSKUsubmit" runat="server" Text="" OnClick="btnSKUsubmit_Click" /></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </table>

    <asp:Label ID="lblErrorMessage" runat="server" Text="" CssClass="ErrorMessage"></asp:Label>

</asp:Content>





代码背后:





Code Behind:

protected void btnSKUsubmit_Click(object sender, EventArgs e)
    {
        //Check to see if SKU exists. If it does add the item to Invoice Details.
        int SKUExist = 0;
        string constr = ConfigurationManager.ConnectionStrings["csPOS"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("ochs_Validate_SKU"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@EnteredSKU", txtSKU.Text.Trim());
                cmd.Connection = con;
                con.Open();
                SKUExist = (int)cmd.ExecuteScalar();
                con.Close();
                lblErrorMessage.Text = SKUExist.ToString();
            }
        }
    }

推荐答案

问题是你总是覆盖改变的值在Page_Load中。相反,请查看 IsPostBack [ ^ ]属性:



你在Page_Load方法中应该做的是这样的:



The problem is that you are always overwriting the changed value in Page_Load. Instead, check the IsPostBack [^]property:

What you should do in your Page_Load method is something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        txtSKU.Text = "";
    }
}


这篇关于将文本框文本传递给C#中的存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆