Products

Solutions

Resources

Partners

Community

Blog

About

QA

Ideas Test

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

Welcome to the DNN Community Forums, your preferred source of online community support for all things related to DNN.
In order to participate you must be a registered DNNizen

HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...e.NewObject is null in LinqDataSource_Inserting evente.NewObject is null in LinqDataSource_Inserting event
Previous
 
Next
New Post
5/18/2009 4:00 PM
 

Hi, all:

I got the "A critical error has occurred. Object reference not set to an instance of an object." error message, and this is b/c the e.NewObject within the LinqDataSource_Inserting event is null when I am inserting data.

The background of the problem:

I have two grids (both are Telerik RadGrids, which are similar to Gridview, but more powerful). The top one contains contacts, and the bottom one contains one or more notes concerning a selected contact from the top grid. The user can create his own list of note subjects, from which he can choose only one when creating a new note. This list of subjects is bound to a dropdownlist with the markup below:

<asp:DropDownList ID="ddl_subjects" runat="server"

DataSourceID="LinqDS_Subjects" DataTextField="Subject"

DataValueField="SubjectId"

SelectedValue='<%# Bind("SubjectId") %>'

AppendDataBoundItems="True" >

<asp:ListItem Selected="True" Text="Select" Value=""></asp:ListItem>

</asp:DropDownList>

Every time when I create a new note, if I pick one of the bound values, the note inserts fine. But whenever I pick "Select", which has a value of null, to intentionally not assign a subject to the note, I get that error message. I spent hours debugging, but to no avail. So your help is greatly appreciated.

 Han

 
 

 
New Post
5/18/2009 6:55 PM
 

There are a number of ways to handle this. The most direct way would be to wire up a method to the LinqDataSource_Inserting event and if "e" is null create a empty instance of that class and pass it along.

The fields in the database will have to allow null values. If not then you're gonna get "bla blah blah cannot be null" error :)Lately I have not used the LinqDataSource control for inserts and updates just "reads" because the paging is fast and easy.

When someone hits the button to do an insert or and update I just write code to handle it. It's not much code and problems like this are not really problems at all because you just wrap the logic in if then statements.



Michael Washington
http://ADefWebserver.com
www.ADefHelpDesk.com
A Free Open Source DotNetNuke Help Desk Module
 
New Post
5/19/2009 9:17 AM
 

Hi, Michael:

Thanks for your reply and suggestions. They are helpful.

I have been using Linq for inserts and updates and has worked great, except for this instance.

I still would like to know why the insert and the update don't work when I choose "Select" from the dropdownlist, but it works when I choose a databound item from the dropdown. Any idea?

By the way, your tutorials on DNN module development have been priceless. Keep them up, man.

Han

 
New Post
5/19/2009 12:30 PM
 

My guess would be, because it's value is "" your Linq configuration might be set to treat empty strings as NULL (or nullable column), therefore nothing needs setting.

Look at the column configuration in the model, as well as any parameters set on the datasource for the ConvertEmptyStringToNull attribute.

If this is the _only_ field in the insert/update list then there will be nothing to pass to the linqdatasource, and you can either handle it at the linq datasource events (NULL objects) instancing them by hand, or before that in something like an ItemInserting event on the control where you can add values to a dictionary:

(for ListView control,  but should be similar)

    protected void SomeListView_ItemInserting(object sender, ListViewInsertEventArgs e)
    {
      var somevalue = somecontrol.someproperty;
      e.Values.Add("somefield", somevalue);
    }
 

This will then feed through the linq datasource pipeline, instancing the object and necessary field. Or you can instance the object yourself:

    protected void SomeLinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {
      var newinstance = e.NewObject as Yourtypehere;
      if (newinstance == null)
        newinstance = new Yourtypehere();
        newinstance.someproperty = somecontrol.someproperty;
    }

Hope this gives a few move avenues for people to inivestigate...
Shea

 

 

 
New Post
5/19/2009 12:50 PM
 

zhehanforchina wrote
 

I have been using Linq for inserts and updates and has worked great, except for this instance.

I am still only using Linq but I am not using the LinqDataSource control. I am just doing the inserts and updates manually using LinqtoSQL.



Michael Washington
http://ADefWebserver.com
www.ADefHelpDesk.com
A Free Open Source DotNetNuke Help Desk Module
 
Previous
 
Next
HomeHomeDevelopment and...Development and...DNN Platform (o...DNN Platform (o...e.NewObject is null in LinqDataSource_Inserting evente.NewObject is null in LinqDataSource_Inserting event


These Forums are dedicated to discussion of DNN Platform and Evoq Solutions.

For the benefit of the community and to protect the integrity of the ecosystem, please observe the following posting guidelines:

  1. No Advertising. This includes promotion of commercial and non-commercial products or services which are not directly related to DNN.
  2. No vendor trolling / poaching. If someone posts about a vendor issue, allow the vendor or other customers to respond. Any post that looks like trolling / poaching will be removed.
  3. Discussion or promotion of DNN Platform product releases under a different brand name are strictly prohibited.
  4. No Flaming or Trolling.
  5. No Profanity, Racism, or Prejudice.
  6. Site Moderators have the final word on approving / removing a thread or post or comment.
  7. English language posting only, please.
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out