ASP.net when i click a button i want to add rows from a grid view to a database but its giving me an error when i try to add more than one row but working fine with only one row.
The error it is giving me is System.FormatException: 'Input string was not in a correct format.'
Also when i click on the medication gridview to add the next row to the prescription gridview the first row of the prescription gridview gets deleted
Here is my .aspx file
<%@ Page Title="" Language="C#" MasterPageFile="~/Doctor/Doctor.Master" AutoEventWireup="true" CodeBehind="DoctorDischargeForm.aspx.cs" Inherits="HWMS08.Doctor.DoctorDischargeForm" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<title>Discharge Form</title>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cphContent" runat="server">
<div style="margin-left:240px;text-align:left;">
<asp:Label ID="Label1" runat="server" ForeColor="#6F8C46" Text="Discharge Patient" Font-Names="Calibri" Font-Size="25px"></asp:Label>
<br />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Discharge Form" Font-Names="Calibri" Font-Size="18px"></asp:Label>
<br />
<br />
<div style="text-align:left" class="col">
<br />
<br />
</div>
<div class="col">
<asp:Label ID="Label3" runat="server" Text="Discharge Date:"></asp:Label>
<asp:Calendar ID="Calendar1" runat="server" Height="16px" Width="119px"></asp:Calendar>
<br />
<br />
<asp:Label ID="Label5" runat="server" Text="Discharge Time:"></asp:Label>
<asp:TextBox ID="txtTime" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtTime" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Discharge Condtion:"></asp:Label>
<asp:TextBox ID="txtCondition" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtCondition" ErrorMessage="*"></asp:RequiredFieldValidator>
<br />
<br />
<asp:Button ID="btnConfirm" runat="server" Height="21px" Text="Confirm" Width="92px" OnClick="btnConfirm_Click" />
</div>
</div>
</asp:Content>
Here is my .aspx.cs code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BusinessLogicLayer;
using DataAccessLayer;
namespace HWMS08.Doctor
{
public partial class DoctorMedication : System.Web.UI.Page
{
BusinessLogic bll = new BusinessLogic();
DataAccess dal = new DataAccess();
string DrugName;
int Row=0, Cell=0,scriptID;
DataTable script = new DataTable();
DataTable d = new DataTable();
DataRow row;
DataColumn column;
DataView view;
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == true)
{
}
else
{
d.Columns.Add("drugname");
d.Columns.Add("refills");
d.Columns.Add("quantity");
d.Columns.Add("note");
Session.Add("script",d);
dgvMedication.DataSource = bll.GetMedication();
dgvMedication.DataBind();
dgvScript.DataSource = bll.GetPrescription();
dgvScript.DataBind();
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "drug";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "refills";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "quantity";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "note";
//script.Columns.Add(column);
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnAddDrug_Click(object sender, EventArgs e)
{
//string refill = txtRefills.Text;
//string qty = txtQuantity.Text;
//string note = txtNote.Text;
//DrugName = Session["DrugName"].ToString();
//script = (DataTable)Session["script"];
//script.Columns.Add("drugname");
//script.Columns.Add("refills");
//script.Columns.Add("quantity");
//script.Columns.Add("note");
//row = script.NewRow();
//row["drugname"] = DrugName;
//row["refills"] = refill;
//row["quantity"] = qty;
//row["note"] = note;
//script.Rows.Add(row);
//dgvPrescription.DataSource = script;
//dgvPrescription.DataBind();
//dgvPrescription.Rows[Row].Cells[1].Text = DrugName;
//dgvPrescription.Rows[Row].Cells[2].Text = refill;
//dgvPrescription.Rows[Row].Cells[3].Text = qty;
//dgvPrescription.Rows[Row].Cells[4].Text = note;
//Session.Add("script", script);
////column = new DataColumn();
////column.DataType = Type.GetType("System.String");
////column.ColumnName = "drug";
////script.Columns.Add(column);
////column = new DataColumn();
////column.DataType = Type.GetType("System.String");
////column.ColumnName = "refills";
////script.Columns.Add(column);
////column = new DataColumn();
////column.DataType = Type.GetType("System.String");
////column.ColumnName = "quantity";
////script.Columns.Add(column);
////column = new DataColumn();
////column.DataType = Type.GetType("System.String");
////column.ColumnName = "note";
////script.Columns.Add(column);
//if (txtRefills.Text != null && txtQuantity.Text != null && txtNote.Text != null)
//{
// //row = script.NewRow();
// //row["drug"] = DrugName;
// //row["refills"] = refill;
// //row["quantity"] = qty;
// //row["note"] = note;
// //script.Rows.Add(row);
// //script.Rows.Add(DrugName,refill, qty, note);
// //script.Rows[Row].Cells[Cell].Text = DrugName;
// //Cell++;
// //script.Rows[Row].Cells[Cell].Text = refill;
// //Cell++;
// //script.Rows[Row].Cells[Cell].Text = qty;
// //Cell++;
// //dgvPrescription.Rows[Row].Cells[Cell].Text = UnitPrice;
// //Cell++;
// //dgvPrescription.Rows[Row].Cells[Cell].Text = note;
// //Row++;
// //Cell = 0;
// //view = new DataView(script);
// //dgvPrescription.DataSource = script;
// //dgvPrescription.DataBind();
//}
//Row++;
}
protected void btnRemoveDrug_Click(object sender, EventArgs e)
{
}
protected void dgvPrescription_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
}
protected void dgvPrescription_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
foreach (GridViewRow g1 in dgvPrescription.Rows)
{
scriptID = int.Parse(Session["scriptID"].ToString());
if (g1.Cells[1].Text != null && g1.Cells[2].Text != null && g1.Cells[3].Text != null && g1.Cells[4].Text != null)
{
PrescriptionDetail detail = new PrescriptionDetail();
detail.MedicationName = g1.Cells[1].Text;
detail.PrescriptionID = scriptID;
detail.Refills = Convert.ToInt32(g1.Cells[2].Text);
detail.Quantity = Convert.ToInt32(g1.Cells[3].Text);
detail.ScriptNotes = g1.Cells[4].Text;
int i = bll.InsertScriptMed(detail);
}
}
}
protected void btnAddPrescription_Click(object sender, EventArgs e)
{
if (txtStatus.Text != null)
{
DataTable dt = (DataTable)Session["DoctorUserDetails"];
int doctorID = int.Parse(dt.Rows[0]["DoctorID"].ToString());
Prescription script = new Prescription();
script.DoctorID = doctorID;
script.PatientID = int.Parse(Session["DoctorPatientId"].ToString());
script.PrescriptionDate = cldPrescription.SelectedDate;
script.Status = txtStatus.Text;
int i = bll.InsertPrescription(script);
}
else { }
dgvScript.DataSource = bll.GetPrescription();
dgvScript.DataBind();
}
protected void txtSearchScript_TextChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
table = bll.GetPrescription();
//table = fill the table with proper values by accessing the database
if (txtSearchScript.Text != "")
{
DataView dv = table.DefaultView;
dv.RowFilter = string.Format("Name like '{0}%'", txtSearchScript.Text);
dgvScript.DataSource = dv;
dgvScript.DataBind();
}
else if (txtSearchScript.Text == "")
{
dgvScript.DataSource = table;
dgvScript.DataBind();
}
}
protected void txtMedication_TextChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
table = bll.GetMedication();
//table = fill the table with proper values by accessing the database
if (txtMedication.Text != "")
{
DataView dv = table.DefaultView;
dv.RowFilter = string.Format("Name like '{0}%'", txtMedication.Text);
dgvMedication.DataSource = dv;
dgvMedication.DataBind();
}
else if (txtMedication.Text == "")
{
dgvMedication.DataSource = table;
dgvMedication.DataBind();
}
}
protected void LinkButton1_click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex + 1;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data
dt.Rows.Remove(dt.Rows[rowID]);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
dgvPrescription.DataSource = dt;
dgvPrescription.DataBind();
}
}
protected void btnAddDrug_Click1(object sender, EventArgs e)
{
string refill = txtRefills.Text;
string qty = txtQuantity.Text;
string note = txtNote.Text;
DrugName = Session["DrugName"].ToString();
script = (DataTable)Session["script"];
row = script.NewRow();
row["drugname"] = DrugName;
row["refills"] = refill;
row["quantity"] = qty;
row["note"] = note;
script.Rows.Add(row);
dgvPrescription.DataSource = script;
dgvPrescription.DataBind();
dgvPrescription.Rows[Row].Cells[1].Text = DrugName;
dgvPrescription.Rows[Row].Cells[2].Text = refill;
dgvPrescription.Rows[Row].Cells[3].Text = qty;
dgvPrescription.Rows[Row].Cells[4].Text = note;
Session["script"] = script;
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "drug";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "refills";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "quantity";
//script.Columns.Add(column);
//column = new DataColumn();
//column.DataType = Type.GetType("System.String");
//column.ColumnName = "note";
//script.Columns.Add(column);
if (txtRefills.Text != null && txtQuantity.Text != null && txtNote.Text != null)
{
//row = script.NewRow();
//row["drug"] = DrugName;
//row["refills"] = refill;
//row["quantity"] = qty;
//row["note"] = note;
//script.Rows.Add(row);
//script.Rows.Add(DrugName,refill, qty, note);
//script.Rows[Row].Cells[Cell].Text = DrugName;
//Cell++;
//script.Rows[Row].Cells[Cell].Text = refill;
//Cell++;
//script.Rows[Row].Cells[Cell].Text = qty;
//Cell++;
//dgvPrescription.Rows[Row].Cells[Cell].Text = UnitPrice;
//Cell++;
//dgvPrescription.Rows[Row].Cells[Cell].Text = note;
//Row++;
//Cell = 0;
//view = new DataView(script);
//dgvPrescription.DataSource = script;
//dgvPrescription.DataBind();
}
Row++;
}
protected void dgvScript_SelectedIndexChanged(object sender, EventArgs e)
{
scriptID = int.Parse(dgvScript.SelectedRow.Cells[1].Text);
Session.Add("scriptID", scriptID);
}
protected void btnEditDrug_Click(object sender, EventArgs e)
{
}
protected void dgvMedication_SelectedIndexChanged(object sender, EventArgs e)
{
DrugName = dgvMedication.SelectedRow.Cells[2].Text;
Session.Add("DrugName",DrugName);
}
}
}