ASP.Net GridView cell value Error: Input string was not in a correct format

Last Reply one year ago By dharmendr

Posted one year ago

Hello,

I tried to update a checkbox in database but I hot this exception: 'Input string was not in a correct format.'

I share the code with you. I want to update a checkbox in database if it is checked.

    protected void chkRowPlatita_CheckedChanged(object sender, EventArgs e)
    {

        dtFirst = (DataTable)ViewState["dtFirst"];
        dtPlatit = (DataTable)ViewState["dtPlatit"];

        int i = 0;
        foreach (GridViewRow row in GridViewTerm.Rows)
        {
            int myId;
            int FacturaId = Int32.Parse(row.Cells[0].Text); //exception 
            if (((CheckBox)row.FindControl("chkRowPlatita")).Checked)
            {
                dtPlatit.Rows.Add(GridViewTerm.DataKeys[row.DataItemIndex].Value, ((Label)row.FindControl("lblNume")).Text, ((Label)row.FindControl("lblDataSc")).Text, ((Label)row.FindControl("lblSumaP")).Text);
                dtFirst.Rows.RemoveAt(row.DataItemIndex - i);
                i++;

                updaterow(FacturaId, "true");
            }
        }
        GridViewPlatit.DataSource = dtPlatit;
        GridViewPlatit.DataBind();
        GridViewTerm.DataSource = dtFirst;
        GridViewTerm.DataBind();
        ViewState["dtFirst"] = dtFirst;
        ViewState["dtPlatit"] = dtPlatit;
    }

    private void updaterow(int FacturaId, String Platit)
    {
        String CS = ConfigurationManager.ConnectionStrings["BudgetTrackDBConnectionString1"].ConnectionString;
        String updatedata = "Update Facturi set Platit='" + Platit + "' where FacturaId=" + FacturaId;
        SqlConnection con = new SqlConnection(CS);
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = updatedata;
        cmd.Connection = con;
        cmd.ExecuteNonQuery();

    }

How to solve my issue?

        <asp:GridView ID="GridViewTerm" runat="server" HorizontalAlign="Center" DataKeyNames="FacturaId" OnSorting="GridViewTerm_Sorting" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" EmptyDataText="Nu există facturi!" OnPageIndexChanging="OnPageIndexChanging" PageSize="4">
            <Columns>
                <asp:TemplateField HeaderText="Factura ID" Visible="false">
                    <ItemTemplate>
                        <asp:Label ID="lblId" runat="server" Text='<%# Eval("FacturaId") %>' ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Nume">
                    <ItemTemplate>
                        <asp:Label ID="lblNume" runat="server" Text='<%# Eval("Nume") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Data Scadenta">
                    <ItemTemplate>
                        <asp:Label ID="lblDataSc" runat="server" Text='<%# Eval("Data_Scadenta") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Suma">
                    <ItemTemplate>
                        <asp:Label ID="lblSumaP" runat="server" Text='<%# Eval("Suma_Plata") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Plătită">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkRowPlatita" runat="server" AutoPostBack="true" OnCheckedChanged="chkRowPlatita_CheckedChanged" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Parțial plătită">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkRowPartialPlatita" runat="server" AutoPostBack="true" OnCheckedChanged="chkRowPartialPlatita_CheckedChanged" />
                    </ItemTemplate>
                </asp:TemplateField> 
            </Columns> 
        </asp:GridView>

 

Posted one year ago

Hi ada20,

ada20 says:
int FacturaId = Int32.Parse(row.Cells[0].Text);

You are using ItemTemplate and trying to access from Cell Text property. Thats why the error. You can't access with cell value.

Use Find control method to find the Control in the row and from that control get the Text.

Use the below code.

int FacturaId = Int32.Parse((row.FindControl("lblId") as Label).Text);