ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1735
Number of posts: 3305

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
How to retrieve value of a Templated Field text box in GridView? Posted by Dotnetstudent on 27 May 2010 at 10:26 PM
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);

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

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



}

}
[</code>]



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.