I have a local database for a system / application I'm doing. Within this DB I have Books & Category Columns - CategoryID also makes part of the Books table as a Foreign Key, and I have 10 Category Records, for book genre, like 'Comics' (ID: 1), 'Crafts' (ID: 2), 'Education' (ID: 3), etc.
I am currently implementing an Add Book Record function in the application, so that when I save, a new book record is inserted in my DB with the details given, however, for CategoryID (in Books table), I can only input integers, which I don't want (as explained below)
(BTW, apologies but I'm still a coding newbie)
I have seen some tutorials on Enums, but I can't seem to find anything on how to combine an Enum with a combobox.
What I want is to have the combobox show something like:
1 - Comics, 2 - Crafts, 3 - Education, etc.
The above combobox dropdown list would point out to (for example) '1 - Comics' for CategoryID: 1, '2 - Crafts' for CategoryID: 2, and so on.
This is what I currently have outside of the Add New Record button
public partial class AddBookRecordForm : Form
{
public AddBookRecordForm()
{
InitializeComponent();
}
enum BookCategory
{
Comics = 1,
Crafts = 2,
Education = 3,
History = 4,
Entertainment = 5,
Thriller = 6,
Religion = 7,
Romance = 8,
Fantasy = 9,
Sports = 10
}
Not sure how to proceed now - I'm assuming that the next part of code would need to be implemented within the Save Button Code.
For Reference, the below is the DB connection I did within the Save Button method.
string ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=
C:\Program Files\Microsoft SQL
Server\MSSQL14.SQLEXPRESS\MSSQL\DATA\Library System Project.mdf
;Integrated Security=True;Connect Timeout=30";
string Query = "insert into Books (BookName, BookAuthor, CategoryID, ClassificationID, BookAvailabilityQuantity, Price) values ('" + this.txtName.Text.Trim() + "','" + this.txtAuthor.Text.Trim() + "','" + this.txtCategory.Text.Trim() + "','" + this.txtClassification.Text.Trim() + "','" + this.txtAvailabilityQuantity.Text.Trim() + "','" + this.txtPrice.Text.Trim() + "');";
SqlConnection DBCon = new SqlConnection(ConnectionString);
SqlCommand DBCommand = new SqlCommand(Query, DBCon);
SqlDataReader DBReader;
try
{
DBCon.Open();
DBReader = DBCommand.ExecuteReader();
MessageBox.Show("New book record added to the system.", "Library System", MessageBoxButtons.OK);
while (DBReader.Read())
{
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
// *** If you're going to be opening a connection be sure to close it ***
// *** Finally blocks work well for this ***
DBCon.Close();
this.txtName.ResetText();
this.txtAuthor.ResetText();
this.txtCategory.ResetText();
this.txtClassification.ResetText();
this.txtAvailabilityQuantity.ResetText();
this.txtPrice.ResetText();
}
}