public partial class Purchase : System.Web.UI.Page
{
SqlConnection con;
public void Page_Load(object sender, EventArgs e)
string s = ConfigurationManager.ConnectionStrings["myconstring"].ConnectionString;
con = new SqlConnection(s);
if (!Page.IsPostBack)
DataTable dt1 =(DataTable)Session["MyDataTable"];
dt1.Columns.Add("Discount");
dt1.Columns.Add("Total Price");
GridView1.DataSource = dt1;
GridView1.DataBind();
int row = GridView1.Rows.Count;
int i;
for (i = 0; i < row; i++)
SqlCommand cmd = new SqlCommand("select ddiscount from dconfiguration where configid='" + GridView1.Rows[i].Cells[1].Text + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
GridView1.Rows[i].Cells[4].Text = dr[0].ToString();
dr.Close();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
foreach (TableCell c in e.Row.Cells)
//e.Row.Cells[c].ForeColor = Color.White;c.
c.ForeColor = System.Drawing.Color.Violet;
c.BackColor = System.Drawing.Color.Yellow;
GridView1.BorderColor = System.Drawing.Color.YellowGreen;
protected void checkPrice_Click(object sender, EventArgs e)
int row1 = GridView1.Rows.Count;
for (i = 0; i < row1; i++)
string price = GridView1.Rows[i].Cells[2].Text;
string discount = GridView1.Rows[i].Cells[3].Text;
int price_len = price.Length;
string p = price.Substring(0, price_len - 3);
string p1=p.Replace(",", String.Empty);
int discount_len = discount.Length;
string d = discount.Substring(0, discount_len - 1);
TextBox tb = (TextBox)GridView1.Rows[i].Cells[0].FindControl("tempqty");
string unit = tb.Text;
int u = Convert.ToInt32(unit); //getting error NullReferenceException
int price1 = Convert.ToInt32(p1);
float discount1 = Convert.ToInt16(d);
double x = (discount1 / 100);
double tot = u * (price1 - (price1 * (x)));
GridView1.Rows[i].Cells[5].Text = tot.ToString();