protected void gvItems_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "receipt") { int index = Convert.ToInt32(e.CommandArgument); DropDownList ddlTransfomer = (DropDownList)gvItems.Rows[index].FindControl("ddlTransfomer"); if (ddlTransfomer.SelectedIndex > 0) //making sure a transformer is selected first from the dropdown list { //get division name based on the selected transformer id string divisionname = TransformerReceiptLogic.GetDivisionNameofReceivedTransformer(Convert.ToInt32(ddlTransfomer.SelectedValue)); ScriptManager.RegisterStartupScript(Page, typeof(Page), "Message", "alert('Division of selected transformer: " + divisionname + "');", true); } else { ScriptManager.RegisterStartupScript(Page, typeof(Page), "Message", "alert('Please select transformer first');", true); } } }
//method for saving all the data private void _SaveDeliveryItemDetails() { TransformerReceiptLogic baby = new TransformerReceiptLogic(); string insertStatement1; string insertStatement2; StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); try { //insert statement for main table insertStatement1 = "Insert into Transformer_Receipts(ChallanNo, ChallanDate, Division, ReceiptDate,ProbableCause,ModifiedBy)values('@ChallanNo', '@ChallanDate', '@Division', '@ReceiptDate', '@ProbableCause','@ModifiedBy')"; sb1.Append(insertStatement1.Replace("@ChallanNo", _tbChalanNo.Text.TrimEnd()).Replace("@ChallanDate", _tbChallanDate.Text).Replace("@Division", _ddIntendDivisions.SelectedItem.ToString()).Replace("@ReceiptDate", _tbReceiptDate.Text).Replace("@ProbableCause", tbProbableCause.Text).Replace("@ModifiedBy", Session["USERID"].ToString())); //multiple insert statements for details table insertStatement2 = "INSERT INTO Transformer_ReceiptsDetails(ChallanNo,Voltage, kVA,Make,Seriel,Oil) values('@ChallanNo','@Voltage', '@kVA', '@Make', '@Seriel', '@Oil')"; DropDownList ddlVoltage, ddlKVA, ddlMake; TextBox tbSeriel, tbOil; for (int i = 0; i < gvItems.Rows.Count; i++) { ddlVoltage = gvItems.Rows[i].FindControl("ddlVoltage") as DropDownList; ddlKVA = gvItems.Rows[i].FindControl("ddlKVA") as DropDownList; ddlMake = gvItems.Rows[i].FindControl("ddlMake") as DropDownList; tbSeriel = gvItems.Rows[i].FindControl("tbSeriel") as TextBox; tbOil = gvItems.Rows[i].FindControl("tbOil") as TextBox; //SAVE CODE if (ddlVoltage.SelectedValue.ToString() != "") { sb2.Append(insertStatement2.Replace("@ChallanNo", _tbChalanNo.Text.TrimEnd()).Replace("@Voltage", ddlVoltage.SelectedValue).Replace("@kVA", ddlKVA.SelectedItem.ToString()).Replace("@Make", ddlMake.SelectedValue).Replace("@Seriel", tbSeriel.Text).Replace("@Oil", tbOil.Text)); } } //now save it to db //making sure sb string is not empty if (sb2.ToString() != "") { //call the method to save both in primary deliverychallan table and delivery details table baby.SaveTransformerReceiptEntry(sb1.ToString(), sb2.ToString()); //using session here to //Session["CHALLANNO"] = _tbChalanNo.Text; //Response.Redirect(Request.Url.ToString()); panelError.Visible = false; lblSuccess.Text = "Receipt Entry successfully added."; panelSuccess.Visible = true; Utilities.ClearAllControls(Page.Controls); } else { panelError.Visible = true; lblError.Text = "Error! Select atleast one item to add."; panelSuccess.Visible = false; } } catch (System.Data.SqlClient.SqlException ex) { if (ex.Message.Contains("duplicate key")) { panelError.Visible = true; lblError.Text = "Error! Challan number already exists. "; panelSuccess.Visible = false; return; } } catch (System.Threading.ThreadAbortException) { //do nothing } catch (Exception ex) { Session["ERRORMSG"] = ex.ToString(); Response.Redirect("~/Error.aspx", true); } }