Convert TextBox value to integet value in rowcommand event in Grid View

I am trying to convert a value within TextBox

an int in RowCommand

gridview

.

else if (e.CommandName == "UpdateRow")
{
    int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
    int TimeSheetId = Convert.ToInt32(e.CommandArgument);
    int Sunday = Convert.ToInt32((TextBox)gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtSunday"));// Error here
    int Monday = Convert.ToInt32((TextBox)gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtMonday"));// Error her
}

      

I am getting the following error:

Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.

      

+3


source to share


2 answers


You find the control and then you try to pass it to an integer, you need to select the Textbox text property and transform it.



 int Sunday = Convert.ToInt32(((TextBox)gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtSunday")).Text);
 int Monday = Convert.ToInt32(((TextBox)gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtMonday")).Text); 

      

0


source


As the error says, you are trying to apply a textbox to an integer. It's impossible. So it should be like this:



int Sunday = Convert.ToInt32((TextBox)(gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtSunday")).Text);
int Monday = Convert.ToInt32((TextBox)(gvEmployeeTimeSheet.Rows[rowIndex].FindControl("txtMonday")).Text);

      

+1


source







All Articles