Reputation: 149
i have a repeater bounded with records from from a source. the content of the repeater looks like this
Science 5 67
Art 3 57
at the end of the day, i want to get the result of this repeater like this {Science, Art}{5,3}{67,57}
so i can save into the database. If you have any other way i can get to save that table in the database for a particular Student, Kindly suggest.
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<tr>
<td class="ctg-type">
<strong> <%#Eval("SubjectName") %> </strong> <span><strong> <%#Eval("Unit") %></strong></span>
</td>
<td class="cgt-des">
<%# Eval("Score") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
repeater.DataSource = getSudentRecord();
repeater.DataBind();
}
public DataTable getStudentRecord()
{
using (SqlCommand command = new SqlCommand("StudentRecord", con))
{
command.CommandType = CommandType.StoredProcedure;
con.Open();
command.ExecuteNonQuery();
var data = new SqlDataAdapter(command);
var Table = new DataTable();
data.Fill(Table);
con.Close();
return Table;
}
}
Upvotes: 0
Views: 1030
Reputation: 310
This approach will parse the DataTable twice:
DataTable foo = getSudentRecord();
string serializedFormat = "{{{0}}},{{{1}}},{{{2}}}";
List<string> subjectnames = new List<string>();
List<string> units = new List<string>();
List<string> scores = new List<string>();
foreach (DataRow row in foo.Rows)
{
subjectnames.Add(row["SubjectName"].ToString());
units.Add(row["Unit"].ToString());
scores.Add(row["Score"].ToString());
}
string serialized = String.Format(serializedFormat,
String.Join(",",subjectnames.ToArray()),
String.Join(",", units.ToArray()),
String.Join(",", scores.ToArray())
);
//finally fill the repeater
repeater.DataSource = foo;
repeater.DataBind();
Upvotes: 1