I am working in ASP.NET using C# and I am struggling to finish the piece of code.
A dropdownlist is displayed on my page on which the user can give a rating for a book on the page.
First, I want to check if the user already gave a rating or not (if the rating column has a value).
If it doesn't have a value, the user can then do the rating by selecting from the dropwdownlist.
Here is my code so far. I am unsure what to write within the if()
// CRUD statement
SqlCommand cmdCheck = ratingconn.CreateCommand();
cmdCheck.CommandText = "SELECT bookRating FROM tbl_ratingInfo WHERE userID = '" + Session["userID"] + "'";
if()
{
// reading the information from the database
SqlDataReader reader = cmdCheck.ExecuteReader();
if (reader.Read())
{
// setting the label text values
ddl_BookName.Text = reader.GetInt32(0).ToString();
}
}
else
{
// creating CRUD statement
SqlCommand cmdRating = ratingconn.CreateCommand();
cmdRating.CommandText = "INSERT INTO tbl_ratingInfo (bookRating) VALUES('"
+ ddl_Rating.Text + "') WHERE userID = " + Session["userID"] + "' ";
}
Here is my database. This table is an intersection table in SQL code.
-- Create the rating info table
CREATE TABLE tbl_ratingInfo
(
-- Add Foreign Keys from members and class tables
bookTitle VARCHAR (100) NOT NULL REFERENCES tbl_bookInfo(bookTitle),
userID INT NOT NULL REFERENCES tbl_userInfo(userID),
bookRating INT NOT NULL DEFAULT 5 CHECK (bookRating <= 5),
-- Composite Primary Key
PRIMARY KEY (bookTitle, userID)
)
GO