?
Solved

Sorting linked tables in gridview

Posted on 2014-01-27
2
Medium Priority
?
304 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 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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