Joshua Slocum Joshua Slocum - 1 year ago 54
ASP.NET (C#) Question data entry form

I want to build a form where users can enter some data in a few text boxes and click the "Add" button and have the the data appear in a grid or something like it. They need to be able to enter multiple rows.

Then when they are done they can click the "Save" button to save the data to the database.

How do I get the data from from the text boxes into the "grid"?


Here's what I have so far

protected void Page_Load(object sender, EventArgs e)


DataTable myDataTable = new DataTable();

DataColumn dc1 = new DataColumn("Employee");
DataColumn dc2 = new DataColumn("Category");
DataColumn dc3 = new DataColumn("Description");
DataColumn dc4 = new DataColumn("P/S/N");
DataColumn dc5 = new DataColumn("Hours");
DataColumn dc6 = new DataColumn("WeekEnding");

protected void btnAddToGrid_Click(object sender, EventArgs e)

DataRow row = myDataTable.NewRow();// i'm getting error here sayind myDataTable does not exist

row["Employee"] = LoginName1.ToString();
row["Category"] = ddlCategory.SelectedValue;
row["Description"] = txtDescription.Text;
row["P/S/N"] = ddlPSN.SelectedValue;
row["Hours"] = ddlHours.SelectedValue;
row["WeekEnding"] = txtWeekEnding.Text;


Answer Source

Ok your first problem from your comment:

i'm getting error here sayind myDataTable does not exist

Is because you defined your table in the Page_Load and then it goes out of scope at the end of the function. It sounds like you don't understand the basic concepts of ASP.NET and what you are trying to do.

Here is a quick and dirty untested solution to your problem but I must stress that you should try and understand why this solution works before trying to extend it or do more in ASP.NET. I hope my 10 minutes of my time helps you get a good start into understanding C# and ASP.NET.

public class YourData
    public string Employee { get; set; }
    public string Category { get; set; }
    public string Description { get; set; }
    public string P_S_N { get; set; }
    public string Hours { get; set; }
    public string WeekEnding { get; set; }

// Used to add your list of data to the viewstate cache
// (There are other ways to store data between posts but I am trying to keep it simple)
public void SetYourCachedData(List<YourData> data)
    ViewState["YourDataCache"] = data;

// Used to get your save data so far
public List<YourData> GetYourCachedData()
    return ViewState["YourDataCache"] == null ?
        new List<YourData>() : (List<YourData>)(ViewState["YourDataCache"]);

protected void Page_Load(object sender, EventArgs e)
    // Do nothing

protected void btnAddToGrid_Click(object sender, EventArgs e)
    // Get the data and store it in the ViewState to cache it between post backs
    // Assuming one record added with each click.
    List<YourData> data = GetYourCachedData();
    data.Add(new YourData()
        Employee = LoginName1.ToString(),
        Category = ddlCategory.SelectedValue,
        Description = txtDescription.Text,
        P_S_N = ddlPSN.SelectedValue,
        Hours = ddlHours.SelectedValue,
        WeekEnding = txtWeekEnding.Text

    // You can bind to any type of collection easily.
    // You don't need to use a DataTable with a GridView
    yourGrid.DataSource = data;


protected void btnSaveData_Click(object sender, EventArgs e)
    // Pull data from the ViewState cache
    List<YourData> data = GetYourCachedData();

    // Now iterate through data to save it to your database...
    // look this up if you don't know how as this is a lot more work.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download