Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to store one by one data dynamically in DetailsView from DataTable and display result in DetailsView.
After Store data into database table then display.
Suppose, Table attribute of "Product" table are
1. ID (Int) (Auto Increment)
2. Features (Varchar2(50))

I want to store "Product name", "Color" and "Price" into only one column i.e, "Features";
C#
protected void Button1_Click(object sender, EventArgs e)
{
string item1 = TextBox1.Text + "\n"; // Product Name
string item2 = TextBox2.Text + "\n"; // Color
string item3 = TextBox3.Text + "\n"; // Price
string AllItem = item1 + item2 + item3; // Suppose: AllItem= Nokia 5230\nBlack\n7500
con.Open();
cmd.Connection = con;
cmd.CommandText = "insert into product values('" + AllItem + "')";
cmd.ExecuteNonQuery();
con.Close();
}

Then I try to store value separately into DataTable Then display DetailsView:
C#
protected void Button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
con.Open();
cmd.Connection = con;
cmd.CommandText = "Select Features from Product where id=5";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string result = dr["Features"].ToString();
int ln = result.Length;
dt.Columns.Add("items");
int index = result.IndexOf("\n");
while (ln > 0)
{
string a = result.Substring(0, index);
dt.Rows.Add(a);
ln = ln - (index + 1);
result = result.Substring(index + 1, ln);
index = result.IndexOf("\n");
}
}
int len = dt.Rows.Count;
for (int i = 0; i < len; i++)
{
DetailsView1.Rows.Cells.Text = dt.Rows[1].ToString();
}
DetailsView1.DataBind();
}

}

Store value in DataTable dt. That is
C#
dt.Rows[0][0]=Nokia 5230
dt.Rows[1][0]-Black
dt.Rows[2][0]=7500


I want to store these value in following structure in DetailsView
Product Name: Nokia 5230
Color: Black
Price: 7500

But can't be store.
Please solve my problem.
Posted
Updated 8-Aug-13 9:41am
v5

1 solution

Best solution for this would be to create a asp.net Repeater and bind the item to it.
 
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