Solved

Sorting linked tables in gridview

Posted on 2014-01-27
2
300 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 40

Accepted Solution

by:
Kyle Abrahams earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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