Conversion Error From String ""

tdignan87

Well-known member
Joined
Jul 8, 2019
Messages
95
Programming Experience
Beginner
Hi i need some help please.
I have a textbox that the user scans into with a scanner and if the barcode exists i want another form to appear to allow them to continue, and if the barcode doesn't exist in the DB another form which will tell them.
I keep getting this flag though. Can anyone help please?

1575152767728.png


C#:
        private void BarcodeTxtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return)
            {
                FbConnectionStringBuilder rfs = new FbConnectionStringBuilder();
                rfs.DataSource =
                    "localhost";
                rfs.Port = ;
                rfs.Database = @"";
                rfs.UserID = "sysdba";
                rfs.Password = "";
                rfs.ServerType = FbServerType.Default;

                FbConnection db = new FbConnection(rfs.ToString());

                FbDataReader myReader = null;

                
                string barcodeQuery = "SELECT COMMODITIES.CODE,COMMODITIES.NAME FROM COMMODITIES WHERE BARCODE  = " + BarcodeTxtBox.Text + "  ";
                FbCommand myCommand = new FbCommand(barcodeQuery, db);

                db.Open();
                myReader = myCommand.ExecuteReader();


            
                myReader.Read();
                int count = Convert.ToInt32(myCommand.ExecuteScalar());

                if (count > 0)
                {
                    MessageBox.Show("Existed!");
                }
                else
                {
                    MessageBox.Show("No record");
                }

            }
        }

I have put messageboxes for now instead of the other forms.

Thanks
 
Varchar(255)
If I remove the barcode textbox and just put the barcode in its place as the query it works fine.. weird...
 
Do you get the same error if you put in an invalid barcode value?

As an aside, you should be using parameterized queries instead of concatenating strings together -- specially since you are just taking the textbox value and sticking it into your query directly. You are opening yourself up to SQL injection attacks.
 
And as another aside, even though you are using WPF, it looks like you are writing WPF code as if you were still in WinForms. WPF shines when you use the MVVM pattern with it. Embedding business logic into your view is the WinForms way of doing things.
 
If the column is varchar then surely you need to wrap any literal values for it in single quotes. If you use parameters, which I assume are available for that ADO.NET provider, then you wouldn't have to worry about such things.
 
I will try single quotes thank you. Yeh same error skydive If invalid barcode regardless. I will check out the MVVM also . This is my first WPF project from moving from win forms :D . Thanks guys
 
Wrapping the textbox in single quotes did the trick.
Looking to start a youtube tutorial on MVVM tonight to get me started :)

Thanks all,
Tom
 
Back
Top Bottom