void updateBook() { try { //for update value of current and Actual stock int actual_stock = Convert.ToInt32(txtAStock.Text.Trim()); int current_stock = Convert.ToInt32(txtCStock.Text.Trim()); if (globalActualStock == actual_stock) { } else { if (actual_stock < globalIsuuedbook) { Response.Write("<script>alert('Actual stock can not be less than current stock');</script>"); return; } else { current_stock = actual_stock - globalIsuuedbook; txtCStock.Text = " " + current_stock; } } //for multiples category string DLCategoryy = ""; foreach (int i in DLCategory.GetSelectedIndices()) { DLCategoryy = DLCategoryy + DLCategory.Items[i] + ","; } // DLCategory=horro,health, //so remove comma at last we have do one thing DLCategoryy = DLCategoryy.Remove(DLCategoryy.Length - 1); // for upload file string basepath = "~/bookinventory/books.png"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("bookinventory/" + filename)); if (filename == "" || filename == null) { basepath = globalfilepath; } else { FileUpload1.SaveAs(Server.MapPath("bookinventory/" + filename)); basepath = "~/bookinventory/" + filename; } SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } // query with placeholder SqlCommand cmd = new SqlCommand ("UPDATE book_master_tbl SET book_name=@book_name,category=@category,author_name=@author_name,publisher_name=@publisher_name,publish_date=@publish_date,language=@language,edition=@edition,book_cost=@book_cost,no_of_pagess=@no_of_pagess,book_description=@book_description,actual_stock=@actual_stock,current_stock=@current_stock,book_img_link=@book_img_link WHERE book_id='" + txtBookId.Text.Trim() + "'", con); //Attach @parameter values cmd.Parameters.AddWithValue("@book_name", txtBookName.Text.Trim()); cmd.Parameters.AddWithValue("@category", DLCategoryy); cmd.Parameters.AddWithValue("@author_name", DLAuthorName.SelectedItem.Value); cmd.Parameters.AddWithValue("@publish_date", txtPdate.Text.Trim()); cmd.Parameters.AddWithValue("@publisher_name", DLPublisherName.SelectedItem.Value); cmd.Parameters.AddWithValue("@language", DLLang.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", txtEdition.Text.Trim()); cmd.Parameters.AddWithValue("@book_cost", txtBookCost.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pagess", txtPages.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", txtDes.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", actual_stock.ToString()); cmd.Parameters.AddWithValue("@current_stock", current_stock.ToString()); cmd.Parameters.AddWithValue("@book_img_link", basepath); cmd.ExecuteNonQuery(); con.Close(); GridView1.DataBind(); Response.Write("<script>alert('Book Details Update succcesfuuky..');</csript>"); clearForm(); } catch (Exception ex) { Response.Write("Something Went Wrong" + ex.Message); } }
void addBook() { try { //for multiples category string DLCategoryy = ""; foreach (int i in DLCategory.GetSelectedIndices()) { DLCategoryy = DLCategoryy + DLCategory.Items[i] + ","; } // DLCategory=horro,health, //so remove comma at last we have do one thing DLCategoryy = DLCategoryy.Remove(DLCategoryy.Length - 1); // for upload file string basepath = "~/bookinventory/books.png"; string filename = Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(Server.MapPath("bookinventory/" + filename)); basepath = "~/bookinventory/" + filename; SqlConnection con = new SqlConnection(strcon); if (con.State == ConnectionState.Closed) { con.Open(); } // query with placeholder SqlCommand cmd = new SqlCommand ("INSERT into book_master_tbl(book_id,book_name,category,author_name,publisher_name,publish_date,language,edition,book_cost,no_of_pagess,book_description,actual_stock,current_stock,book_img_link) values(@book_id,@book_name,@category,@author_name,@publisher_name,@publish_date,@language,@edition,@book_cost,@no_of_pagess,@book_description,@actual_stock,@current_stock,@book_img_link)", con); //Attach @parameter values cmd.Parameters.AddWithValue("@book_id", txtBookId.Text.Trim()); cmd.Parameters.AddWithValue("@book_name", txtBookName.Text.Trim()); cmd.Parameters.AddWithValue("@author_name", DLAuthorName.SelectedItem.Value); cmd.Parameters.AddWithValue("@publisher_name", DLPublisherName.SelectedItem.Value); cmd.Parameters.AddWithValue("@publish_date", txtPdate.Text.Trim()); cmd.Parameters.AddWithValue("@language", DLLang.SelectedItem.Value); cmd.Parameters.AddWithValue("@edition", txtEdition.Text.Trim()); cmd.Parameters.AddWithValue("@book_cost", txtBookCost.Text.Trim()); cmd.Parameters.AddWithValue("@no_of_pagess", txtPages.Text.Trim()); cmd.Parameters.AddWithValue("@book_description", txtDes.Text.Trim()); cmd.Parameters.AddWithValue("@actual_stock", txtAStock.Text.Trim()); cmd.Parameters.AddWithValue("@current_stock", txtAStock.Text.Trim()); // Multiple selected items Dropdownlisst cmd.Parameters.AddWithValue("@category", DLCategoryy); //Img path store in DB cmd.Parameters.AddWithValue("@book_img_link", basepath); cmd.ExecuteNonQuery(); Response.Write("<script>alert('Book deatils Enter Successfully...');</script>"); con.Close(); clearForm(); // clearForm(); GridView1.DataBind(); } catch (Exception ex) { // Error_msg.Visible = true; //Suceess_msg.Visible = false; Response.Write("Something Went Wrong" + ex.Message); } }