void AddNewBook() { try { string genres = ""; foreach (int i in ListBox1.GetSelectedIndices()) { genres = genres + ListBox1.Items[i] + ","; } // genres = Adventure,Self Help, genres = genres.Remove(genres.Length - 1); string filepath = "~/book_inventory/books1.png"; string filename = Path.GetFileName(FIleUpload1.PostedFile.FileName); FIleUpload1.SaveAs(Server.MapPath("book_inventory/" + filename)); filepath = "~/book_inventory/" + filename; SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } SqlCommand cmd2 = new SqlCommand("SELECT * from author_master_tbl WHERE author_name = '" + DropDownList3.SelectedItem.Value + "' ", con); SqlDataAdapter sda = new SqlDataAdapter(cmd2); DataTable sdt = new DataTable(); sda.Fill(sdt); var aid = ""; if (sdt.Rows.Count >= 1) { aid = sdt.Rows[0]["author_id"].ToString().Trim(); } else { Response.Write("<script>alert('Author Not Found.');</script>"); } SqlCommand cmd3 = new SqlCommand("SELECT * from publisher_master_tbl WHERE publisher_name = '" + DropDownList2.SelectedItem.Value + "' ", con); SqlDataAdapter sda2 = new SqlDataAdapter(cmd3); DataTable sdt2 = new DataTable(); sda2.Fill(sdt2); var pid = ""; if (sdt.Rows.Count >= 1) { pid = sdt2.Rows[0]["publisher_id"].ToString().Trim(); } else { Response.Write("<script>alert('Publisher Not Found.');</script>"); } SqlCommand cmd = new SqlCommand("INSERT INTO book_master_tbl (book_id,book_name,genre,author,publisher,published_date,language,edition,book_price,no_of_pages,book_description,actual_stock,current_stock,book_img_link) values (@book_id,@book_name,@genre,@author,@publisher,@published_date,@language,@edition,@book_price,@no_of_pages,@book_description,@actual_stock,@current_stock,@book_img_link)", con); cmd.Parameters.AddWithValue("@book_id", TextBox3.Text.Trim()); cmd.Parameters.AddWithValue("@book_name", TextBox4.Text.Trim()); cmd.Parameters.AddWithValue("@genre", genres); cmd.Parameters.AddWithValue("@author", aid); cmd.Parameters.AddWithValue("@publisher", pid); cmd.Parameters.AddWithValue("@published_date", TextBox6.Text.Trim()); cmd.Parameters.AddWithValue("@language", DropDownList1.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@book_price", TextBox9.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pages", TextBox1.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", TextBox10.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", TextBox8.Text.Trim()); cmd.Parameters.AddWithValue("@current_stock", TextBox7.Text.Trim()); cmd.Parameters.AddWithValue("@book_img_link", filepath); cmd.ExecuteNonQuery(); con.Close(); Response.Write("<script>alert('Book added successfully.');</script>"); GridView1.DataBind(); ClearForm(); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
void UpdateBookByID() { if (CheckIfBookExists()) { try { int actual_stock = Convert.ToInt32(TextBox8.Text.Trim()); int current_stock = Convert.ToInt32(TextBox7.Text.Trim()); if (global_actual_stock == actual_stock) { } else { if (actual_stock < global_issued_books) { Response.Write("<script>alert('Actual Stock value cannot be less than the Issued books');</script>"); return; } else { current_stock = actual_stock - global_issued_books; TextBox7.Text = "" + current_stock; } } string genres = ""; foreach (int i in ListBox1.GetSelectedIndices()) { genres = genres + ListBox1.Items[i] + ","; } genres = genres.Remove(genres.Length - 1); string filepath = "~/book_inventory/books1.png"; string filename = Path.GetFileName(FIleUpload1.PostedFile.FileName); if (filename == "" || filename == null) { 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 cmd2 = new SqlCommand("SELECT * FROM author_master_tbl WHERE author_name = '" + DropDownList3.SelectedItem.Value + "' ", con); SqlDataAdapter sda = new SqlDataAdapter(cmd2); DataTable sdt = new DataTable(); sda.Fill(sdt); var aid = ""; if (sdt.Rows.Count >= 1) { aid = sdt.Rows[0]["author_id"].ToString().Trim(); } else { Response.Write("<script>alert('Author Not Found.');</script>"); } SqlCommand cmd3 = new SqlCommand("SELECT * from publisher_master_tbl WHERE publisher_name = '" + DropDownList2.SelectedItem.Value + "' ", con); SqlDataAdapter sda2 = new SqlDataAdapter(cmd3); DataTable sdt2 = new DataTable(); sda2.Fill(sdt2); var pid = ""; if (sdt.Rows.Count >= 1) { pid = sdt2.Rows[0]["publisher_id"].ToString().Trim(); } else { Response.Write("<script>alert('Publisher Not Found.');</script>"); } SqlCommand cmd = new SqlCommand("UPDATE book_master_tbl SET book_name=@book_name, genre=@genre, author=@author, publisher=@publisher, published_date=@published_date, language=@language, edition=@edition, book_price=@book_price, 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='" + TextBox3.Text.Trim() + "'", con); cmd.Parameters.AddWithValue("@book_name", TextBox4.Text.Trim()); cmd.Parameters.AddWithValue("@genre", genres); cmd.Parameters.AddWithValue("@author", aid); cmd.Parameters.AddWithValue("@publisher", pid); cmd.Parameters.AddWithValue("@published_date", TextBox6.Text.Trim()); cmd.Parameters.AddWithValue("@language", DropDownList1.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@book_price", TextBox9.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pages", TextBox1.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", TextBox10.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", TextBox8.Text.Trim()); cmd.Parameters.AddWithValue("@current_stock", TextBox7.Text.Trim()); cmd.Parameters.AddWithValue("@book_img_link", filepath); cmd.ExecuteNonQuery(); con.Close(); GridView1.DataBind(); Response.Write("<script>alert('Book Updated Successfully');</script>"); ClearForm(); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } } else { Response.Write("<script>alert('Invalid Book ID');</script>"); } }