Solved

Sorting linked tables in gridview

Posted on 2014-01-27
2
292 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
2 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
This worked:

adapter.Fill(dataset, "Test");

Thanks for your help
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now