You should be extremely careful when using code like this because if your Session object is null or doesn't exist, you'll receive a bevy of null reference exceptions. I would recommend checking if your Session object exists and then creating a variable to
deal with accessing and manipulating it as seen below :
protected void btnSelectAction_Click(object sender, EventArgs e)
{
// Get the row that was clicked
GridViewRow gvr = ((Button)sender).NamingContainer as GridViewRow;
// Check if your Session value exists
if(Session["SessionObject"] != null)
{
// It exists, so grab it
SessionClass so = Session["SessionObject"] as SessionClass;
// If the casting went through (which it should have), use the element
if(so != null)
{
// Update your Session values
so.CurrentAction = so.CurrentFeedback.ActionsList[gvr.RowIndex];
// Populate fields based on the Session (ensure they are all strings)
txtIndicators.Text = Convert.ToString(so.CurrentAction.Indicator_Desc);
txtChecked.Text = Convert.ToString(so.CurrentAction.Number_Checked);
txtErrors.Text = Convert.ToString(so.CurrentAction.Errors);
txtUnitNumberwithErrors.Text = Convert.ToString(so.CurrentAction.UnitNos_WithErrors);
// Ensure your Session is re-saved and updated
Session["SessionObject"] = so;
// Update your buttons
btnCancelAction.Visible = true;
btnCancelAction.Enabled = true;
btnAddAction.Text = "Update";
}
else
{
// Something went wrong casting your Session
}
}
else
{
// Your Session didn't exist, something is wrong
}
}
Additionally, you'll notice that I used the
Convert.ToString() method above to handle converting your various object types to Strings. It's a fairly safe method to use when converting different object types to strings and without knowing exactly what data types each of your properties were, it might
be the best solution in this case.
Member
59 Points
306 Posts
Cannot implicitly convert type 'int' to 'string'
Jul 11, 2014 07:42 AM|sam233|LINK
I am getting the following error Cannot implicitly convert type 'int' to 'string', how do fix this?
I tried Int32.Parse(txtErrors.Text) to the begginning but that failed.
Contributor
2400 Points
669 Posts
Re: Cannot implicitly convert type 'int' to 'string'
Jul 11, 2014 07:53 AM|PawanPal|LINK
Hi,
You can use below code:
int number;
Int32.TryParse(value, out number);
Thanks
Pawan
https://pawanpalblog.wordpress.com
www.jsengineeringworks.com
Member
59 Points
306 Posts
Re: Cannot implicitly convert type 'int' to 'string'
Jul 11, 2014 07:56 AM|sam233|LINK
thanks for the solution, but i'm not too sure where in my original code i can add it to ?
All-Star
114593 Points
18503 Posts
MVP
Re: Cannot implicitly convert type 'int' to 'string'
Jul 11, 2014 07:59 AM|Rion Williams|LINK
You should be extremely careful when using code like this because if your Session object is null or doesn't exist, you'll receive a bevy of null reference exceptions. I would recommend checking if your Session object exists and then creating a variable to deal with accessing and manipulating it as seen below :
Additionally, you'll notice that I used the Convert.ToString() method above to handle converting your various object types to Strings. It's a fairly safe method to use when converting different object types to strings and without knowing exactly what data types each of your properties were, it might be the best solution in this case.