2

My code is working properly except when I try to Retrieve float/reel number from database , it return only the integer part as the screenshots shows. the value in the database is 145.55 but the textbox show 145 only.

*I tried with MessageBox to make sure the problem is not in the textbox but in the value returned by the sql query.

Table description

enter image description here

Sql query result

enter image description here

Form result

enter image description here

iteminformation.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace Posfaction
{
    public partial class frmItemInformation : Form
    {
        string fileExtension = ".jpg";
        string ITEM_ID, WarehouseID;
        public frmItemInformation(String VAR_ITEM_ID, String VAR_WarehouseID)
        {
            InitializeComponent();
            ITEM_ID = VAR_ITEM_ID.ToString();
            WarehouseID = VAR_WarehouseID.ToString();
            txtItemID.Text = VAR_ITEM_ID.ToString();
        }
    private void ItemDescription() {
        try
        {
            clsUtility.ExecuteSQLQuery("SELECT *  FROM  ItemInformation  WHERE ITEM_ID ='" + ITEM_ID + "'  ");
            if (clsUtility.sqlDT.Rows.Count > 0)
            {
                txtItemName.Text = clsUtility.sqlDT.Rows[0]["ItemName"].ToString();
                txtUnit.Text = clsUtility.sqlDT.Rows[0]["UnitOfMeasure"].ToString();
                txtBatch.Text = clsUtility.sqlDT.Rows[0]["Batch"].ToString();
                cmbGroup.SelectedValue = clsUtility.sqlDT.Rows[0]["GROUP_ID"].ToString();
                cmbDefaultWarehouse.SelectedValue = clsUtility.sqlDT.Rows[0]["WarehouseID"].ToString();
                txtBarcode.Text = clsUtility.sqlDT.Rows[0]["Barcode"].ToString();
                //txtPurchaseCost.Text = clsUtility.sqlDT.Rows[0]["Cost"].ToString();
                txtPurchaseCost.Text = Convert.ToString(clsUtility.sqlDT.Rows[0]["Cost"]);
                txtSalesPrice.Text = clsUtility.sqlDT.Rows[0]["Price"].ToString();
                txtReorderPoint.Text = clsUtility.sqlDT.Rows[0]["ReorderPoint"].ToString();

                if (clsUtility.sqlDT.Rows[0]["VAT_Applicable"].ToString() == "Y") { cbVATapplicable.Checked = true; }
                else { cbVATapplicable.Checked = false; }

                if (clsUtility.sqlDT.Rows[0]["Barcode_Applicable"].ToString() == "Y") { chkApplicableBarcode.Checked = true; }
                else { chkApplicableBarcode.Checked = false; }

                try
                {
                    pictureBox1.ImageLocation = Application.StartupPath + @"\Upload\ItemImage\" + clsUtility.sqlDT.Rows[0]["PhotoFileName"].ToString();
                    pictureBox1.InitialImage.Dispose();
                    fileExtension = Path.GetExtension(clsUtility.sqlDT.Rows[0]["PhotoFileName"].ToString());
                }
                catch (Exception) { pictureBox1.Image = Posfaction.Properties.Resources.No_image_found; }
            }

            clsUtility.ExecuteSQLQuery("SELECT *  FROM  Stock  WHERE ITEM_ID ='" + ITEM_ID + "' AND WarehouseID ='" + WarehouseID + "'  ");
            if (clsUtility.sqlDT.Rows.Count > 0)
            {
                cmbWarehouse.SelectedValue = clsUtility.sqlDT.Rows[0]["WarehouseID"].ToString();
                cmbShelf.SelectedValue = clsUtility.sqlDT.Rows[0]["SHELF_ID"].ToString();
                txtOpeningStock.Text = clsUtility.sqlDT.Rows[0]["Quantity"].ToString();
                try { dtpExpDate.Text = clsUtility.sqlDT.Rows[0]["ExpiryDate"].ToString(); }
                catch (Exception) { }

                if (clsUtility.sqlDT.Rows[0]["Expiry"].ToString() == "Y") { chkExp.Checked = true; }
                else { chkExp.Checked = false; }
            }

            btnSubmit.Enabled = false;
            btnDelete.Enabled = true;
            btnAlter.Enabled = true;

        }
        catch (Exception) { }
    }

    private void frmItemInformation_Load(object sender, EventArgs e)
    {
        this.ControlBox = false;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        btnReset.PerformClick();
        LoadLanguegePack();
        if (ITEM_ID == "0") { }
        else { ItemDescription(); }

    }
}

clsUtility.cs

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Xml;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Windows.Forms;
using System.Data.SQLite;
//using MySql.Data.MySqlClient;
//using System.Data.SqlClient;

namespace Posfaction
{
    class clsUtility
    {
        public static string CnString = Properties.Settings.Default.App_Conn_string;
        public static DataTable sqlDT = new DataTable();
        public static DataTable sqlDT2 = new DataTable();
        public static string UserID;
        public static string UserName;
        public static string UsersPrivilege;

        // Initializing Database Connection
        public static bool DBConnectionInitializing()
        {
            bool functionReturnValue = false;
            try
            {
                SQLiteConnection sqlCon = new SQLiteConnection();
                sqlCon.ConnectionString = CnString;
                sqlCon.Open();
                functionReturnValue = true;
                sqlCon.Close();
            }
            catch (Exception ex)
            {
                functionReturnValue = false;
                Properties.Settings.Default.App_Default_Conn = false;
                Properties.Settings.Default.Save();
                MessageBox.Show("Error : " + ex.Message, "Error establishing the database connection..", MessageBoxButtons.OK, MessageBoxIcon.Error);
                System.Environment.Exit(0);
            }
            return functionReturnValue;
        }
    

    public static DataTable ExecuteSQLQuery(string SQLQuery)
    {
        try
        {
            SQLiteConnection sqlCon = new SQLiteConnection(CnString);
            SQLiteDataAdapter sqlDA = new SQLiteDataAdapter(SQLQuery, sqlCon);
            SQLiteCommandBuilder sqlCB = new SQLiteCommandBuilder(sqlDA);
            sqlDT.Reset();
            sqlDA.Fill(sqlDT);
        }
        catch (Exception ex)
        {
            Properties.Settings.Default.App_Default_Conn = false;
            Properties.Settings.Default.Save();
            MessageBox.Show("Error : " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return sqlDT;
    }
}

}

Guissous Allaeddine
  • 425
  • 1
  • 4
  • 19
  • 1
    Have you try `((double)clsUtility.sqlDT.Rows[0]["Cost"]).ToString()` ? –  May 19 '21 at 14:40
  • 2
    please use ***parameterised queries*** - building SQL queries by concatenation etc. is a recipe for disaster. not only is it a source for many hard to debug syntax errors, it's also a wide, open gate for ***[SQL Injection attacks](https://bobby-tables.com/)***. – Franz Gleichmann May 19 '21 at 14:40
  • [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) –  May 19 '21 at 14:41
  • ((double)clsUtility.sqlDT.Rows[0]["Cost"]).ToString() ; I just tried it as you asked, nothing change. same issue . – Guissous Allaeddine May 19 '21 at 14:46
  • 1
    How is the value stored in the database (string or number)? Seeing a comma in the GUI doesn't mean the value is stored as a string in the database. You are not using parameters in the query so the type of data may not get transferred correctly between the database and the c# application. – jdweng May 19 '21 at 14:56

1 Answers1

3

A value like 145,55 which is stored in the column Cost is actually a string and not REAL, because it contains , as the decimal separator.

Do an update in the table to replace all occurrences of , to . in the column Cost and finally cast it to a REAL number implicitly by adding 0.0:

UPDATE ItemInformation
SET Cost = REPLACE(Cost, ',', '.') + 0.0
WHERE INSTR(Cost, ',')
forpas
  • 160,666
  • 10
  • 38
  • 76