public OpenBook(Book book, SearchBooks searchBooks) { InitializeComponent(); THIS = this; username = MainWindow.THIS.UsernameTop.Text.Trim(); if (username == "admin") { RedStack.Visibility = Visibility.Visible; } this.searchBooks = searchBooks; string a = searchBooks.SearchTextBox.Text; this.book = book; BookTitle.Text = book.BookName; Author.Text = book.Author; Country.Text = book.Country; Year.Text = Convert.ToString(book.GodIzdaniya); Genre.Text = book.Genre; Disc.Text = book.Discription; string z = "pack://application:,,,/Resources/" + book.Picture; Picture.Source = new BitmapImage(new Uri(z)); try { using (SqlConnection con = new SqlConnection(ConnectionString)) { con.Open(); string CmdString = "select AVG(value) from Rating where book_id =" + book.Id; SqlCommand cmd = new SqlCommand(CmdString, con); string value = cmd.ExecuteScalar().ToString(); if (value.Length > 3) { value = value.Substring(0, 3); } RateBlock.Text = value; } using (SqlConnection con = new SqlConnection(ConnectionString)) { con.Open(); string Cmd = "SELECT (select COUNT(*) from Rating where book_id = " + book.Id + " and value = 1)[1]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 2)[2]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 3)[3]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 4)[4]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 5)[5]," + "(select COUNT(*) from Rating where book_id = " + book.Id + ")[count]"; SqlCommand command = new SqlCommand(Cmd, con); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если есть данные { reader.Read(); Rating rating = new Rating(Convert.ToInt32(reader.GetValue(0)), Convert.ToInt32(reader.GetValue(1)), Convert.ToInt32(reader.GetValue(2)), Convert.ToInt32(reader.GetValue(3)), Convert.ToInt32(reader.GetValue(4)), Convert.ToInt32(reader.GetValue(5))); if (rating.total == 0) { NoRatesPanel.Visibility = Visibility.Visible; RatesPanel.Visibility = Visibility.Collapsed; } else { int[] array = new int[] { rating.five, rating.four, rating.three, rating.two, rating.one }; int[] sorted = (from element in array orderby element ascending select element).ToArray(); double part = 150 / (double)sorted[4]; Five.Width = (double)rating.five * part; Four.Width = (double)rating.four * part; Three.Width = (double)rating.three * part; Two.Width = (double)rating.two * part; One.Width = (double)rating.one * part; } } reader.Close(); } } catch (Exception err) { NoRatesPanel.Visibility = Visibility.Visible; RatesPanel.Visibility = Visibility.Collapsed; } try { using (SqlConnection con = new SqlConnection(ConnectionString)) { con.Open(); string CmdString = "select username, comment, value from Rating where book_id = " + book.Id; SqlCommand cmd = new SqlCommand(CmdString, con); SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) // если есть данные { while (reader.Read()) { CommentItem item = new CommentItem(Convert.ToString(reader.GetValue(0)), Convert.ToString(reader.GetValue(1)), Convert.ToInt32(reader.GetValue(2))); CommentPanel.Children.Add(item); } } reader.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void SendRateBtn_OnClick(object sender, RoutedEventArgs e) { try { using (SqlConnection con2 = new SqlConnection(ConnectionString)) { con2.Open(); string CmdString = "insert into Rating(username, book_id, value, comment)values('" + username + "'," + book.Id + "," + currentset + ",'" + Comment.Text + "')"; SqlCommand cmd2 = new SqlCommand(CmdString, con2); cmd2.ExecuteNonQuery(); AddComOk.IsOpen = true; CommentPanel.Children.Add(new CommentItem(username, Comment.Text, currentset)); Comment.Text = ""; for (int i = 1; i <= 5; i++) { PackIcon z = Dev.FindChild <PackIcon>(THIS, "Icon_" + Convert.ToString(i)); z.Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF44404D")); } } } catch (Exception) { AddComFail.IsOpen = true; } try { using (SqlConnection con = new SqlConnection(ConnectionString)) { con.Open(); string CmdString = "select AVG(value) from Rating where book_id =" + book.Id; SqlCommand cmd = new SqlCommand(CmdString, con); string value = cmd.ExecuteScalar().ToString(); if (value.Length > 3) { value = value.Substring(0, 3); } RateBlock.Text = value; } using (SqlConnection con = new SqlConnection(ConnectionString)) { con.Open(); string Cmd = "SELECT (select COUNT(*) from Rating where book_id = " + book.Id + " and value = 1)[1]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 2)[2]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 3)[3]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 4)[4]," + "(select COUNT(*) from Rating where book_id = " + book.Id + " and value = 5)[5]," + "(select COUNT(*) from Rating where book_id = " + book.Id + ")[count]"; SqlCommand command = new SqlCommand(Cmd, con); SqlDataReader reader = command.ExecuteReader(); if (reader.HasRows) // если есть данные { reader.Read(); Rating rating = new Rating(Convert.ToInt32(reader.GetValue(0)), Convert.ToInt32(reader.GetValue(1)), Convert.ToInt32(reader.GetValue(2)), Convert.ToInt32(reader.GetValue(3)), Convert.ToInt32(reader.GetValue(4)), Convert.ToInt32(reader.GetValue(5))); if (rating.total == 0) { Five.Width = Four.Width = Three.Width = Two.Width = One.Width = 0; } else { int[] array = new int[] { rating.five, rating.four, rating.three, rating.two, rating.one }; int[] sorted = (from element in array orderby element ascending select element).ToArray(); double part = 175 / (double)sorted[4]; Five.Width = (double)rating.five * part; Four.Width = (double)rating.four * part; Three.Width = (double)rating.three * part; Two.Width = (double)rating.two * part; One.Width = (double)rating.one * part; } } reader.Close(); if (NoRatesPanel.Visibility == Visibility.Visible) { NoRatesPanel.Visibility = Visibility.Collapsed; RatesPanel.Visibility = Visibility.Visible; } } } catch (Exception err) { NoRatesPanel.Visibility = Visibility.Visible; RatesPanel.Visibility = Visibility.Collapsed; } }