The infomous Object reference not set to an instance of an object error occurs when you try to reference or use an object that has not been set to a valid "value" or instance. For example, let's assume you have a Class "Person" that has a property Name. If you try to access the Name property and your Person object is not set to an instance, you will receive the Object reference not set to an instance of an object error.
In code:
Person p = null;
p.Name = "John Doe";
This is an obvious case, but what makes this error difficult to track down at times is when you do not know why you do not have an instance of the object. This can occur for a variety of reasons. If you are able to create a unit test (or already have one) this can help you to track down the root cause. You can also rely on the stack trace and/or debug the offending code.
If after following the manual upgrade instructions you receive an error such as "The type 'System.Web.Mvc.AcceptVerbsAttribute' exists in both " you should try the following:
- Right click on the web site and view the properties -> and in the references section make sure you do not have duplicate references...if you do, delete one of them
- Check you bin directory, if you have System.Web.Mvc.dll in the bin, remove it (you may also need to remove System.Web.Abstractions.dll, etc.
You should then be able to build
If you do development on your localhost and you want to test email, you can update your web.config as per below. Note that you will need a directory of D:\Maildrop on your machine. Doing this will result in emaills being delivered to the directory you specify.
You may receive Unable to relay for... errors and if you do, you can specify a from in the smtp element.
<mailSettings> <smtp deliveryMethod="SpecifiedPickupDirectory" from="local@localhost">
<specifiedPickupDirectory pickupDirectoryLocation="D:\Maildrop"/> </smtp></mailSettings>
Often I find the need to dynamically specify the order by for a LINQ to SQL query. I came across a very nice example of this here.
I needed to use this technique with a CSLA class, so I needed to call it in a sligthly different manner than the way explained in the article. I was grouping information and needed to then dynamically set the order by and finally place the results into a CSLA read only list.
I accomplished this as follows:
- First I specified my LINQ query and selected the results into an annonymous type and placed into a variable (var data = ...)
- Next, called my OrderBy extension method on the LINQ query created in #1
var ordered = data.OrderBy(criteria.OrderByProperty, criteria.OrderByDesc);
The criteria object is the typical pattern used in CSLA to pass data to your data access methods such as fetch.
- Finally, I selected the data elements into my CSLA list item, and add to the parent list object
var selected = ordered.Select(a => MyCslaListItem.GetMyCslaListItem(a.Id, a.Name, a.Email, ...));
this.AddRange(selected);
I understand I could do this in fewer steps, but am leaving as it to explicitly outline the approach taken.
I have been doing some work on a asp.net webforms website that has been "upgraded" to also include the mvc bits. One issue I ran into was an error when trying to create a strongly typed view. I was receiving the below exception:
Parser Error Message: Could not load type 'System.Web.Mvc.ViewPage<MyViewModel>'.
I also was not getting intellisense in my View page when trying to access the Model property.
It sounds like this relates to dll references, and I found the solution at the below link.
What I did to resolve was to create a new MVC project and then copy the web.config from the Views directory to my older webforms website. That did the trick...
http://forums.asp.net/p/1378448/2908692.aspx
Recently I had the need to handle the selected index changed event in an ASP.NET page. I did not want to post back to the server as I just wanted to hide/show an HTML element, so jQuery was the perfect fit.
Below is the code. Basically, within the jQuery document ready funciton, I wire a function to each dropdown list ('select') change event. You will probably want to refine this to a single id or class. Each time the dropdown selected index changes, I want to check the text value, and if it contains some text, either show or hide an HTML element. I also set a checkbox control to checked using the client id within a script tag.
<script type="text/javascript">
$(function() {
$('select').change(function(e) {
if (this[this.selectedIndex].text.indexOf("SomeText") > 0) { $('#emailActivity').fadeIn("slow");
// Ensure checkbox initially checked
$('#<%=cbSendEmail.ClientID %>').attr('checked', true);
}
else {$('#emailActivity').fadeOut();
}
});
});
</script>
Recently I was working on a custom page in a Dynamic Data project and the "Delete" link was not working on a GridView that had a LinqDataSource as its data source. Below are the steps I followed to resolve:
First, I was getting an error when clicking delete for a record in the GridView
LinqDataSource
'GridDataSource' does not support the Delete operation unless
EnableDelete is true.' when calling method
This was fixed easily by setting EnableDelete="true" on the LinqDataSource.
Next up, I needed to set the DataKeyNames on the GridView. This basically indicates what the where condition will be for the delete operation. I set the GridView DataKeyNames equal to the name of the primary key column in my table.
That will probabaly resolve for most people, but for me, it did not. I had also set the Select property on the LinqDataSource and this was preventing the delete from occuring. Once I REMOVED the value for this propery, all worked.
The below article was helpful in diagnosing:
http://msdn.microsoft.com/en-us/library/bb514963.aspx
There are times when developing that I want to be able to see what the contents will be for emails generated from my application, but I do not want the emails to actually go to the reciepients. In addition I may not have my local development machine set up to relay mail.
Sometime, updating the development system email addresses to a generic address makes sense. Other times, I do not or cannot do this, so instead, I opt for the following technique.
With this approach, you add a configuration setting to your config file, and the result is that the email will be placed in a directory of your choosing, but will not actually be sent.
To accomplish this, add the below setting to your config file:
<system.net>
<mailSettings>
<smtp deliveryMethod='SpecifiedPickupDirectory'>
<specifiedPickupDirectory pickupDirectoryLocation="C:\All\Maildrop" />
</smtp>
</mailSettings>
</system.net>
Now, when you send your email using code similar to below, the email will appear in the directory you specified.
MailMessage message = new MailMessage();
message.From = new MailAddress(...
... other code to set to, body goes here
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(message);
When using the LINQ to SQL designer surface in Visual Studio, each time
you add an object (table, procedure, etc), you will end up adding
connection string information to your config file. This can be
confusing as the names used for the connection string may be different
then what you want to use and you may end up with multiple connection
strings, especially if you have multiple developers working on the same
project.
To address this issue, you can:
- Create a partial class for your data context and create a parameter-less constructor as follows
public partial class LinqAppDataContext
{
public LinqAppDataContext()
: base(ConfigurationManager.ConnectionStrings["AppConnectionString"].ToString())
{
}
}
In this example, I already have a LinqAppDataContext class, and I am
creating a partial class that calls the constructor on the base class
that takes a connection string. I then pull this connection string
from the config file.
- Step 1 is a one time setup. The remaining steps need to be followed each time you add a new object to the designer surface
- Add a new object to the designer surface (for example, drag a table from the Server Explorer)
- Click on an empty section of the designer surface (dbml file)
- In
the properties window, go to Connection, then clear the information
from the Connection String property and then set the Application
Settings property to false. By doing this, the parameter-less
constructor from step 1 above will kick in and pull the connection
string from your config file.
- Verify that no extra connection strings have been added to your projects settings or config files
Hopefully this will help you in clearly knowing which connection string will be used with your LINQ to SQL context classes.