-1

During a gridview => database update function, I use a column-by-column conversion to string in order to pass data back to my database as a whole command string. I'm aware that this sounds convoluted, so here is an example:

Classes in use:
InvoiceHandler.cs
Default.aspx.cs

Since this is a rather large snippet, here is a pastebin:

Default.aspx.cs: http://pastebin.com/Y3fJZ36Z

InvoiceHandler.cs: http://pastebin.com/ZsdAnDxr

At the first point of conversion (invoiceTableEdited.Columns["Column1"].ColumnName = "@K_INV";) I get a NullReferenceException error, assumedly because the method call in Default.aspx.cs

handler.invoiceTableEdited = ViewState["invoiceTable"] as DataTable; handler.invoiceTableEdited.Rows[row.RowIndex]["K_INVOICE"] = sK_INVOICE;

appears to be having trouble.

What must I do to resolve this?

5
  • "Appears to be having trouble" is not very helpful. Try telling us the error or the current results vs the expected results.
    – Steve
    Aug 8, 2014 at 14:24
  • 2
    Looks like you get the exception because you're trying to assign a name to a column in the columns collection of invoiceTableEdited when no columns have been created.
    – sr28
    Aug 8, 2014 at 14:33
  • 1
    @sr28 Are you sure? In Default.aspx.cs, in the method OnUpdate I create those columns out of the row in the GridView?
    – Wolfish
    Aug 8, 2014 at 14:35
  • 1
    My mistake. However, in Default.aspx.cs you are creating ["column1"] etc and in InvoiceHandler.cs you try and assign the name to ["Column1"]. Try altering that to be ["column1"].
    – sr28
    Aug 8, 2014 at 14:40
  • 1
    @sr28 I'd ignore the column names. I had to change them all in a rush to anonymise them.
    – Wolfish
    Aug 8, 2014 at 14:48

1 Answer 1

1

I'll be honest, it's not very clear how you're trying to create the column collection for the table 'InvoiceTableEdited'. What it looks like you're doing in OnUpdate is you are assigning a string variable to a row in the table with a given index and a column that doesn't currently exist. Your basically saying put this string into a cell with a row number of 'x' and a column name of "column1". At this point "column1" doesn't exist.

I'd create the columns you need first in InvoiceHandler.cs like this (assumes they're string):

invoiceTableEdited.Columns.Add("MyColumn1", typeof(string));
invoiceTableEdited.Columns.Add("MyColumn2", typeof(string));

Obviously if you have loads of columns and you don't care about their names then just create a loop and add them that way. This will give them the naming convention "ColumnN", where 'N' is the number. You can then assign a name to them by referencing there name ("Column1" for example) or using their index.

1
  • 1
    I looked at what this was actually doing, and realised I don't need the extra table. In fact, I'm already extracting the data from the gridview as strings at the beginning of onupdate(), so I'm using those. I do appreciate your help, you were correct that the requisite column did not exist in memory.
    – Wolfish
    Aug 8, 2014 at 15:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.