public string AddBook(string BookGenre, string Name,string AuthorName,string Series,string Publisher,DateTime PublishDate,int NoOfCopies,int Cost) { if (ModelState.IsValid) { AdminBookDAL dalObj = new AdminBookDAL(); AddBookViewModel bookObject = new AddBookViewModel(); bookObject = dalObj.AddBook(BookGenre,Name,AuthorName,Series,Publisher,PublishDate,NoOfCopies,Cost); if (bookObject != null) { if (bookObject.DataError== null) { ViewBag.message = "Book ID " + bookObject.BookId; } else { ViewBag.message = "-1"; } return bookObject.BookId.ToString(); } } return "false"; }
public AddBookViewModel AddBook(string BookGenre, string Name,string AuthorName,string Series,string Publisher,DateTime PublishDate,int NoOfCopies,int Cost) { //bool status = false; string conStr = ConfigurationManager.ConnectionStrings["ConnectToStopNShopDatabase"].ConnectionString; SqlConnection conn = new SqlConnection(conStr); AddBookViewModel bookObj = new AddBookViewModel(); try { conn.Open(); SqlCommand check = new SqlCommand("select count(*) from Books where BookGenre=@bookGenre and Name=@bookName and AuthorName=@authorName and Series=@series and Publisher=@publisher and PublishDate=@publishDate and NoOfCopies=@noOfCopies and Cost=@cost", conn); check.Parameters.AddWithValue("bookGenre", BookGenre); check.Parameters.AddWithValue("bookName", Name); check.Parameters.AddWithValue("authorName", AuthorName); check.Parameters.AddWithValue("series", Series); check.Parameters.AddWithValue("publisher", Publisher); check.Parameters.AddWithValue("publishDate", PublishDate); check.Parameters.AddWithValue("noOfCopies", NoOfCopies); check.Parameters.AddWithValue("cost", Cost); int num = (int)check.ExecuteScalar(); if (num == 0) { SqlCommand cmd = new SqlCommand("insert into Books values (@bookGenre,@bookName,@authorName,@series,@publisher,@publishDate,@noOfCopies,@cost)", conn); cmd.Parameters.AddWithValue("bookGenre", BookGenre); cmd.Parameters.AddWithValue("bookName", Name); cmd.Parameters.AddWithValue("authorName", AuthorName); cmd.Parameters.AddWithValue("series", Series); cmd.Parameters.AddWithValue("publisher", Publisher); cmd.Parameters.AddWithValue("publishDate", PublishDate); cmd.Parameters.AddWithValue("noOfCopies", NoOfCopies); cmd.Parameters.AddWithValue("cost", Cost); int res = cmd.ExecuteNonQuery(); //status = true; SqlCommand id = new SqlCommand("select Id from Books where BookGenre=@bookGenre and Name=@bookName and AuthorName=@authorName and Series=@series and Publisher=@publisher and PublishDate=@publishDate and NoOfCopies=@noOfCopies and Cost=@cost", conn); id.Parameters.AddWithValue("bookGenre", BookGenre); id.Parameters.AddWithValue("bookName", Name); id.Parameters.AddWithValue("authorName", AuthorName); id.Parameters.AddWithValue("series", Series); id.Parameters.AddWithValue("publisher", Publisher); id.Parameters.AddWithValue("publishDate", PublishDate); id.Parameters.AddWithValue("noOfCopies", NoOfCopies); id.Parameters.AddWithValue("cost", Cost); var reader = id.ExecuteReader(); if (reader.HasRows) { reader.Read(); bookObj.BookId = Convert.ToInt32(reader[0]); bookObj.DataError = null; } } else { bookObj = new AddBookViewModel(); bookObj.BookId = -1; } } catch (Exception exc) { HttpContext.Current.Session["ErrorMessage"] = exc.Message; } conn.Close(); return bookObj; }