I'm having a problem storing and displaying values from a datatable. My goal is to add a value to a datatable, reload the page, and then display the datatable with the added value and give the ability to enter more. Currently, I can't even get the data table to show up. I am sure that it is an issue with saving the data in a session, but I simply cannot get the saving/loading from session to work.
I appericate any help you guys can give me, and I apologize if it's something really blindingly simple. Seems I just need a second opinion on it.
This is my current webpage,
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p><asp:TextBox ID="txtName" runat="server"></asp:TextBox><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><asp:TextBox ID="txtAddress" runat="server"></asp:TextBox><asp:TextBox ID="txtInfo" runat="server"></asp:TextBox></p>
<p>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click"/></p>
</div>
<div>
<table border="1">
<asp:Repeater ID="rptrData" runat="server">
<HeaderTemplate>
<tr>
<td>Name</td>
<td>Email</td>
<td>Address</td>
<td>Info</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%#Eval("Name") %></td>
<td><%#Eval("Email") %></td>
<td><%#Eval("Address") %></td>
<td><%#Eval("Info") %></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr>
<td><%#Eval("Name") %></td>
<td><%#Eval("Email") %></td>
<td><%#Eval("Address") %></td>
<td><%#Eval("Info") %></td>
</tr>
</AlternatingItemTemplate>
</asp:Repeater>
</table>
</div>
</form>
</body>
</html>
And my current code behind,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data;
using System.Collections;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
public DataTable MyDT = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable MyDT = (DataTable)Session["MyDT"];
rptrData.DataSource = MyDT;
rptrData.DataBind();
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
//DataTable MyDT = new DataTable();
MyDT.Columns.Add("Name");
MyDT.Columns.Add("Address");
MyDT.Columns.Add("Email");
MyDT.Columns.Add("Info");
string Name = txtName.Text;
string Address = txtAddress.Text;
string Email = txtEmail.Text;
string Info = txtInfo.Text;
DataRow dtRow = MyDT.NewRow();
dtRow["Name"] = Name;
dtRow["Address"] = Address;
dtRow["Email"] = Email;
dtRow["Info"] = Info;
Session.Add("MyDT", MyDT);
}
}
}