-1

Hi i have a table in database named images. I want to fetch image_path from this table as shown below by matching the Category to it.

Ex: if i select Web Development category it should fetch image_path related with that category.

I am working on asp.net web pages. please guide me how can i achieve it?

id      Category                 image_path

4 New Websites          img\images\1982d16f-1a85-49c3-b7ac-94e05152273d_01.jpg
6 Mobiles                  img\images\87678076-3f19-43c0-909f-eec172d69919_02.jpg
7 Web Development          img\images\4fef0362-b5fd-43d1-b988-c5d7e674add0_03.jpg
8 Web Development          img\images\5752419a-013d-4c09-99af-f28132102189_04.jpg
9 Web Development          img\images\e4876bff-b647-48d6-a3a0-b1e7bfb6d60a_05.jpg
10 Web Designing          img\images\e86c3edc-0591-4157-8525-33e52ca21f64_06.jpg
11 Mobiles                  img\images\2b6ccd51-77f1-4da8-9ed6-98c2b069e92d_07.jpg
12 Mobile apps          img\images\44694e2e-24dc-4f06-bb8f-88b597322c0d_005.jpg
13 Mobile apps          img\images\9d5ca99a-f6fd-43d7-8a05-a6671f73bd54_006.jpg
Dennis Christian
  • 186
  • 1
  • 14
Radhe Sham
  • 141
  • 10

2 Answers2

0

As your're saying that "if i select", so I'm assuming that you're having Drop down in your scenario.

It's simple follow the query that will pull all the records related to the selected item from database.

Select image_path from images where Category = '"+DropDown1.SelectedItem+"'

The above query will do the trick.

If you're beginner, learn ADO.NET. It will help you to proceed further.

Note: Dropdown is assumption. You can keep as per your requirement.

gkrishy
  • 756
  • 1
  • 8
  • 33
0

Here's a very straightfoward method to retrieve your image paths:

    public List<string> GetImagePathsForCategory(string category)
    {
        const string DatabaseConnectionString = "MyConnectionString"; //probably use a setting from your web.config
        const string CategoryParameterName = "@version";
        const string ImagePathQuerySqlString = "Select image_path from images where Category = " + CategoryParameterName;
        var imagePaths = new List<string>();
        using (var dbConnection = new SqlConnection(DatabaseConnectionString))
        {
            dbConnection.Open();
            using (var query = new SqlCommand(ImagePathQuerySqlString, dbConnection))
            {
                query.Parameters.Add(new SqlParameter() { ParameterName = CategoryParameterName, Value = category });
                using (var reader = query.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        imagePaths.Add(reader.GetString(0));
                    }
                }
            }
        }
        return imagePaths;
    }

While this is okay for a one off usage, if you're querying the database often then I'd look into either an ORM (like NHibernate or Entity Framework for instance) which will do the heavy lifting for you or - if you don't want to do that - then use ADO.Net to implement your own Data Access Layer or Repository Pattern.

KevD
  • 697
  • 9
  • 17
  • Please read my post carefully i am not using web forms. I am working on asp.net(Web Pages Razor). if you can guide me in that way it will be helpful for me. Thanks – Radhe Sham Jul 15 '15 at 13:50
  • Hi, this method above is independant of WebForms of MVC and probably works best in a separate class that is called by your controller to provide a list of strings that represent the paths. If you want to elloborate on how your View / Controller setup is I could probably help more. – KevD Jul 15 '15 at 14:25