Book Class from HelperLibrary.Models.Book.cs
public class Book
{
public string Title;
public string Author;
public string ISBN;
public Book(string title, string author, string iSBN)
{
Title = title;
Author = author;
ISBN = iSBN;
}
}
Call
private void SaveChanges_btn_Click(object sender, RoutedEventArgs e)
{
List<HelperLibrary.Models.Book> NewUsersBooks = new List<HelperLibrary.Models.Book>();
foreach (var x in UserBooks_List.Items)
{
foreach(HelperLibrary.Models.Book y in App.GlobalBookList)
{
if (y.ISBN == x.ToString())
{
NewUsersBooks.Add(y);
}
}
}
HelperLibrary.Helpers.SQLHelper.AddBookToUser(App.GlobalUserList[UserList_List.SelectedIndex], NewUsersBooks);
}
Sql call from HelperLibrary.SqlHelper.cs
public static void AddBookToUser(Models.User user, List<Models.Book> NewBooks)
{
List<Models.Book> OnlineUsersBooks = new List<Models.Book>();
OnlineUsersBooks = GetUsersBooks(user);
Debug.WriteLine("Online Count: " + OnlineUsersBooks.Count.ToString());
if (OnlineUsersBooks.Count > 0)
{
foreach (Models.Book y in NewBooks)
{
if (!(OnlineUsersBooks.Contains(y)))
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("INSERT INTO Bookings VALUES (@UserId, @Title, @Author, @ISBN)", connection);
command.Parameters.AddWithValue("@UserId", user.GetUserID);
command.Parameters.AddWithValue("@Title", y.Title);
command.Parameters.AddWithValue("@Author", y.Author);
command.Parameters.AddWithValue("@ISBN", y.ISBN);
Debug.WriteLine(command.ToString());
command.Connection.Open();
command.ExecuteNonQuery();
}
}
}
}
else
{
foreach (Models.Book y in NewBooks)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand("INSERT INTO Bookings VALUES (@UserId, @Title, @Author, @ISBN)", connection);
command.Parameters.AddWithValue("@UserId", user.GetUserID);
command.Parameters.AddWithValue("@Title", y.Title);
command.Parameters.AddWithValue("@Author", y.Author);
command.Parameters.AddWithValue("@ISBN", y.ISBN);
Debug.WriteLine(command.ToString());
command.Connection.Open();
command.ExecuteNonQuery();
}
}
}
}
GetUserBooks method tested and working fine, returning a list of Books. Do i need some sort of extra override to get the
if (!(OnlineUsersBooks.Contains(y)))
too compare correctly? This is a rather ruff early-stage code, be kind, still have allot of metrics to improve.