DataTable in ASP.Net Session

I have to show users entered parameters in Asp.net Gridview example some values from dropdownlist textboxes and startDate EndDate etc . I am getting these values from user and add them to a Temporary dataTable . i am adding each row to DatTable on every add button call it goes fine for first time means for first row when i try to enter another row on Add button Click i overWrites on previous row and shows me only one row.

Here is my code :

BusinessLayer.LaunchPortfolioTest objAddParametersToDataTable = new BusinessLayer.LaunchPortfolioTest();
DataTable TempTable;

if (Session["TempTable"] == null)
{
    TempTable = objAddParametersToDataTable.TempTableView();
    Session["IDcolumn"] = 1;
}
else
{
    Session["IDcolumn"] = Convert.ToInt16(Session["IDcolumn"]) + 1;
    TempTable = (DataTable)Session["TempTable"];
}
int ID = Convert.ToInt16(Session["IDcolumn"]);
string nome = Utilities.formatShortDate(Convert.ToDateTime(txtEndDate.Text.ToString()));
string priority = drpPriority.SelectedValue.ToString();
string portfolioCode = drpPortfolioCode.SelectedValue.ToString();
DateTime startDate = Convert.ToDateTime(txtStartDate.Text.ToString());
DateTime endDate = Convert.ToDateTime(txtEndDate.Text.ToString());
string currency = drpCalculationCurrency.SelectedValue.ToString();
string author = lblLoginnedUser.Text.ToString();
TimeSpan Difference = endDate.Date.AddDays(1) - startDate.Date;
//Adding Parameters to DataTable:
objAddParametersToDataTable.AddRow(ID, priority, author, nome, portfolioCode, Convert.ToString(startDate), Convert.ToString(endDate), currency, TempTable);

grdViewReport.DataSource = TempTable;
grdViewReport.DataBind();

Session.Add("TempTable", objAddParametersToDataTable.TempTableView());

TempTableView method:

public DataTable TempTableView()
{
    DataTable TempAnalysisTable = new DataTable();
    DataColumn identity = new DataColumn("IDcolumn", typeof(int));
    TempAnalysisTable.Columns.Add(identity);
    TempAnalysisTable.Columns.Add("Priority", typeof(string));
    TempAnalysisTable.Columns.Add("Author", typeof(string));
    TempAnalysisTable.Columns.Add("Name", typeof(string));
    TempAnalysisTable.Columns.Add("PortfolioCode", typeof(string));
    TempAnalysisTable.Columns.Add("StartDate", typeof(string));
    TempAnalysisTable.Columns.Add("EndDate", typeof(string));
    TempAnalysisTable.Columns.Add("Currency", typeof(string));        
    return TempAnalysisTable;
}

Add Method Code IS here:

public void AddRow(int id,string priority, string author, string name, string portfolioCode, string startDate, string endDate, string currency, DataTable TempAnalysisTable)
{
    TempAnalysisTable.Rows.Add(new object[] {id, priority, author, name, portfolioCode, startDate, endDate, currency });
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Am not sure for you scenario. But here is the thing for adding grid view dynamically without using database.

 protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        DataTable date = new DataTable();
        date.Columns.Add("Column !", typeof(string));
        date.Columns.Add("Column 2", typeof(string));
        Session["dte"] = date;
     }
 }

protected void addbutton_Click(object sender, ImageClickEventArgs e)
{
    DataTable date = (DataTable)Session["dte"];
    DataRow dr = date.NewRow();
    dr["Column 1"] = TextBox1.Text.Trim();// Your Values
    dr["Column 2"] = TextBox2.Text.Trim();// Your Values
    date.Rows.Add(dr);
    GridView1.DataSource = date;
    GridView1.DataBind();
}

It may help you to over come this hurdle. Please let me know your further queries in this case.

Method 2

You need to get the previous datatable from session and read it and put rows in it. This should be done at addrow AddButton_Click event.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x