简体   繁体   中英

Passing String through C# Code Behind to SQL in .ASPX

I am pretty new at working with this kind of thing so forgive me if my question seems too easy. I have been trying for a while at this and have searched through every relevant article I can find but I cannot get my code to work.

I want to pass a string from 'BadgeNum.Text' through a parameter on the other side and have it execute a query. I cannot get the value to pass correctly and thus cannot get any records to appear out of my query. I have tested the query multiple times using the hard-coded data and it works fine.

Any help here would be greatly appreciated.

<asp:SqlDataSource ID="GridDataSource" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Oracle1TestString %>" 
            ProviderName="<%$ ConnectionStrings:Oracle1TestString.ProviderName %>" 
            SelectCommand="SELECT MODEL_NUMBER, SERIAL_NUMBER, DEFECT_CODE, RECORD_DATE, RECORD_TIME, PRODUCTION_DATE, AUDIT_TYPE FROM AUDITENT WHERE AUDITOR_BADGE = @BadgeNum" 
            onselecting="GridDataSource_Selecting">

            <SelectParameters>
                <asp:Parameter Name="BadgeNum" Direction="Input" DbType="String" />
            </SelectParameters>
        </asp:SqlDataSource>

Here is the code behind (C#):

protected void Page_Load(object sender, EventArgs e) {

        BadgeNum.Text = "0205096";
        Parameter badge = new Parameter();
        badge.DefaultValue = BadgeNum.Text;
        badge.Type = TypeCode.String;
        badge.Name = "BadgeNum";
        GridDataSource.SelectParameters.Add(badge);
        GridDataSource.DataBind();
}

OK then is your prefix possibly wrong in your select statement

@BadgeNum

vs

:BadgeNum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM