Hi Team
I have a barcode app, somehow my connection string are correct, but when i try to run this app its giving me this weird and dont know how to resolve it. See below the screen show for more details,

Hi Team
I have a barcode app, somehow my connection string are correct, but when i try to run this app its giving me this weird and dont know how to resolve it. See below the screen show for more details,

Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaimin ShethiyaPosted Jul 22, 2024, 9:13 AM
Hello,
Your database connection was closed that's why it throw an error.
Please refer the below code.
Thanks
Amit MohantyPosted Jul 22, 2024, 6:44 AM
Try this:
Nikunj SatasiyaPosted Jul 21, 2024, 7:14 PM
Hi Gcobani,
As i can review your code, the error you're encountering when using ExecuteScalar() with the query SELECT idx FROM [dbo].[Adr_Batch_Materials] WHERE Code = 'PREMIX' is likely due to the query potentially returning multiple rows.
The ExecuteScalar() method is designed to retrieve a single value.
Solution 1:
If you expecting only single record as per your WHERE condition, you shoud check your data there might duplicate record exist in the table [dbo].[Adr_Batch_Materials] WHERE Code = 'PREMIX'
Solution 2.:
Modify the your query if you expecting single value return by your query.
OR
You can take refrence from this sample code:
Note:
When you expect your query to return multiple rows, you should use methods that are designed to handle multiple results, such as ExecuteReader() in ADO.NET. This method provides a forward-only, read-only iterator over the result set that allows you to handle each row returned by the query one at a time. Here’s how you can use ExecuteReader() to process multiple rows.
Vishal JoshiPosted Jul 20, 2024, 6:04 AM
Hello,
As per the exception message, you need to open a connection before executing the command. Try the below code.
Thanks
Amit MohantyPosted Jul 19, 2024, 2:17 PM
Make sure you are opening the connection before executing the command.