locked
Cannot implicitly convert type 'int' to 'string' RRS feed

  • Question

  • User2109257026 posted

    I am getting the following error Cannot implicitly convert type 'int' to 'string', how do fix this?

            protected void btnSelectAction_Click(object sender, EventArgs e)
            {
                //get row clicked
                GridViewRow gvr = ((Button)sender).NamingContainer as GridViewRow;
    
                //store action in session
                ((Session["SessionObject"]) as SessionClass).CurrentAction = ((Session["SessionObject"]) as SessionClass).CurrentFeedback.ActionsList[gvr.RowIndex]; ;
    
                //populate action fields
                txtIndicators.Text = ((Session["SessionObject"]) as SessionClass).CurrentAction.Indicator_Desc;
                txtChecked.Text = ((Session["SessionObject"]) as SessionClass).CurrentAction.Number_Checked;
                txtErrors.Text = ((Session["SessionObject"]) as SessionClass).CurrentAction.Errors;
                txtUnitNumberwithErrors.Text = ((Session["SessionObject"]) as SessionClass).CurrentAction.UnitNos_WithErrors;
    
                //update button view
                btnCancelAction.Visible = true;
                btnCancelAction.Enabled = true;
                btnAddAction.Text = "Update";
            }

    I tried Int32.Parse(txtErrors.Text) to the begginning but that failed.

     

    Friday, July 11, 2014 7:42 AM

Answers

  • User281315223 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 7:59 AM

All replies

  • User1633621018 posted

    Hi,

    You can use below code:

    int number;
    Int32.TryParse(value, out number);

    Thanks

    Pawan

    Friday, July 11, 2014 7:53 AM
  • User2109257026 posted

    thanks for the solution, but i'm not too sure where in my original code i can add it to ?

    Friday, July 11, 2014 7:56 AM
  • User281315223 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 11, 2014 7:59 AM