Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Object Reference not found after sorting a column

Posted on 2007-09-30
10
Medium Priority
?
370 Views
Last Modified: 2013-11-07
I get "Object Reference not found" error after sorting a column in datagridview control
if I initial the DataTable this way:
                vDGV.DTable.Clear();
                vDGV.DTable.Columns.Clear();
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                SqlDataAdapter mDA = new SqlDataAdapter(mCmd);
                mDA.Fill(vDGV.DTable);
                dataGridView1.DataSource = vDGV.DTable;
but if I changed the first two lines to
                vDGV.DTable = new DataTable();  // changed
                dataGridView1.Columns.Clear();
                dataGridView1.DataSource = null;
                SqlDataAdapter mDA = new SqlDataAdapter(mCmd);
                mDA.Fill(vDGV.DTable);
                dataGridView1.DataSource = vDGV.DTable;
I am OK.

Question is, how should I initialise the DataTable to prevent the object not found reference?


0
Comment
Question by:sepknow
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 14

Expert Comment

by:Jai S
ID: 19988655
is this coding in your SORTING part ?
if yes, you dont need to set the DATASOURCE each and every time it is sort !!

vDGV.DTable = new DataTable() - this statement itself will create a new object of your datatable...you dont need to .clear the rows and columns...
0
 
LVL 13

Expert Comment

by:dungla
ID: 19988662
vDGV.DTable.Clear(); will clear the data and keep structure of DataTable. When you try to sorting gridview maybe the column does not exist?
0
 

Author Comment

by:sepknow
ID: 19988665
I just allowed sorting in some of the columns by setting the column SortMode to DataGridViewColumnSortMode.Automatic.

The error occurred when executing reach the line:
mDA.Fill(vDGV.DTable);

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 22

Expert Comment

by:JimBrandley
ID: 19988666
Try:
vDGV.DTable.Constraints.Clear();
vDGV.DTable.Clear();
vDGV.DTable.Columns.Clear();

You may have constraints complainig when the columns collection is cleared.

Jim
0
 

Author Comment

by:sepknow
ID: 19988698
I still get the same error.
0
 
LVL 14

Expert Comment

by:Jai S
ID: 19988702
can you tell in which line you are getting the error ?
0
 
LVL 14

Expert Comment

by:Jai S
ID: 19988706
you need to add
vDGV.DTable = new DataTable();  // changed
before you start filling the data table...
0
 
LVL 22

Accepted Solution

by:
JimBrandley earned 2000 total points
ID: 19988707
If you are loading from the same table each time, try DataTable.Clear(), but leave the columns and constraints collections alone.

There is a lot of stuff in a DataTable in addition to columns and rows. I suspect the clears we have tried so far is leaving an invalid reference.

Jim
0
 

Author Comment

by:sepknow
ID: 19988743
Hi Jim,
This helps.
Thanks.
0
 
LVL 22

Expert Comment

by:JimBrandley
ID: 19988758
My pleasure. Good luck.

Jim
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month21 days, 3 hours left to enroll

810 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