0

I have a gridview with three textboxes txtOpeningAdv , TxtAdvanceDeducted , TxtClosingAdvance..... Using a KeyUp function on TxtAdvanceDeducted i calculated TxtClosingAdvance... My page Shows value in TxtClosingAdvance textbox... But when i accessed it using c# it gives me error Input String was not in a correct format...

When i inspected through firebug,

<input type="text" class="text_box_height_14_width_50" id="ctl00_ContentPlaceHolder1_gridEmployee_ctl02_txtOpeningAdv" readonly="readonly" value="500.00" name="ctl00$ContentPlaceHolder1$gridEmployee$ctl02$txtOpeningAdv">

<input type="text" onkeyup="totalAmount(event,this);" autocomplete="off" class="text_box_height_14_width_50" id="ctl00_ContentPlaceHolder1_gridEmployee_ctl02_TxtAdvanceDeducted" name="ctl00$ContentPlaceHolder1$gridEmployee$ctl02$TxtAdvanceDeducted">

<input type="text" class="text_box_height_14_width_50" id="ctl00_ContentPlaceHolder1_gridEmployee_ctl02_TxtClosingAdvance" readonly="readonly" name="ctl00$ContentPlaceHolder1$gridEmployee$ctl02$TxtClosingAdvance">

This is my gridview, alt text http://img90.imageshack.us/img90/7237/gridemp.jpg

My c# Code,

foreach (GridViewRow row in gridEmployee.Rows) 
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                DataRow dr = dt.NewRow();
                dr["EmpId"] = Convert.ToInt64(((HiddenField)row.Cells[0].FindControl("HiddenId")).Value);
                dr["FromDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(fromdate[1].ToString()) + '/' + fromdate[0].ToString() + '/' + fromdate[2].ToString());
                dr["ToDate"] = Convert.ToDateTime(GetMonthNumberFromAbbreviation(todate[1].ToString()) + '/' + todate[0].ToString() + '/' + todate[2].ToString());
                dr["DaysPresent"] = Convert.ToDecimal(((TextBox)row.Cells[3].FindControl("TxtDaysPresent")).Text);//(row.Cells[4].Text);
                dr["OpeningAdvance"] = Convert.ToDouble(((TextBox)row.Cells[4].FindControl("txtOpeningAdv")).Text);
                dr["AdvanceDeducted"] = Convert.ToDouble(((TextBox)row.Cells[5].FindControl("TxtAdvanceDeducted")).Text);
                dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);
                dr["SalaryGiven"] = Convert.ToDouble(((TextBox)row.Cells[7].FindControl("TxtSalary")).Text);
                dr["CreatedDate"] = System.DateTime.Now;
                dt.Rows.Add(dr);
            }
        }

Error is in the line, dr["RemainingAdvance"] = Convert.ToDouble(((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text);

Input string was not in a correct format

But my gridview has TxtClosingAdvance="400.00"... Its a readonly textbox where its value will be placed from TxtAdvanceDeducted onkeyup event javascript...

1 Answer 1

2

As a guess, your string value is empty or contains unwanted symbols, try to check the value of ((TextBox)row.Cells[6].FindControl("TxtClosingAdvance")).Text if it contains 400.00 than the error is caused because in server's regional settings "." is not decimal symbol. If it is empty, that's mean ASP.NET doesn't recognizes your JavaScript changes. ASP.NET restores non editable control values from VIEWSTATE on response for security reasons, try to disable VIEWSTATE for that control by setting EnableViewState = false.

EDIT

By the way there is an ReadOnly TextBox issue discussed here

Hope this helps

Sign up to request clarification or add additional context in comments.

2 Comments

@Arsen it has been discussed for a textbox in a page... But my textbox is inside a gridview ... How can i add readonly property ... Should i use rowdatabound event...
yea try to set that attribute on rowdatabound event

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.