Reputation: 43
I've been trying to select into database with three condition and the two are from the same column.
I have a flowlayoutpanel with usercontrol and I can't seem to view all data with different value from the same column.
I have tried
SELECT borrowID, studentID, bookTitle, dateBorrowed, dueDate, status FROM tblBorrowedBook WHERE status = 'Not Returned' AND status = 'Overdue' AND studentNum = @studentNum
but sadly doesn't work.
I have also tried
public void BooksOnHand()
{
ucBooksOnHand uc = new ucBooksOnHand(this);
flowLayoutPanel1.Controls.Clear();
cn.Open();
cm = new SqlCommand("SELECT borrowID, studentID, bookTitle, dateBorrowed, dueDate, status FROM tblBorrowedBook WHERE status = 'Not Returned' AND studentNum = @studentNum", cn);
cm.Parameters.AddWithValue("@studentNum", frmissue.lblStudNo.Text);
dr = cm.ExecuteReader();
while (dr.Read())
{
//ucBooksOnHand uc = new ucBooksOnHand(this);
uc.Dock = DockStyle.Top;
uc.lblBorrowID.Text = dr["borrowID"].ToString();
uc.lblStudentID.Text = dr["studentID"].ToString();
uc.lblBookName.Text = dr["bookTitle"].ToString();
uc.lblDateBorrowed.Text = Convert.ToDateTime(dr["dateBorrowed"]).ToString("MM/dd/yyyy");
uc.lblDueDate.Text = Convert.ToDateTime(dr["dueDate"]).ToString("MM/dd/yyyy");
uc.lblStatus.Text = dr["status"].ToString();
uc.dtDueDate.Value = Convert.ToDateTime(dr["dueDate"]);
flowLayoutPanel1.Controls.Add(uc);
}
dr.Close();
cn.Close();
}
public void BooksOverdue()
{
cn.Open();
cm = new SqlCommand("SELECT borrowID, bookTitle, studentID, dateBorrowed, dueDate, status FROM tblBorrowedBook WHERE status = 'Overdue' AND studentNum = @studentNum", cn);
cm.Parameters.AddWithValue("@studentNum", frmissue.lblStudNo.Text);
dr = cm.ExecuteReader();
while (dr.Read())
{
ucBooksOnHand uc = new ucBooksOnHand(this);
uc.Dock = DockStyle.Top;
uc.lblBorrowID.Text = dr["borrowID"].ToString();
uc.lblStudentID.Text = dr["studentID"].ToString();
uc.lblBookName.Text = dr["bookTitle"].ToString();
uc.lblDateBorrowed.Text = Convert.ToDateTime(dr["dateBorrowed"]).ToString("MM/dd/yyyy");
uc.lblDueDate.Text = Convert.ToDateTime(dr["dueDate"]).ToString("MM/dd/yyyy");
uc.lblStatus.Text = dr["status"].ToString();
uc.dtDueDate.Value = Convert.ToDateTime(dr["dueDate"]);
flowLayoutPanel1.Controls.Add(uc);
}
dr.Close();
cn.Close();
}
The above code worked for me but I am looking for a better and simple solution for my problem.
Here are some data from the table
borrowID studentID bookID studentNum bookTitle dateBorrowed dueDate returnedDate status
10 1 1 2020-01-0001 Worthless 2020-03-30 2020-04-06 Mar 30 2020 Returned
11 1 1 2020-01-0001 Worthless 2020-03-23 2020-03-27 Mar 30 2020 Returned
12 1 2 2020-01-0001 Heartless 2020-03-30 2020-04-06 Not Returned
13 1 1 2020-01-0001 Worthless 2020-03-15 2020-03-28 Overdue
And I want to show all the books in possession of a student that is still not returned and is overdue.
Sample output that I want:
Books In Possesion:
Book Name Status
----------|-------------
Heartless | Not Returned
Worthless | Overdue
Upvotes: 0
Views: 49
Reputation: 9650
You can't have an AND
on the same column with two different values. You need to use an OR
for these in brackets like this:
SELECT ... WHERE (status = 'Not Returned' OR status = 'Overdue') AND studentNum = @studentNum
Another maybe better method is to use the IN operator. Certainly better if you have more than two items to check
SELECT ... WHERE status IN('Not Returned', 'Overdue') AND studentNum = @studentNum
Upvotes: 4