Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//read the below comment on code....

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class admin_product_attribute : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand comm;
   
    protected void Page_Load(object sender, EventArgs e)
    {
        int ProductId, CategoryId;
        if(Page.IsPostBack==false)
        {
            ProductId =
            Convert.ToInt32(Session["ProductId"].ToString());
            CategoryId =
                Convert.ToInt32(Session["CategoryId"].ToString());
            ViewState.Add("ProductId", ProductId);
            ViewState.Add("CategoryId", CategoryId);
            con = new SqlConnection(MyConnection.GetConnectionString());
            con.Open();

            String selectquery = "select * from attribute where category_id="
                + CategoryId;
            comm = new SqlCommand(selectquery, con);
            SqlDataReader reader = comm.ExecuteReader();

            Table table = new Table();
            table.Style.Add("margin", "auto");
            table.ID = "Table1";
            Page.Form.Controls.Add(table);
            int i = 0,j = 0;
            while (reader.Read() != false)
            {
                Label1.Text = Label1.Text + "<br />" + reader.GetString(reader.GetOrdinal("title"));

                TableRow row = new TableRow();

                TableCell cell = new TableCell();
                Label lb = new Label();
                lb.Text = reader.GetString(reader.GetOrdinal("title"));
                lb.ID = Convert.ToString(i);
                cell.Controls.Add(lb);
                row.Cells.Add(cell);

                TableCell cell2 = new TableCell();
                TextBox tb = new TextBox();
                tb.ID = Convert.ToString(j);
                cell2.Controls.Add(tb);
                row.Cells.Add(cell2);
                i++;
                j++;
                table.Rows.Add(row);
            }
            ViewState.Add("AttributeCounti", i);
            ViewState.Add("AttributeCountj", j);
            Panel1.Controls.Add(table);
            reader.Close();
        }
    }
    protected void btnproattribute_Click(object sender, EventArgs e)
    {
        int ProductId, CategoryId, AttributeCounti, AttributeCountj;
        
        ProductId = Convert.ToInt32(ViewState["ProductId"].ToString());
        CategoryId = Convert.ToInt32(ViewState["CategoryId"].ToString());
        AttributeCounti = Convert.ToInt32(ViewState["AttributeCounti"].ToString());
        AttributeCountj = Convert.ToInt32(ViewState["AttributeCountj"].ToString());
       
        String name, value;
        for (int i = 0; i < AttributeCounti; i++)
        {

//--> error are here to not fetch the label name for getordinal to title...

            name = (Request.Form[Convert.ToString(i)].ToString());
            Label2.Text = Label2.Text + "<br /> name" + name;     
        }

        for(int j=0;j<attributecountj;j++)>
        {

//---> here not a problem.

                value = (Request.Form[Convert.ToString(j)].ToString());
                Label2.Text = Label2.Text + "<br /> value" + value;     
        }

//also to how database to store name and value plz give suggestion...
    }
}
Posted
Updated 19-Mar-15 22:32pm
v2

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