I'm really having a hard time to get the values of the checkedlistbox. Here it is. I will explain this.
I have a database named "LibraryDB" which has three tables.
Table 1: Book
Columns: Book_ID, Book_Title, Call_Number, Accession_Number
Table 2: Author
Columns: Author_ID, Author_Name
Table 3: Book_Author
Columns: Book_Author_ID, Book_ID, Author_ID
I have a form for inserting a book record which has the following query.
ALTER procedure [dbo].[usp_insertbook]
@booktitle nvarchar(100),
@callno nvarchar(14),
@accessno nvarchar(20),
AS
insert into Book values (@booktitle, @callno, @accessno)
It's easy really but it gets hard when I need to add a record that is separate with it. The Book_Author must also have records when I add the book. It's quite confusing especially there's no author column in book table but it's the requirement.
I have done this
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_insertbook";
cmd.Connection = con;
cmd.Parameters.Add("@booktitle", SqlDbType.NVarChar).Value = booktitle.Text;
cmd.Parameters.Add("@callno", SqlDbType.NVarChar).Value = callnumber.Text;
cmd.Parameters.Add("@accessno", SqlDbType.NVarChar).Value = accessionno.Text;
cmd.Parameters.Add("@isbn", SqlDbType.BigInt).Value = isbn.Text;
foreach (string @authorname in authorclb.SelectedItems)
{
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "usp_insertbookauthor";
cmd1.Connection = con;
cmd1.Parameters.Add("@authorname", SqlDbType.NVarChar).Value = authorclb.SelectedItem;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
It doesn't get the values for checkedlistbox.
My code for Book_Author that is inside the foreach statement is
foreach (string @authorname in authorclb.SelectedItems)
{
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "usp_insertbookauthor";
cmd1.Connection = con;
cmd1.Parameters.Add("@authorname", SqlDbType.NVarChar).Value = authorclb.SelectedItem;
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
}
I really need some help here.