locked
Filling an input textbox via data from stored procedure RRS feed

  • Question

  • User-2006371901 posted

    I have a link that has a tracking number appended to it in querystring, and I need to call up the data that correlates with that tracking number in a database table, and display that value in an input textbox. The below code works with just displaying it in an asp:label container on the aspx page, but I get "FirstName cannot be used in this context" error when trying to associate the value to its appropriate input textbox

    //////////// stored procedure

    ALTER PROCEDURE [dbo].[sp_get_JobA] (
    @track int )
    AS
    BEGIN

    SET NOCOUNT ON;
    SELECT FirstName, LastName, EmailAddy from Applicants where track = @track

    END
    /////////// codebehind inside native Page_Load function

    string qtrack = Request.QueryString["Trackid"];

    SqlCommand cmd = new SqlCommand("sp_get_JobA", new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]));
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@track", qtrack);

    cmd.Connection.Open();

    SqlDataReader reader = cmd.ExecuteReader();
    reader.Read();
    labelname1.Text = reader["FirstName"].ToString();
    /// FirstName.Text = reader["FirstName"].ToString();

    cmd.Connection.Close();

    /////////////// on aspx page
    <asp:Label ID="labelname1" runat="server" Text="Label"></asp:Label>

    <input class="form-control" name="FirstName" id="FirstName">

    ??? 
    thanks for all help
    Ned

    Tuesday, May 15, 2018 6:11 PM

Answers

  • User2103319870 posted

    but I get "FirstName cannot be used in this context" error when trying to associate the value to its appropriate input textbox

    You are using an HTML Textbox control. Add a runat="server" to html textbox and you will be able to access the control in server side

    <input class="form-control" name="FirstName" id="FirstName" runat="server"></input>

    Assign value to textbox like below

     FirstName.Value = reader["FirstName"].ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 16, 2018 1:19 AM

All replies

  • User2103319870 posted

    but I get "FirstName cannot be used in this context" error when trying to associate the value to its appropriate input textbox

    You are using an HTML Textbox control. Add a runat="server" to html textbox and you will be able to access the control in server side

    <input class="form-control" name="FirstName" id="FirstName" runat="server"></input>

    Assign value to textbox like below

     FirstName.Value = reader["FirstName"].ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 16, 2018 1:19 AM
  • User2103319870 posted

    You could also use an asp.net textbox control like below

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    Wednesday, May 16, 2018 1:22 AM