Howdy, Stranger!

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

Categories

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

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

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