Extracting data from a database based on the selection of an item from the combo box

I have maintained a database dbo.Devices that stores information about paired Bluetooth Devices. I have attached the database file image . I have also created a combo box cmb_UsersDisplay that displays the list of Devices that is stored in the database. Now I want to store the Folder_Path of the device name that i select from the combo box cmb_UsersDisplay in a variable and display it on the console. I have tried to execute it using the below code, but the string displays the command not the the Folder path from the database for the device name I have selected from the combo box. For example: If I select a device name Moto E4 plus from the combo box and its folder path is stored in the database ,then it must display the Folder path associated with that device name on the console.
Code:
{
string selected_item = this.cmb_UsersDisplay.GetItemText(this.cmb_UsersDisplay.SelectedItem);
            SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\HP\Documents\Visual Studio 2015\Projects\Folder_Security\Folder_Security\Database1.mdf; Integrated Security = True");
            con.Open();
            string Sql = "select File_Path from dbo.Devices where DeviceName=selected_item";
            SqlCommand cmd = new SqlCommand(Sql, con);
            Console.WriteLine(Sql);
}
Devices-database.PNG
Check-range-form.PNG
Abhishek ModakStudentAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Abhishek;

Try using this line in place of yours.
string Sql = "select File_Path from dbo.Devices where DeviceName=" + selected_item;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Abhishek ModakStudentAuthor Commented:
After changing the line, string Sql only prints the DeviceName. I want to print the Folder path associated with that device name not the DeviceName itself.this information is stored in my database whose image I have attached.
0
Fernando SotoRetiredCommented:
Place the following statement after assigning the value to selected_item and post here.
Console.WriteLine( selected_item );

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Abhishek ModakStudentAuthor Commented:
It will print the value Moto e4 plus..have you seen the database image that have attached already?
I want to print the Folder path associated with the selected item device name..not the DeviceName.
0
Fernando SotoRetiredCommented:
Please test to verify.

Depending on how the database was set up column names may be case sensitive.
0
Abhishek ModakStudentAuthor Commented:
I have executed this sql query in the database.
select File_Path from dbo.Devices where DeviceName='Moto E (4) Plus'
This query executes properly and prints C:\Users\HP\Desktop\8cta\CF\chapter 1 as output.
I have made the following changes to the code:
string selected_item = this.cmb_UsersDisplay.GetItemText(this.cmb_UsersDisplay.SelectedItem);
            SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\HP\Documents\Visual Studio 2015\Projects\Folder_Security\Folder_Security\Database1.mdf; Integrated Security = True");
            con.Open();
            string Sql = "select File_Path from dbo.Devices where DeviceName='"+selected_item+"'";
            SqlCommand cmd = new SqlCommand(Sql, con);

            SqlDataReader Dr = cmd.ExecuteReader();
            Console.WriteLine(Dr.GetValue(0).ToString());
            con.Close();
But still the file path is not displayed on the console and at the Console.WriteLine code , it prints an error
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll
 {"Invalid attempt to read when no data is present."}
What might be the method to resolve this error and display only the folder path on the console.
0
Abhishek ModakStudentAuthor Commented:
I want to print only the File_Path column data for selected_item i.e. Moto E4 Plus that I have specified in the sql query.
0
Abhishek ModakStudentAuthor Commented:
string Sql = "select File_Path from dbo.Devices where DeviceName=" + selected_item;
I changed this line to
string Sql = "select File_Path from dbo.Devices where DeviceName='"+selected_item+"'";
Now the code is working. Thamks for your help.
0
Abhishek ModakStudentAuthor Commented:
Thanks for your help.
0
Fernando SotoRetiredCommented:
Not a problem Abhishek, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.