Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sorting linked tables in gridview

Posted on 2014-01-27
2
Medium Priority
?
305 Views
Last Modified: 2014-01-27
Hello:

I am getting this error message
Object reference not set to an instance of an object.

on this line:
dataset.Tables["Test"].DefaultView.Sort = sortExpression;

    private void BindGrid()
    {
        SqlConnection conn;
        DataSet dataset = new DataSet();
        SqlDataAdapter adapter;
        if (ViewState["dataset"] == null)
        {
            string connectionString = "Data Source=BirdCage;Initial Catalog=BirdCageDB;User Id=User;Password=BCUser;";
            conn = new SqlConnection(connectionString);
            adapter = new SqlDataAdapter("SELECT Test.ID, Test.PersonName, Data.DeptID FROM Test INNER JOIN Data ON Test.ID = Data.AutoID", conn);
            adapter.Fill(dataset);
            ViewState["datadset"] = dataset;
        }
        else
        {
            dataset = (DataSet)ViewState["dataset"];
        }
        string sortExpression;
        if (gridSortDirection == SortDirection.Ascending)
        {
            sortExpression = gridSortExpression + " ASC";
        }
        else
        {
            sortExpression = gridSortExpression + " DESC";
        }
        dataset.Tables["Test"].DefaultView.Sort = sortExpression;
        ViewData.DataSource = dataset.Tables["Test"].DefaultView;
        ViewData.DataBind();
    }
    protected void ViewData_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortExpression = e.SortExpression;
        if (sortExpression == gridSortExpression)
        {
            if (gridSortDirection == SortDirection.Ascending)
            {
                gridSortDirection = SortDirection.Descending;
            }
            else
            {
                gridSortDirection = SortDirection.Ascending;
            }
        }
        else
        {
           gridSortDirection = SortDirection.Ascending;
        }
        gridSortExpression = sortExpression;
        BindGrid();
    }
    private SortDirection gridSortDirection
    {
        get
        {
            if (ViewState["GridSortDirection"] == null)
            {
                ViewState["GridSortDirection"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["GridSortDirection"];
        }
        set
        {
            ViewState["GridSortDirection"] = value;
        }
    }
    private string gridSortExpression
    {
        get
        {
            if (ViewState["GridSortExpression"] == null)
            {
                ViewState["GridSortExpression"] = "ID";
            }
            return (string)ViewState["GridSortExpression"];
        }
        set
        {
            ViewState["GridSortExpression"] = value;
        }
    }

Open in new window

0
Comment
Question by:RecipeDan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 39813671
Try
  dataset.Tables[0].DefaultView.Sort = sortExpression

to find out what the name is set a breakpoint after the fill . . . in the immediate window type
"?dataset.Tables[0].TableName" without the quotes.

You could also call:
adapter.Fill(dataset, "Test");
0
 
LVL 1

Author Comment

by:RecipeDan
ID: 39813787
This worked:

adapter.Fill(dataset, "Test");

Thanks for your help
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question