2

I'm confused as to why setting the datasource of a datagridview control to null, would cause an "object reference not set to an instance of an object" error. Thanks in advance

while (xmlReader.Read())
{
    if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "deposits"))
    {
        oDeposit.DepAmt = Convert.ToDouble(xmlReader.GetAttribute("depamount"));
        oDeposit.DepDate = Convert.ToDateTime(xmlReader.GetAttribute("depdate"));
        oDeposit.DepositId = Convert.ToInt32(xmlReader.GetAttribute("depid"));

        oCustomer.addDeposits(oDeposit);
        **dgvDeposits.DataSource = null;**
        dgvDeposits.DataSource = oCustomer.Deposits;            
    }
}
5
  • are you sure that dgvDeposits is not null? It might be that you are just barking up the wrong tree. I've never worked with DataGridView though and it might really be the implemented behaviour to throw an exception if you pass null to the setter. Commented Oct 23, 2011 at 15:46
  • I agree with yas4891 suggestion - check with debugger if dgvDeposits is not null in line you've marked. There is nothing wrong with setting DataGridView.Datasource = null - I've decompiled it's setter and it doesn't throw any exception if Datasource is null. Maybe you're wired with some additional event, like DataSourceChanged, where the exception is thrown? Commented Oct 23, 2011 at 16:02
  • there is no add'l event for the DataGridView. The exception is thrown on when I set the value to null. I beleive the problem lies somewhere with the DataGridView not be initialized as an object. But Initializecomponent() is called when the form loads and before the exception. There are references to the datagrid view in the Initialize components. Commented Oct 23, 2011 at 17:16
  • As the others have said - this should not be throwing an error. You will need to provide your whole class to get an answer - I don't believe it can be answered with the current information. Commented Oct 23, 2011 at 19:21
  • @Susan: Try calling another method on the DGV to check if it's that one that's not initialized properly or if it's just the DataSource property that fails. So for example, try adding the row dgvDeposits.MultiSelect = false; before the line setting the DataSource. If that fails, try looking here and comparing with your own code to make sure it's initialized properly. Commented Oct 24, 2011 at 8:48

2 Answers 2

3

You should use this instead of setting DataSource to null:

dgvDeposits.DataSource = typeof(Deposit);

Please check following question it might have an explanation for your exception.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't have a 'typeof' Deposit. I looked over your reference but it doesn't seem to help.
@Susan, of course You don't have type Deposit. It is example, I am sorry I didn't clarify that. Guess it should be typeof(whatever_type_is_oCustomer.Deposits) - if it is a List<T> then use T. When You set typeof(some_type) to set DataGridView DataSource, basically you say: "i will store instance(s) of something but there is no data at the moment". However in most cases You will not get an exception when setting DataSource to null. I did and I had to use typeof. I am sorry i didn't help You, guess You will have to provide us with more code.
I have no idea what this is doing.. why does setting the type of the object to the datasource her problem?
@sksallaj, what exactly you don't understand? Her problem or why proposed solution helps solve her problem? Why it solves her problem is explained in my comment.
Ohhh I didn't read your comment section. Sorry about that. Typically solutions on stackoverflow are edited multiple times based on clearing up any confusions in the comment section. Not saying you have to do it, it's my fault for not reading it. I'm upvoting this because this helped me out.
1

Ok, So I know I'm new to this, but I had the same type of problem. I found that creating a DataTable using the Columns in the DataGridView then setting the table as the DataSource fixes the problem.

DataTable dt = new DataTable();
dt.Columns.Add("DepAmt", typeof(double));
dt.Columns.Add("DepDate", typeof(DateTime));
dt.Columns.Add("DepositId", typeof(int));
dgvDeposits.DataSource = dt;       

This site is what I referenced.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.