Asp.net 数据绑定:';System.Data.DataRowView';不包含名为';书名'; 显而易见的问题很明显:您确定表BookDetails包含名为BookName的字段吗?拼写中没有下划线或其他变体?此外,如果在Selec
数据绑定:';System.Data.DataRowView';不包含名为';书名';Asp.net 数据绑定:';System.Data.DataRowView';不包含名为';书名'; 显而易见的问题很明显:您确定表BookDetails包含名为BookName的字段吗?拼写中没有下划线或其他变体?此外,如果在Selec,asp.net,Asp.net,数据绑定:';System.Data.DataRowView';不包含名为';书名'; 显而易见的问题很明显:您确定表BookDetails包含名为BookName的字段吗?拼写中没有下划线或其他变体?此外,如果在SelectedIndexChanged事件处理程序的查询中使用DropDownList的SelectedValue,可能会得到更可靠的结果。两本书可能有相同的标题并非不可能,但是——除非你有一个非常不标准的实现——它们不会有相同的BookId值。 pu
显而易见的问题很明显:您确定表
BookDetails
包含名为BookName
的字段吗?拼写中没有下划线或其他变体?此外,如果在SelectedIndexChanged
事件处理程序的查询中使用DropDownList
的SelectedValue
,可能会得到更可靠的结果。两本书可能有相同的标题并非不可能,但是——除非你有一个非常不标准的实现——它们不会有相同的BookId
值。
public partial class _bookstore : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
if (Page.IsPostBack == false)
{
// SqlConnection cn = new SqlConnection("trusted_connection=true;" +
// "database=Test1; " +
// "server=.\\sqlexpress");
SqlConnection cn = new SqlConnection("trusted_connection=true;Database=Test1;server=.\\sqlexpress;");
cn.Open();
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from dbo.BookDetails", cn);
da.Fill(ds, "e");
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataValueField = "BookId";
DropDownList1.DataTextField = "BookName";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("--Select--", "-1"));
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection("trusted_connection=true;Database=Test1;server=.\\sqlexpress;");
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
cn.Open();
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from dbo.BookDetails where BookName='" + DropDownList1.SelectedItem.Text + "'";
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr["BookId"].ToString();
TextBox2.Text = dr["BookAuthorName"].ToString();
TextBox3.Text = dr["Address"].ToString();
}
else
{
Label1.Text = "Please select Book";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
}
}
}