void addNewBook() { try { string genres = ""; foreach (int i in GenreList.GetSelectedIndices()) { genres = genres + GenreList.Items[i] + ","; } genres = genres.Remove(genres.Length - 1); // remove last commas string filepath = "~/book_inventory/books1.png"; if (FileUpload1.FileName != "") { string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("book_inventory/" + filename)); filepath = "~/book_inventory/" + filename; } else { Response.Write("<script>alert('choose your photo, Thanks!');</script"); return; } SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand("INSERT INTO book_master_tb1" + "(book_id,book_name,genre,author_name,publisher_name,publish_date,language,edition,book_cost,no_of_pages,book_description,actual_stock,current_stock,book_img_link)" + "values" + "(@book_id,@book_name,@genre,@author_name,@publisher_name,@publish_date,@language,@edition,@book_cost,@no_of_pages,@book_description,@actual_stock,@current_stock,@book_img_link)", con); cmd.Parameters.AddWithValue("@book_id", BookIDText.Text.Trim()); cmd.Parameters.AddWithValue("@book_name", BookNameText.Text.Trim()); cmd.Parameters.AddWithValue("@genre", genres); cmd.Parameters.AddWithValue("@author_name", AuthorNameDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@publisher_name", PublisherDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@publish_date", PublishDateText.Text.Trim()); cmd.Parameters.AddWithValue("@language", LanguageDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", EditionText.Text.Trim()); cmd.Parameters.AddWithValue("@book_cost", BookCostText.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pages", PagesText.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", BookDescriptionText.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", ActualStockText.Text.Trim()); cmd.Parameters.AddWithValue("@current_stock", ActualStockText.Text.Trim()); cmd.Parameters.AddWithValue("@book_img_link", filepath); cmd.ExecuteNonQuery(); con.Close(); Response.Write("<script>alert('Add Book Success!');</script"); } catch (Exception ex) { Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script"); } }
void updateBookByID() { try { int actual_stock = Convert.ToInt32(ActualStockText.Text.ToString()); int current_stock = Convert.ToInt32(CurrentStockText.Text.ToString()); if (actual_stock != current_stock) { if (actual_stock < global_issued_books) { Response.Write("<script>alert('Actual Stock Value con't be less than the Issued Books!');</script"); return; } else { current_stock = actual_stock - global_issued_books; CurrentStockText.Text = "" + current_stock; } } string genres = ""; foreach (int i in GenreList.GetSelectedIndices()) { genres = genres + GenreList.Items[i] + ","; } genres = genres.Remove(genres.Length - 1); // remove last commas string filepath = "~/book_inventory/books1.png"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); if (string.IsNullOrEmpty(filename)) { filepath = global_filepath; } else { FileUpload1.SaveAs(Server.MapPath("book_inventory/" + filename)); filepath = "~/book_inventory/" + filename; } SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd = new SqlCommand("UPDATE book_master_tb1 set book_name=@book_name, genre=@genre, author_name=@author_name, publisher_name=@publisher_name, publish_date=@publish_date, language=@language, edition=@edition, book_cost=@book_cost, no_of_pages=@no_of_pages, book_description=@book_description, actual_stock=@actual_stock, current_stock=@current_stock, book_img_link=@book_img_link where book_id= @author_id;", con); //cmd.Parameters.AddWithValue("@author_name", AuthorNameText.Text.Trim()); cmd.Parameters.AddWithValue("@author_id", BookIDText.Text.Trim()); cmd.Parameters.AddWithValue("@book_name", BookNameText.Text.Trim()); cmd.Parameters.AddWithValue("@genre", genres); cmd.Parameters.AddWithValue("@author_name", AuthorNameDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@publisher_name", PublisherDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@publish_date", PublishDateText.Text.Trim()); cmd.Parameters.AddWithValue("@language", LanguageDropDownList.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", EditionText.Text.Trim()); cmd.Parameters.AddWithValue("@book_cost", BookCostText.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pages", PagesText.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", BookDescriptionText.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", actual_stock.ToString()); cmd.Parameters.AddWithValue("@current_stock", current_stock.ToString()); cmd.Parameters.AddWithValue("@book_img_link", filepath); cmd.ExecuteNonQuery(); con.Close(); getBookByID(); GridView1.DataBind(); } catch (Exception ex) { Response.Write("<script language='javascript'>alert('" + ex.Message + "');</script"); } }