Solved

Sorting linked tables in gridview

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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 …
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
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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