2
Answers

How to retrieve value of a Templated Field text box in GridView?

Jennifer Macwan

Jennifer Macwan

14y
2.7k
1
Hi,

I am developing shopping cart in my website. If the user selects any product from DetailsView  details like ProductID, Description and Price should be stored in a datatable. All this is working perfectly fine. Upon clicking CheckOut button I am navigated to another page where I have used the contents of the same datatable as well as additional data from database. Here, I have used templated field text box to store quantity entered by the user. My problem is, on clicking Check Price button I am not able to retrieve its value. At run time, it shows NullReferenceException. I have provided the source code for the page in which Templated text field exists. Can anyone help me with the correct code?

 

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;

        int i;

        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();

           

        }

    }

   

       

 


Answers (2)
Next Recommended Forum