Can someone help me with this error: Object reference not set to an instance of an object
<p>I am building a simple CRUD (Create, Read, Update, Delete) system in ASP.NET and C#. I get the error <code>Object reference not set to an instance of an object</code> on line 79: </p>

<pre><code>Line 77:       SqlParameter forn = new SqlParameter("@fornavn", SqlDbType.VarChar, 100);
Line 78:       forn.Direction = ParameterDirection.Input;
Line 79:       forn.Value = Fornavn.Text;
Line 80:       insertParameters.Add(forn);
</code></pre>

<p>My code looks like this <strong><em>kunder.aspx:</em></strong></p>

<pre><code>&lt;%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="kunder.aspx.cs" Inherits="Secure_kunder" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form id="form1" runat="server"&gt;
    &lt;div&gt;
        &lt;asp:GridView ID="KundeGrid" runat="server" AutoGenerateColumns="False" DataSourceID="kundeDataSource"
            DataKeyNames="kundeid" ShowFooter="true" OnRowCommand="Customers_RowCommand"&gt;
            &lt;Columns&gt;
                &lt;asp:TemplateField HeaderText="Kundenr"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("kundeid") %&gt;
                    &lt;/ItemTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Fornavn"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("fornavn") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox ID="EditFornavn" runat="server" Text='&lt;%# Bind("fornavn") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertFirstName" Text='&lt;%# Bind("fornavn") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Efternavn"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("efternavn") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="EditLastName" Text='&lt;%# Bind("efternavn") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertLastName" Text='&lt;%# Bind("efternavn") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Firma"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("firma") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="EditFirma" Text='&lt;%# Bind("firma") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertFirma" Text='&lt;%# Bind("firma") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Website"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("website") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="EditWebsite" Text='&lt;%# Bind("website") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertWebsite" Text='&lt;%# Bind("website") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Pakke"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("pakke") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="EditPakke" Text='&lt;%# Bind("pakke") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertPakke" Text='&lt;%# Bind("pakke") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Oprettelsesdato"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;%# Eval("oprettelsesdato") %&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="EditOprettelsesdato" Text='&lt;%# Bind("oprettelsesdato") %&gt;' /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:TextBox runat="server" ID="InsertOprettelsesdato" Text='&lt;%# Bind("oprettelsesdato") %&gt;' /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
                &lt;asp:TemplateField HeaderText="Commands"&gt;
                    &lt;ItemTemplate&gt;
                        &lt;asp:Button runat="server" ID="Edit" Text="Edit" CommandName="Edit" /&gt;
                        &lt;asp:Button runat="server" ID="Delete" Text="Delete" CommandName="Delete" /&gt;
                    &lt;/ItemTemplate&gt;
                    &lt;EditItemTemplate&gt;
                        &lt;asp:Button runat="server" ID="Update" Text="Update" CommandName="Update" /&gt;
                        &lt;asp:Button runat="server" ID="Cancel" Text="Cancel" CommandName="Cancel" /&gt;
                    &lt;/EditItemTemplate&gt;
                    &lt;FooterTemplate&gt;
                        &lt;asp:Button runat="server" ID="Insert" Text="Insert" CommandName="InsertNew" /&gt;
                        &lt;asp:Button runat="server" ID="Cancel" Text="Cancel" CommandName="CancelNew" /&gt;
                    &lt;/FooterTemplate&gt;
                &lt;/asp:TemplateField&gt;
            &lt;/Columns&gt;
            &lt;EmptyDataTemplate&gt;
                Fornavne:&lt;asp:TextBox runat="server" ID="NoDataFornavn" /&gt;
                Efternavn:&lt;asp:TextBox runat="server" ID="NoDataEfternavn" /&gt;
                Firma:&lt;asp:TextBox runat="server" ID="NoDataFirma" /&gt;
                Website:&lt;asp:TextBox runat="server" ID="NoDataWebsite" /&gt;
                Pakke:&lt;asp:TextBox runat="server" ID="NoDataPakke" /&gt;
                Oprettelsesdato:&lt;asp:TextBox runat="server" ID="NoDataOprettelsesdato" /&gt;
                &lt;asp:Button runat="server" ID="NoDataInsert" CommandName="NoDataInsert" Text="Insert" /&gt;
            &lt;/EmptyDataTemplate&gt;
        &lt;/asp:GridView&gt;
        &lt;asp:SqlDataSource ID="kundeDataSource" runat="server" ConnectionString="&lt;%$ ConnectionStrings:minConnectionString %&gt;"
            SelectCommand="SELECT * FROM [kunder]" 
            InsertCommand="Insert into kunder (fornavn, efternavn, firma, website, pakke, oprettelsesdato) values (@fornavn, @efternavn, @firma, @website, @pakke, @oprettelsesdato)"
            UpdateCommand="Update kunder set fornavn=@fornavn, efternavn=@efternavn, firma=@firma, website=@website, pakke=@pakke, oprettelsesdato=@oprettelsesdato where kundeid=@kundeid"
            DeleteCommand="Delete from kunder where kundeid=@kundeid"
            oninserting="Sqldatasource1_Inserting" 
            OnInserted="Sqldatasource1_Inserted"&gt;
            &lt;/asp:SqlDataSource&gt;
    &lt;/div&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p><em><strong>kunder.aspx.cs:</em></strong></p>

