gpt4 book ai didi

c# - 从sql查询中获取数据到文本框中

转载 作者:行者123 更新时间:2023-11-30 16:25:33 24 4
gpt4 key购买 nike

通过在页面初始化时关闭连接然后在下拉列表中重新打开来修复它。

我一直在寻找这个问题的答案,但没有任何运气。

我想从选定的下拉列表项中获取数据到文本框。我一直在尝试执行此 sql 查询,但没有成功。

这是我的代码:

    public partial class EditContact : System.Web.UI.Page
{
SqlConnection connection = new SqlConnection("SqlConnection");
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Page_Init(object sender, EventArgs e)
{
connection.Open();
SqlCommand SqlCommandDD = new SqlCommand("SELECT FirstName + ' ' + LastName AS 'TextField', Contact_ID, Email, PhoneNumber, CompanyID FROM ContactPerson");
SqlCommandDD.Connection = connection;

DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
DropDownList2.DataValueField = "Contact_ID";
DropDownList2.DataTextField = "TextField";
DropDownList2.DataBind();

}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string fNameTemp = DropDownList2.SelectedValue;


string sqlquery = ("SELECT FirstName FROM ContactPerson WHERE (Contact_ID = " + fNameTemp + ")");

SqlCommand command = new SqlCommand(sqlquery, connection);

SqlDataReader sdr = command.ExecuteReader();

fNameTextBox.Text = sdr.ToString();

}


}

最佳答案

尝试这样修改代码:

string sqlquery = "SELECT FirstName FROM ContactPerson WHERE Contact_ID = " + fNameTemp;

SqlCommand command = new SqlCommand(sqlquery, connection);

SqlDataReader sdr = command.ExecuteReader();

while ( sdr.Read() )
{
fNameTextBox.Text = sdr[ "FirstName" ].ToString();
}

关于c# - 从sql查询中获取数据到文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9761545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com