select * from Order a inner join Book b on a.BookID=b.id
--------------------编程问答--------------------
if (reader.Read())
改成while
--------------------编程问答--------------------
OrderForm order = null;
if (reader.Read()) //这里要用While读取,reader.Read()每次 读取一条记录
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
}
IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );//order 是null的话,会出错,即使判断了,这里也只是最有一个order的book显示了。如果每个order只有一本书,放到前面的{}里面,否则的话可以用主从表,即先只显示Order,用户选择一个Order是,在显示Books
this.DataList1.DataSource = book;
this.DataList1.DataBind();
--------------------编程问答--------------------
大神们说的是对的,+1
--------------------编程问答--------------------
if (reader.Read())这样是不行的
要用while 并且里面用List<T> .add 存起来
--------------------编程问答--------------------
假如说UserID对应的没有BookID,VS会报错,是因为 order.BookID = (int)reader["BookID"];你BookID为空的话,转换为整型肯定会报错的!
--------------------编程问答--------------------
错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
//SqlDataReader reader = DBHelper.GetReader(sql);
//OrderForm order = null;
//if (reader.Read())
//{
//order = new OrderForm();
//order.BookID = (int)reader["BookID"];
//}
IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
this.DataList1.DataSource = book;
this.DataList1.DataBind();
--------------------编程问答--------------------
..
IList<Book> book = new List<Book>(); //这里创建一个集合
if (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
...
//这里必须往集合里加数据,而不是在外面加。否则你加的永远是最后一条记录
}
--------------------编程问答--------------------
哦没看仔细,确实需要循环,这里只是读了一次而已
--------------------编程问答--------------------
Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
SqlDataReader reader = DBHelper.GetReader(sql);
OrderForm order = null;
if (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
}
IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
this.DataList1.DataSource = book;
this.DataList1.DataBind();
你这样写根本不对,你想获得Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
SqlDataReader reader = DBHelper.GetReader(sql);
OrderForm order = null;
if (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
}
IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
this.DataList1.DataSource = book;
this.DataList1.DataBind();
你这样写更本就不对,你想获得UserID下的BookID的信息。你可以这样修改:
Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
SqlDataReader reader = DBHelper.GetReader(sql);
OrderForm order = null;
if (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
}
IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
this.DataList1.DataSource = book;
this.DataList1.DataBind();
你这样写根本不对,你想获得Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
SqlDataReader reader = DBHelper.GetReader(sql);
OrderForm order = null;
IList<Book> book = new List<Book>();
if (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
book.Add(order);
}
//IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
this.DataList1.DataSource = book;
this.DataList1.DataBind();
这样应该就可以了。
--------------------编程问答--------------------
IList<Book> booklist=new new List<Book>();
while(reader.Read())
{
Book book=new Book();
order = new OrderForm();
order.BookID = (int)reader["BookID"];
book = BookManage.BookManager.GetBookBybookId(order .BookID );
booklist.add(book);
}
this.DataList1.DataSource = booklist;
this.DataList1.DataBind();
--------------------编程问答--------------------
Users user = (Users)Session["user"];
Users u = new Users();
u.UserID = user.UserID;
string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
SqlDataReader reader = DBHelper.GetReader(sql);
OrderForm order = null;
while (reader.Read())
{
order = new OrderForm();
order.BookID = (int)reader["BookID"];
}
IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
if(book!=null)
{
this.DataList1.DataSource = book;
this.DataList1.DataBind();
}