How to store one by one data dynamically in DetailsView from DataTable.

Posted by prakashmondal5-21103 under ASP.NET on 8/8/2013 | Points: 10 | Views : 881 | Status : [Member] | Replies : 2
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";
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:
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[i].Cells[i].Text = dt.Rows[i][1].ToString();
}
DetailsView1.DataBind();
}

}
Store value in DataTable dt. That is
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 and display in DetailsView
Product Name: Nokia 5230
Color: Black
Price: 7500

But can't be store.
Please solve my problem.




Responses

Posted by: Satyapriyanayak on: 8/9/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DetailsView_insert._Default" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px"
onpageindexchanging="DetailsView1_PageIndexChanging" AllowPaging="true">
</asp:DetailsView>
</div>
<asp:Label ID="Label1" runat="server" Text="ProductName" Width="100px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="Color" Width="100px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="Price" Width="100px"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Button ID="btn_insert" runat="server" Text="Insert"
onclick="btn_insert_Click" />
<asp:Label ID="lblmsg" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace DetailsView_insert
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlDataAdapter sqlda = new SqlDataAdapter();
SqlCommand com = new SqlCommand();
DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
private void bindgrid()
{
SqlConnection conn = new SqlConnection(connStr);
dt = new DataTable();
com.Connection = conn;
com.CommandText = "SELECT * FROM product";
sqlda = new SqlDataAdapter(com);
sqlda.Fill(dt);
DetailsView1.DataSource = dt;
DetailsView1.DataBind();

}

protected void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
{
DetailsView1.PageIndex = e.NewPageIndex;
bindgrid();
}

protected void btn_insert_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
com = new SqlCommand();
com.Connection = conn;
com.CommandType = CommandType.Text;
com.CommandText = "insert into product values(@ProductName,@Color,@Price)";
com.Parameters.Clear();
com.Parameters.AddWithValue("@ProductName", TextBox1.Text);
com.Parameters.AddWithValue("@Color", TextBox2.Text);
com.Parameters.AddWithValue("@Price", TextBox3.Text);
if (conn.State == ConnectionState.Closed)
conn.Open();
com.ExecuteNonQuery();
conn.Close();
lblmsg.Text = "Data entered successfully!!!";
bindgrid();
}
}
}


If this post helps you mark it as answer
Thanks

prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: prakashmondal5-21103 on: 8/9/2013 [Member] Starter | Points: 25

Up
0
Down
Hi! Satyapriyanayak ,

You don't understand my question. Please see ones again my question.
ProductName,Color and Price are not attribute. These are combine attribute value. Only one Attribute is "Features".
The value of the attribute named "features" is Nokia 5230\nBlack\n7500 .
these value separately store and datatable.
Store value in DataTable dt. That is
dt.Rows[0][0]=Nokia 5230
dt.Rows[1][0]-Black
dt.Rows[2][0]=7500

but I don't display them in DetailsView in tabular format that is:

Product Name: Nokia 5230
Color: Black
Price: 7500

Please give do this solution completely.


prakashmondal5-21103, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response