0
SqlCommand detailsCommand = new SqlCommand("SELECT bd.bookName, bd.authorName, bd.publishDate bc.description, bd.keyWords FROM bookCart as bc, bookDetails as bd WHERE bc.bookCode = bd.code");

DataTable detailsTable = book.showData(detailsCommand);

String BookName = detailsTable.Rows[0]["bookName"].ToString();
String AuthorName = detailsTable.Rows[1]["authorName"].ToString();
String PublishDate = detailsTable.Rows[2]["publishDate"].ToString();
String Describe = detailsTable.Rows[3]["describtion"].ToString();
String KeyWords = detailsTable.Rows[4]["keyWords"].ToString();

It gives me an IndexOutOfRangeException in the last two lines.

I guess it's because the describe is in another table, but I could not solve it, can you help me with this please?

janw
  • 8,758
  • 11
  • 40
  • 62
Hamas
  • 11
  • 5
  • I see several problems. 1. the field name `describtion` is spelt incorrectly. 2. There is a missing comma (`,`) between publishDate and description in the `SELECT` statement. 3. You are looking at 5 different rows for one record. – JayV Aug 02 '20 at 08:54
  • are you sure you want to read each column from different rows? – Ali Bigdeli Aug 02 '20 at 09:02

1 Answers1

0

Try next:

String BookName = detailsTable.Rows[0]["bookName"].ToString();
String AuthorName = detailsTable.Rows[0]["authorName"].ToString();
String PublishDate = detailsTable.Rows[0]["publishDate"].ToString();
String Describe = detailsTable.Rows[0]["description"].ToString();
String KeyWords = detailsTable.Rows[0]["keyWords"].ToString();

DataTable.Rows contains the collection of rows that belong to this table, so detailsTable.Rows[0] is a first row, detailsTable.Rows[1] is second one and so on (in your case it seems there are 3 of them, so you get IndexOutOfRangeException on 4th one)

Guru Stron
  • 102,774
  • 10
  • 95
  • 132