日期:2014-05-18  浏览次数:20430 次

请问这是怎么回事啊
我基本按照书上写的,在网页上创建一个LISTBOX控件,想在里面选一个,就在网页上从数据库里显示这条记录出来,代码如下
public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string str = "server=localhost;database=login;uid=sa;pwd=";
SqlConnection objConnection = new SqlConnection(str);
SqlCommand objCommand1 = new SqlCommand("select * from personLike", objConnection);
objConnection.Open();

ibx.DataSource = objCommand1.ExecuteReader();
ibx.DataTextField = "likeContent";
ibx.DataValueField = "id";
ibx.DataBind();

objConnection.Close();
}


}


protected void sub(object sender, EventArgs e)
{



string str1 = "server=localhost;database=login;uid=sa;pwd=";
SqlConnection objConnection = new SqlConnection(str1);
string ab = "select * from personLike where id=ibx.SelectedItem.value"; 
SqlCommand objCommand = new SqlCommand(ab, objConnection);
objConnection.Open();
SqlDataReader objReader = objCommand.ExecuteReader();
DataGrid1.DataSource = objReader;

DataGrid1.DataBind();

}
}

运行后LISTBOX出来了

但是我按了里面随便一条,出现错误:
“/WebSite28”应用程序中的服务器错误。
--------------------------------------------

列前缀 'ibx.SelectedItem' 与查询中所用的表名或别名不匹配。



------解决方案--------------------
string ab = "select * from personLike where id=ibx.SelectedItem.value";
这句有问题
应该是string ab = "select * from personLike where id='"+ibx.SelectedItem.value+"'";