Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can Anyone correct following code.

Error:

in my code shows index[0] values only but i want all values.

Error Coding:


C#
protected void Button1_Click(object sender, EventArgs e)
    {
        int pro_id = int.Parse(((Label)DataList1.Controls[0].FindControl("lblpro_id")).Text.ToString());
        string pro_name = ((Label)DataList1.Controls[0].FindControl("lblpro_name")).Text;
        double price = Double.Parse(((Label)DataList1.Controls[0].FindControl("lblprice")).Text.ToString());
        //int quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtquantity")).Text.ToString());
        //int quantity = Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtquantity")).Text);
        string img = ((Image)DataList1.Controls[0].FindControl("pro_img")).ImageUrl;
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("insert into addcart values ('" + pro_id+ "','" + pro_name + "','" + price + "','" + img + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
    }
Posted
Updated 24-Nov-11 21:18pm
v2

Do iteration that will replace the index of the control from 0 to max length.

C#
for(int i=0; i <= DataList1.Controls[i].ColumnCount; i++)
{
string pro_name = ((Label)DataList1.Controls[0].FindControl("lblpro_name")).Text;
        double price = Double.Parse(((Label)DataList1.Controls[0].FindControl("lblprice")).Text.ToString());
        //int quantity = int.Parse(((TextBox)DataList1.Controls[0].FindControl("txtquantity")).Text.ToString());
        //int quantity = Convert.ToInt32(((TextBox)DataList1.Controls[0].FindControl("txtquantity")).Text);
        string img = ((Image)DataList1.Controls[0].FindControl("pro_img")).ImageUrl;
}


The logic is here. Hope this one helps :)

Best regards,
Eduard
 
Share this answer
 
Comments
M.Narmatha 25-Nov-11 4:10am    
it shows error like:

Error 1 Operator '<=' cannot be applied to operands of type 'int' and 'method group'
Then you will probably need to change it to use a loop.
Look at either a for[^] loop, or a foreach[^] - whichever is appropriate.

Why is this giving you a problem?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900