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

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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

DotnetstudentDotnetstudent Posts: 1Member

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?
Is this code snippet correct:
TextBox tb =(TextBox)GridView1.Rows[i].Cells[0].FindControl("tempqty");
string unit = tb.Text;
int u = Convert.ToInt32(unit);

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("Total Price");

GridView1.DataSource = dt1;


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


SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())

GridView1.Rows[i].Cells[4].Text = dr[0].ToString();






protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)


foreach (TableCell c in e.Row.Cells)

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


Sign In or Register to comment.