<pre><code>using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Common;

public partial class Secure_kunder : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // here we keep the SqlParameters that we will use to insert new row
    private List&lt;SqlParameter&gt; insertParameters = new List&lt;SqlParameter&gt;();

    protected void Customers_RowCommand(object sender, GridViewCommandEventArgs e) 
    {
        if (e.CommandName == "NoDataInsert")
        {
            TextBox Fornavn = KundeGrid.Controls[0].Controls[0].FindControl("NoDataFornavn") as TextBox;
            TextBox Efternavn = KundeGrid.Controls[0].Controls[0].FindControl("NoDataEfternavn") as TextBox;
            TextBox Firma = KundeGrid.Controls[0].Controls[0].FindControl("NoDataFirma") as TextBox;
            TextBox Website = KundeGrid.Controls[0].Controls[0].FindControl("NoDataWebsite") as TextBox;
            TextBox Pakke = KundeGrid.Controls[0].Controls[0].FindControl("NoDataPakke") as TextBox;
            TextBox Oprettelsesdato = KundeGrid.Controls[0].Controls[0].FindControl("NoDataOprettelsesdato") as TextBox;

            SqlParameter forn = new SqlParameter("@fornavn", SqlDbType.VarChar, 100);
            forn.Direction = ParameterDirection.Input;
            forn.Value = Fornavn.Text.ToString();
            insertParameters.Add(forn);

            SqlParameter efte = new SqlParameter("@efternavn", SqlDbType.VarChar, 100);
            efte.Direction = ParameterDirection.Input;
            efte.Value = Efternavn.Text;
            insertParameters.Add(efte);

            SqlParameter firm = new SqlParameter("@firma", SqlDbType.VarChar, 100);
            firm.Direction = ParameterDirection.Input;
            firm.Value = Firma.Text;
            insertParameters.Add(firm);

            SqlParameter webs = new SqlParameter("@website", SqlDbType.VarChar, 100);
            webs.Direction = ParameterDirection.Input;
            webs.Value = Website.Text;
            insertParameters.Add(webs);

            SqlParameter pakk = new SqlParameter("@pakke", SqlDbType.VarChar, 100);
            pakk.Direction = ParameterDirection.Input;
            pakk.Value = Pakke.Text;
            insertParameters.Add(pakk);

            SqlParameter opre = new SqlParameter("@oprettelsesdato", SqlDbType.VarChar, 100);
            opre.Direction = ParameterDirection.Input;
            opre.Value = Oprettelsesdato.Text;
            insertParameters.Add(opre);

            kundeDataSource.Insert();
        }
        else if (e.CommandName == "InsertNew")
        {

            TextBox Fornavn = KundeGrid.FooterRow.FindControl("InsertFornavn") as TextBox;
            TextBox Efternavn = KundeGrid.FooterRow.FindControl("InsertEfternavn") as TextBox;
            TextBox Firma = KundeGrid.FooterRow.FindControl("InsertFirma") as TextBox;
            TextBox Website = KundeGrid.FooterRow.FindControl("InsertWebsite") as TextBox;
            TextBox Pakke = KundeGrid.FooterRow.FindControl("InsertPakke") as TextBox;
            TextBox Oprettelsesdato = KundeGrid.FooterRow.FindControl("InsertOprettelsesdato") as TextBox;

            SqlParameter forn = new SqlParameter("@fornavn", SqlDbType.VarChar, 100);
            forn.Direction = ParameterDirection.Input;
            forn.Value = Fornavn.Text;
            insertParameters.Add(forn);

            SqlParameter efte = new SqlParameter("@efternavn", SqlDbType.VarChar, 100);
            efte.Direction = ParameterDirection.Input;
            efte.Value = Efternavn.Text;
            insertParameters.Add(efte);

            SqlParameter firm = new SqlParameter("@firma", SqlDbType.VarChar, 100);
            firm.Direction = ParameterDirection.Input;
            firm.Value = Firma.Text;
            insertParameters.Add(firm);

            SqlParameter webs = new SqlParameter("@website", SqlDbType.VarChar, 100);
            webs.Direction = ParameterDirection.Input;
            webs.Value = Website.Text;
            insertParameters.Add(webs);

            SqlParameter pakk = new SqlParameter("@pakke", SqlDbType.VarChar, 100);
            pakk.Direction = ParameterDirection.Input;
            pakk.Value = Pakke.Text;
            insertParameters.Add(pakk);

            SqlParameter opre = new SqlParameter("@oprettelsesdato", SqlDbType.VarChar, 100);
            opre.Direction = ParameterDirection.Input;
            opre.Value = Oprettelsesdato.Text;
            insertParameters.Add(opre);

            kundeDataSource.Insert();
        }
    }

    protected void Sqldatasource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
    {
        e.Command.Parameters.Clear();
        foreach (SqlParameter p in insertParameters)
            e.Command.Parameters.Add(p);
    }

    protected void Sqldatasource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        insertParameters.Clear();
    }
}
</code></pre>
