Reputation: 7285
I have a grid view. I want to take the selected row from the grid view and pass on a column value in that row to a new page while storing that value in the session.
So far I know how to store it as a string from a tut, but don't know how to keep the value in int format not convert it to string.
Here is what I have in my C# code...Like I said this does write out the row as a string type I want it to remain a int.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
Int16 num = Convert.ToInt16(e.CommandArgument);
TextBox2.Text = GridView1.Rows[num].Cells[0].Text;
}
}
Putting it into session will be easy. I figured out it is:
Session["customerID"] = GridView1.Rows[num].Cells[0].Text;
So now the true question is how to keep the value in a int format.
Thanks!
Upvotes: 0
Views: 2079
Reputation: 4019
You get the value back by doing something like this:
var myInt = (int)Session["customerID"]
Session variables can store any object, not just strings. You could technically store your whole gridview object in a session variable.
Upvotes: 1
Reputation: 6123
You can not save it in int type directly.Session stores the value in object type. So you have to cast it as int.
int customerID = (int) Session["customerID"];
// or
int customerID = Convert.ToInt32( Session["customerID"].ToString());
// or
int customerID = int.Parse( Session["customerID"].ToString());
Upvotes: 1