Display data from database in an input field

Donald Symmons 3,066 Reputation points
2023-12-27T00:57:29.0966667+00:00

Is it possible to display data fetched from database into an input field?

I know how to display data from database in a Textbox, but this time I want to learn and know if the same can be done using an input field. I tried it but it did not work.

I tried (.Text) and (.InnerText), both did not work.

<input type="email" runat="server" id="email_address" class="form-control" style="width: 100%;" />

public void Showdata1()
        {
            try
            {
                using (SqlConnection con = new SqlConnection())
                {
                    con.ConnectionString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "SELECT * FROM myTable WHERE Id = @Id";
                        cmd.Parameters.AddWithValue("@Id", Session["user"]);
                        cmd.Connection = con;
                        con.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        if (dr.Read())
                        {
                            email_address.Text = dr["email"].ToString();
                           // email_address.InnerText = dr["email"].ToString();
                        }
                        con.Close();
                    }
                }
            }
            catch (SqlException ex)
            {
                string msg = "Error:";
                msg += ex.Message;
                throw new Exception(msg);
            }
        }

Developer technologies | .NET | Other
Developer technologies | ASP.NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2023-12-27T01:55:55.6433333+00:00

    Hi @Donald Symmons,

    <input> sets the text value through the Value attribute.

      email_address.Value= dr["email"].ToString();
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.