ASP.Net GridView RowUpdating Error converting data type varchar to numeric

Last Reply one year ago By pandeyism

Posted one year ago

Hi to all,

Error converting data type varchar to numeric.

I get an exception from my application. It is thrown on cmd.ExecuteNonQuery(). I tried to update a row in database. In db the type is decimal(10,2), I convert it to decimal when it updated but does not work. How to resolve this convertion?

    protected void GridViewIncomes_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SqlConnection sqlCon = new SqlConnection(CS);
        TextBox tbDenumire = (TextBox)GridViewIncomes.Rows[e.RowIndex].FindControl("tbDenumire");
        TextBox tbSuma = (TextBox)GridViewIncomes.Rows[e.RowIndex].FindControl("tbSuma");
        TextBox tbData = (TextBox)GridViewIncomes.Rows[e.RowIndex].FindControl("tbData");
        
        TextBox tbDescriere = (TextBox)GridViewIncomes.Rows[e.RowIndex].FindControl("tbDescriere");
        int VenitId = Convert.ToInt32(GridViewIncomes.DataKeys[e.RowIndex].Values[0]);
        string sql = "UPDATE Venituri SET Denumire='" + tbDenumire.Text + "',Data='" + tbData.Text + "',Descriere='" + tbDescriere.Text + "',Suma='" + Convert.ToDecimal(tbSuma.Text) + "' WHERE VenitId=" + VenitId + "";

        SqlCommand cmd = new SqlCommand(sql, sqlCon);
        sqlCon.Open();
        * cmd.ExecuteNonQuery();
        sqlCon.Close();
        if (cmd.ExecuteNonQuery() == 1)
        {

            lblSuccessMessage.Text = "Actualizat cu succes!";
            GridViewIncomes.EditIndex = -1;
            GridViewIncomes.DataBind();
        }
       
        lblSuccessMessage.Text = "";
    }

 

<asp:TemplateField HeaderText="Suma">
    <EditItemTemplate>
        <asp:TextBox ID="tbSuma" runat="server" Text='<%# Bind("Suma") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lbSuma" runat="server" Text='<%# Bind("Suma") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

This looks the database:

[VenitId]   INT             IDENTITY (1, 1) NOT NULL,
[Denumire]  NVARCHAR (20)   NOT NULL,
[Data]      NVARCHAR (50)   NOT NULL,
[Descriere] NVARCHAR (MAX)  NULL,
[Suma]      DECIMAL (10, 2) NULL,

 

You are viewing reply posted by: pandeyism one year ago.
Posted one year ago