1

my database table looks like this

enter image description here

i trying to retrieve the FileURL in the CRUD class file like this :

  public IList<string> GetSorted(int listAct)
        {
          IList<Model.story> lstImages = context.stories.ToList();
          return lstImages.Where(c => c.ActivityID == listAct)
                          .Select( a => a.FileURL)
                          .ToList();
        }

Am i doing it right? ( new to this )

Then in my aspx , i try to display the value like this ;

 protected void btnSort_Click1(object sender, EventArgs e)
    {
        if (dropListActivity.SelectedIndex > 0)// drop down list
        {
                  string imgList = daoStory.GetSorted(Convert.ToInt32(dropListActivity.SelectedItem.Value)).ToString();

            Response.Write(imgList);

        } 
    }

Basically the drop down list shows activityID 1 or 2 only , and when i select 1 and click the sort button , it will display the FileURL of '1' and if i select 2 , it will display FileURL of '2' so on from the above database table . However , it now shows System.Collections.Generic.List`1[System.String] instead of the FileURL , i want to show the FileURL as stated in the database table , pls help thanks.

2 Answers 2

2

First off, calling ToList will cause all the records to be retrieved from the database and processed. That's a pretty big waste of resources when all you need is a single record. I'd Suggest writing your DAO method like this:

public string GetFileUrl(int actId)
{
    return context.stories.First(c => c.ActivityID == actId).FileURL;
}

And then I think you want to use it like this:

protected void btnSort_Click1(object sender, EventArgs e)
{
    if (dropListActivity.SelectedIndex > 0)// drop down list
    {
        var actId = Convert.ToInt32(dropListActivity.SelectedItem.Value);
        var imageUrl = daoStory.GetFileUrl(actId);

        Response.Write(imageUrl);
    } 
}

This method assumes that you'll only have one FileUrl for each ActivityId. If that's not the case, you can easily modify the DAO code to return a IEnumerable<string> and use Where instead of First, but there's not enough information in the question for me to figure out what the button click handler is supposed to do with the result.

2
  • yes i have one fileurl for each activityid . strangely it doesn't returns the fileurl string instead , its showing 'Model.Story' which is the namespace . actually i am going to display a list of images in grid view form , so the button is to sort the list out based on selection of the drop down list. Commented Jun 13, 2013 at 2:38
  • does daoStory.GetFileURL(actID) getting the fileurl column only? i think its retrieving the whole single record Commented Jun 13, 2013 at 2:43
0

You're passing a list back from the sort method but converting it to a string in the code behind. You have to deal with the list somehow and get the value of whatever element you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.