void UpdateBookByID() { try { int actual_stock = Convert.ToInt32(TextBox10.Text.Trim()); int current_stock = Convert.ToInt32(TextBox11.Text.Trim()); if (global_actual_stock == actual_stock) { } else { if (actual_stock < global_issued_books) { Response.Write("<script>alert('Actual stock value can not be less than the issued books');</script>"); return; } else { current_stock = actual_stock - global_issued_books; TextBox8.Text = "" + current_stock; } } string genres = ""; foreach (int i in ListBox6.GetSelectedIndices()) { genres = genres + ListBox6.Items[i] + ";"; } genres = genres.Remove(genres.Length - 1); string filepath = "~/images/BookInventory/books1"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); if (filename == "" || filename == null) { filepath = global_filepath; } else { FileUpload1.SaveAs(Server.MapPath("images/BookInventory/" + filename)); filepath = "~/images/BookInventory/" + filename; } SqlConnection conn = new SqlConnection(constr); //checking whether the connection is open or not if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand("UPDATE book_tbl SET book_name=@bnm, genre=@gn, author_name=@an, publisher_name=@pn, publish_date=@pd, language=@lan, eddition=@edd, book_cost=@bc, no_of_pages=@pgs, book_description=@bdes, actual_stock=@astk, current_stock=@cstk, book_img_link=@blnk WHERE book_id='" + TextBox12.Text.Trim() + "'", conn); cmd.Parameters.AddWithValue("@bnm", TextBox13.Text.Trim()); cmd.Parameters.AddWithValue("@gn", genres); cmd.Parameters.AddWithValue("@an", DropDownList3.SelectedItem.Value); cmd.Parameters.AddWithValue("@pn", DropDownList2.SelectedItem.Value); cmd.Parameters.AddWithValue("@pd", TextBox1.Text.Trim()); cmd.Parameters.AddWithValue("@lan", DropDownList1.SelectedItem.Value); cmd.Parameters.AddWithValue("@edd", TextBox7.Text.Trim()); cmd.Parameters.AddWithValue("@bc", TextBox8.Text.Trim()); cmd.Parameters.AddWithValue("@pgs", TextBox9.Text.Trim()); cmd.Parameters.AddWithValue("@bdes", TextBox2.Text.Trim()); cmd.Parameters.AddWithValue("@astk", actual_stock.ToString()); cmd.Parameters.AddWithValue("@cstk", current_stock.ToString()); cmd.Parameters.AddWithValue("@blnk", filepath); cmd.ExecuteNonQuery(); conn.Close(); GridView1.DataBind(); Response.Write("<script>alert('Book Updated successfully');</script>"); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
void AddNewBook() { if (TextBox12.Text.Trim() == "" || TextBox12.Text.Trim() == null) { Response.Write("<script>alert('Please enter a valid book id');</script>"); } else { try { string genres = ""; foreach (int i in ListBox6.GetSelectedIndices()) { genres = genres + ListBox6.Items[i] + ";"; } genres = genres.Remove(genres.Length - 1); string filepath = "~/images/BookInventory/book1.png"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("images/BookInventory/" + filename)); filepath = "~/images/BookInventory/" + filename; SqlConnection conn = new SqlConnection(constr); //checking whether the connection is open or not if (conn.State == ConnectionState.Closed) { conn.Open(); } SqlCommand cmd = new SqlCommand("INSERT INTO book_tbl (book_id, book_name, genre, author_name, publisher_name, publish_date, language,eddition, book_cost, no_of_pages, book_description, actual_stock, current_stock, book_img_link) VALUES(@bid, @bnm, @gn, @an, @pn, @pd, @lan, @edd, @bc, @pgs, @bdes, @astk, @cstk, @blnk);", conn); cmd.Parameters.AddWithValue("@bid", TextBox12.Text.Trim()); // cmd.Parameters.AddWithValue("@bnm", TextBox13.Text.Trim()); // cmd.Parameters.AddWithValue("@gn", genres); cmd.Parameters.AddWithValue("@an", DropDownList3.SelectedItem.Value); // cmd.Parameters.AddWithValue("@pn", DropDownList2.SelectedItem.Value); // cmd.Parameters.AddWithValue("@pd", TextBox1.Text.Trim()); // cmd.Parameters.AddWithValue("@lan", DropDownList1.SelectedItem.Value); // cmd.Parameters.AddWithValue("@edd", TextBox7.Text.Trim()); // cmd.Parameters.AddWithValue("@bc", TextBox8.Text.Trim()); // cmd.Parameters.AddWithValue("@pgs", TextBox9.Text.Trim()); // cmd.Parameters.AddWithValue("@bdes", TextBox2.Text.Trim()); // cmd.Parameters.AddWithValue("@astk", TextBox10.Text.Trim()); // cmd.Parameters.AddWithValue("@cstk", TextBox10.Text.Trim()); cmd.Parameters.AddWithValue("@blnk", filepath); cmd.ExecuteNonQuery(); conn.Close(); Response.Write("<script>alert('A new book added successfully');</script>"); GridView1.DataBind(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } } }