如何在asp.net c中将数据库中的值赋给标签或文本框#

时间:2012-08-14 14:21:48

标签: c#-4.0

我使用以下方法为标签或文本框分配值: 首先我使用dropdownlist然后我使用数据绑定然后我将dropdownlist值分配给标签或文本框。

<asp:DropDownList ID="DropDownList1" runat="server" 
        DataSourceID="A" DataTextField="Regisno" DataValueField="Regisno">
    </asp:DropDownList>

 <asp:AccessDataSource ID="A" runat="server" DataFile="~/App_Data/Database.mdb" 
            SelectCommand="SELECT [Regisno] FROM [tblsignupv] WHERE ([Email] = ?)">
            <SelectParameters>
                <asp:SessionParameter Name="Email" SessionField="User" Type="String" />
            </SelectParameters>
        </asp:AccessDataSource>

我想知道一种使用c#code

直接从数据库为标签或文本框赋值的方法 提前thnx

1 个答案:

答案 0 :(得分:0)

如果您的组合框已填充数据,则只需使用以下选项即可选择项目:

DropDownList1.SelectedValue="123";

如果您的值来自数据库,则必须使用以下内容:

SqlCommand comm = new SqlCommand();
comm.Connection = new SqlConnection("Data Source=(local);Initial Catalog=[db];user=[user];password=[pwd];");
String sql = @"SELECT [Regisno] FROM [tblsignupv] WHERE ([Email] = '[emailaddress]')";
comm.CommandText = sql;
comm.Connection.Open();
SqlDataReader cursor = comm.ExecuteReader();
while (cursor.Read())
{
     // add your option to the combobox here
     customerID.Items.Add(new ListItem(cursor["text"].ToString(), cursor["value"].ToString()));
}
comm.Connection.Close();

当然,您需要将“text”和“value”更改为数据库的列。

相关问题