本文介绍了从SQL查询到文本框获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过在页面初始化关闭引黄联接,然后重新打开在DROPDOWNLIST固定它。
我搜索一个回答这个问题,但没有任何运气。
我想从选定的DropDownList项数据到一个文本框。我一直没有任何成功执行该SQL查询。
下面是我的code:
公共部分类EditContact:System.Web.UI.Page
{
SqlConnection的连接=新的SqlConnection(SqlConnection的);
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ }
保护无效Page_Init(对象发件人,EventArgs的发送)
{
connection.Open();
的SqlCommand SqlCommandDD =新的SqlCommand(SELECT名字+''+姓氏AS文本字段,CONTACT_ID,电子邮件,******中国,CompanyID FROM ContactPerson);
SqlCommandDD.Connection =连接; DropDownList2.DataSource = SqlCommandDD.ExecuteReader();
DropDownList2.DataValueField =CONTACT_ID;
DropDownList2.DataTextField =文本字段;
DropDownList2.DataBind(); } 保护无效DropDownList2_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
字符串fNameTemp = DropDownList2.SelectedValue;
字符串的SQLQuery =(选择firstname和ContactPerson WHERE(CONTACT_ID =+ fNameTemp +)); 的SqlCommand命令=新的SqlCommand(sqlquery的,连接); SqlDataReader的SDR = Command.ExecuteReader却(); fNameTextBox.Text = sdr.ToString(); }
}
解决方案
在的ExecuteReader返回复杂/非标量值。使用方法,而不是
的SqlCommand命令=新的SqlCommand(sqlquery的,连接);
fNameTextBox.Text = command.ExecuteScalar()的ToString()。
Fixed it by closing the connetion at page init and then reopen at Dropdownlist.
I've searched for an answer to this question but without any luck.
I want to get data from a selected dropdownlist item to a textbox. I've been trying to execute this sql query without any success.
Here's my code:
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();
}
}
解决方案
The "ExecuteReader" returns complex/non-scalar value. Use the "ExecuteScalar" method instead:
SqlCommand command = new SqlCommand(sqlquery, connection);
fNameTextBox.Text = command.ExecuteScalar().ToString();
这篇关于从SQL查询到文本框获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!