Insert two Decimal place value in Database using jQuery AJAX and C# in ASP.Net

Last Reply 6 months ago By pandeyism

Posted 6 months ago

I am facing issue when saving the float values in DB.

I am creating a Textbox using HTML code, user has to key in any numeric value(decimal or without decimal) in it, and on click of save button, this value has to save in DB. When I enter any value without decimal, it works fine, but when I enter decimal value in textbox Ex: 10.1, it goes and save as 10.1000003814697

When I debug my code the parameter("txtValue") is going as 10.1 only, but when I check in DB the value saves as 10.1000003814697

In DB table the column DataType is float.

Requirement: -------------

I want to get the numeric values with 2 decimals places from computed variable without rounding off. i.e. 123456.646

then result should be 123456.64 instead of 123456.65

C# Code: ---------

Module Class to save Data in DB: ---------------------------------

[DataObjectMethod(DataObjectMethodType.Update)]
public void SavePIRTextboxValue(string PIRLabel, float textboxPIRValue, string ProjProcessSeqId, string ProjectId, string ProcessCode)
{
	Modules.Project obj = new Modules.Project();            
	obj.InsertEPValue(PIRLabel, textboxPIRValue, ProjProcessSeqId, ProjectId, ProcessCode); //-- getting textboxPIRValue = 10.1 -- here is the parameter   
}

public void InsertEPValue(string PIRLabel, float textboxPIRValue, string ProjProcessSeqId, string ProjectId, string ProcessCode)
{
	int res = pvSaveData(PIRLabel, textboxPIRValue, ProjProcessSeqId, ProjectId, ProcessCode); //-- getting textboxPIRValue = 10.1 -- here is the parameter
}

private int pvSaveData(string PIRLabel, float textboxPIRValue, string ProjProcessSeqId, string ProjectId, string ProcessCode)
{
	int result = 0;
	try
	{
		result = base.Context.ExecuteNonQuery(SqlMaps.Project.InsertUpdateTextboxValue()
					, new SqlParameter("@gPIRLabel", PIRLabel)
					, new SqlParameter("@gtextboxPIRValue", textboxPIRValue) //-- getting textboxPIRValue = 10.1 -- here is the parameter
					, new SqlParameter("@gProjProcessSeqId", ProjProcessSeqId)
					, new SqlParameter("@gProjectId", ProjectId)
					, new SqlParameter("@gProcessCode", ProcessCode));
	}             
	catch(Exception ex)
	{                
		throw ex;
	}
	return result;
}

internal static string InsertUpdateTextboxValue()
{
	return @"
		DECLARE @RC int
		DECLARE @lblValue VARCHAR(10)
		DECLARE @textboxPIRValue float -- here is the parameter
		DECLARE @ProjProcessSeqId uniqueidentifier
		DECLARE @ProjectId uniqueidentifier
		DECLARE @ProcessCode varchar(50)

		SET @lblValue = @gPIRLabel
		SET @textboxPIRValue = @gtextboxPIRValue
		SET @ProjProcessSeqId = @gProjProcessSeqId
		SET @ProjectId = @gProjectId
		SET @ProcessCode = @gProcessCode

		EXECUTE @RC = [dbo].[P_UpdatePIRTextboxValue]
		   @lblValue 
		  ,@textboxPIRValue
		  ,@ProjProcessSeqId
		  ,@ProjectId
		  ,@ProcessCode

		SELECT	'Return Value' = @RC
		";
}

Kindly let me know what is wrong in my code.

Thanks in advance.

Posted 6 months ago

Hey Richa,

Please refer below sample.

HTML

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    function saveData() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/SaveTextboxValues",
            data: '{txtValue: "' + $("#<%=textboxId.ClientID%>")[0].value + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }
</script>
<div>
    <asp:TextBox runat="server" ID="textboxId" />
    <asp:Button Text="Save" runat="server" OnClientClick="saveData();" />
</div>

Namespaces

C#

using System.Web.Services;
using System.Web.Script.Services;
using System.Data.SqlClient;
using System.Configuration;

Code

C#

[WebMethod]
[ScriptMethod()]
public static void SaveTextboxValues(float txtValue)
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("INSERT INTO TestDecimalValues VALUES(@FloadValue)", con))
        {
            if (Convert.ToDouble(txtValue).ToString().IndexOf('.') > -1)
            {
                cmd.Parameters.AddWithValue("@FloadValue", Convert.ToDouble(txtValue).ToString().Substring(0, Convert.ToDouble(txtValue).ToString().IndexOf('.') + 3));
            }
            else
            {
                cmd.Parameters.AddWithValue("@FloadValue", txtValue);
            }
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}