简体   繁体   中英

Retrieving data into textbox from database

The text of Label1 doesn't change, what could be the cause of this issue?

try
{
    string connectionString = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = db1; Integrated Security = True";
    SqlConnection cnn = new SqlConnection(connectionString);

    string query = "SELECT RNO FROM TABLE1 WHERE RNO='" + PRrno.Text + "'";

    SqlCommand cd = new SqlCommand(query, cnn);
    cnn.Open();

    SqlDataReader reader = cd.ExecuteReader();

    while (reader.Read())
    {
        Label1.Text = reader["RNO"].ToString();
    }

    reader.Close();
    cnn.Close();
}

Gaurav Thapa, like I said in one of the comments, make sure in the aspx file your elements have runat="server" also put all the elements you want to get refreshed inside an Ajax Update Panel

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>UpdatePanel</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Always">        
            <ContentTemplate>
                <asp:Label runat="server" id="PRrno" />
            </ContentTemplate>
        </asp:UpdatePanel>       
    </form>
</body>
</html>

You have to get query output in data table and then from there convert that DataTable value to string. Check if you have access to your label.

Label.Text = // String that you converted from DataTable

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