StudentRik
StudentRik

Reputation: 1049

GridView Query not displaying

The page opens to a grid that is loaded for the user to edit/Delete the data.The LoadGrid() is in a !IsPostBack.

private void LoadGrid()
{
try
{
    using (var db = new dbDataContext())
    {
        var loadGrid = from t in db.tbl_Providers
                        where t.provider_Deleted == false
                        orderby t.provider_Name.ToLower()
                        select new
                            {
                                t.ProviderId,
                                t.provider_Name,
                            };

        grd_Provider.DataSource = loadGrid;

        grd_Provider.DataBind();
    }
    // Set value of Edit/Delete columns on grid
    // ========================================
    grd_Provider.HeaderRow.Cells[0].Text = "Edit/Update";
    grd_Provider.HeaderRow.Cells[1].Text = "Delete";

}
catch (SystemException ex)

All this code works fine. What I am trying to do is display the alphabet and the user clicks on the link and it reloads a new query and loads the grid.

The alphabet is working ok as far as I can tell. I have the code load on Page_Load and the Void method is further down the page.

for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++)
{
LinkButton lnkCharacter = new LinkButton();
lnkCharacter.ID = "lnkCharacter" + asciiValue;
lbl_Alphabet.Controls.Add(lnkCharacter);
lnkCharacter.CommandArgument = Convert.ToString(asciiValue);
lnkCharacter.Command += lnkCharacter_Command;

lnkCharacter.Text = Convert.ToString(asciiValue) + " ";
}

void lnkCharacter_Command(object sender, CommandEventArgs e)
{
    try
    {
        var lbtn = (LinkButton) lbl_Alphabet.FindControl("lnkCharacter" + e.CommandArgument);
        var id = lbtn.Text;

        using (var db = new dbDataContext())
        {
            var query = from n in db.tbl_Providers
                        where n.provider_Name.StartsWith(id) && n.provider_Deleted == false
                        select new
                            {
                               id = n.ProviderId,
                               name = n.provider_Name
                            };

            grd_Provider.DataSource = query;
            grd_Provider.DataBind();
        }
    }
    catch (SystemException ex)
    {
        var exceptionUtility = new genericExceptions();
        exceptionUtility.genericSystemException(
            ex,
            Server.MachineName,
            Page.TemplateSourceDirectory.Remove(0, 1) +   Page.AppRelativeVirtualPath.Remove(0, 1),
            ConfigurationManager.AppSettings["emailSupport"],
            ConfigurationManager.AppSettings["emailFrom"],
            string.Empty);
    }
}

I have tested the query in LINQPad and it works and returns the results. When I run it on the page and step through the Datasource = query says expanding the results will enumerate the query. I do this .. Enumeration yielded no results The page reloads and the grid is never show? Is it something to do with the original grid being loaded on page load and some how conflicting?

I have changed my Alphabet to go in the IsPostBack and the page reloads and the alphabet dissapears and the grid is still there loaded as normal. Do I need a different gridview for the Alphabet Query?

Upvotes: 2

Views: 300

Answers (1)

Ann L.
Ann L.

Reputation: 13965

This is what it looks like to me:

You have this code:

lnkCharacter.Text = Convert.ToString(asciiValue) + " ";

Then you have this code:

var lbtn = (LinkButton) lbl_Alphabet.FindControl("lnkCharacter" + e.CommandArgument);
var id = lbtn.Text;

Finally, you have this code:

      var query = from n in db.tbl_Providers
                    where n.provider_Name.StartsWith(id) && n.provider_Deleted == false
                    select new
                        {
                           id = n.ProviderId,
                           name = n.provider_Name
                        };

By the time you get to the LINQ query, the value of id will have a space at the end. So if you hit the "A" link button, you won't be searching for everything that starts with "A", but everything that starts with "A ".

You need to either trim lbtn.Text when you retrieve it, or use some other method to ensure spaces between your alphabet hyperlinks, like adding Literal instances between adding your link buttons.

Upvotes: 1

Related Questions