0

I have a formview in default insert mode that when the insert button is clicked it inserts information into a sql database then returns the newly inserted record and redirects it to a new page inside another form view via stored procedure. Here are the codes from page 1 of the SqlDatasource:

<asp:SqlDataSource ID="BiddersDB" runat="server" ConnectionString="<%$ ConnectionStrings:BiddersDatabaseConnection %>" InsertCommand="INSERT INTO [Bidders] ([Name], [Address], [City], [State], [Zip], [Phone], [Fax], [Email], [Status], [Notes], [MWBE]) VALUES (@Name, @Address, @City, @State, @Zip, @Phone, @Fax, @Email, @Status, @Notes, @MWBE); SET @Id = SCOPE_IDENTITY()" InsertCommandType="StoredProcedure" SelectCommand="SELECT * FROM [Bidders]" UpdateCommand="UPDATE [Bidders] SET [Name] = @Name, [Address] = @Address, [City] = @City, [State] = @State, [Zip] = @Zip, [Phone] = @Phone, [Fax] = @Fax, [Status] = @Status, [MWBE] = @MWBE, [Notes] = @Notes WHERE [Id] = @Id" OnInserted="BiddersDB_Inserted">
            <InsertParameters>
                <asp:Parameter Name="Name" />
                <asp:Parameter Name="Address" />
                <asp:Parameter Name="City" />
                <asp:Parameter Name="State" />
                <asp:Parameter Name="Zip" />
                <asp:Parameter Name="Phone" />
                <asp:Parameter Name="Fax" />
                <asp:Parameter Name="Email" />
                <asp:Parameter Name="Status" />
                <asp:Parameter Name="Notes" />
                <asp:Parameter Name="MWBE" />
                <asp:Parameter Name="Id" Direction="Output" Type="Int32"/>
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" />
                <asp:Parameter Name="Address" />
                <asp:Parameter Name="City" />
                <asp:Parameter Name="State" />
                <asp:Parameter Name="Zip" />
                <asp:Parameter Name="Phone" />
                <asp:Parameter Name="Fax" />
                <asp:Parameter Name="Status" />
                <asp:Parameter Name="MWBE" />
                <asp:Parameter Name="Notes" />
                <asp:Parameter Name="Id" />
            </UpdateParameters>
        </asp:SqlDataSource>

Here is the code behind from the page 1:

protected void BiddersDB_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        int Id;
        Id = (int)e.Command.Parameters["@Id"].Value;

        Response.Redirect("Details.aspx?Id=" + Id);
    }

Here are the SqlDataSource codes from page 2:

<asp:SqlDataSource ID="FormViewDB" runat="server" ConnectionString="<%$ ConnectionStrings:BiddersDatabaseConnection %>" 
        SelectCommand="SELECT Bidders.Id, Bidders.Name, Bidders.Address, Bidders.City, Bidders.State, Bidders.Zip, Bidders.Phone, Bidders.Fax, Bidders.Status, Bidders.Notes, Bidders.Email, Bidders.MWBE AS MWBEID, MWBE_Types.Code AS MWBE FROM Bidders INNER JOIN MWBE_Types ON Bidders.MWBE = MWBE_Types.MWBEID WHERE ([Id] like @ID) AND (Bidders.Name like @Name)"
        UpdateCommand="UPDATE [Bidders] SET [Name] = @Name, [Address] = @Address, [City] = @City, [State] = @State, [Zip] = @Zip, [Phone] = @Phone, [Fax] = @Fax, [Email]=@Email, [Status] = @Status, [MWBE]=@MWBEID, [Notes] = @Notes, [Denied] = @Denied WHERE [Id] = @Id"
        DeleteCommand="DELETE FROM [Bidders] WHERE [Id] = @Id"
        InsertCommand="INSERT INTO [Bidders] ([Name], [Address], [City], [State], [Zip], [Phone], [Fax], [Email], [Status], [MWBE], [Notes], [Denied]) VALUES (@Name, @Address, @City, @State, @Zip, @Phone, @Fax, @Email, @Status, @MWBE, @Notes, @Denied)">

            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="Zip" Type="String" />
                <asp:Parameter Name="Phone" Type="String" />
                <asp:Parameter Name="Fax" Type="String" />
                <asp:Parameter Name="Email" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="MWBEID" />
                <asp:Parameter Name="Notes" Type="String" />
                <asp:Parameter Name="Denied" Type="String" />
                <asp:Parameter Name="Id" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="Address" Type="String" />
                <asp:Parameter Name="City" Type="String" />
                <asp:Parameter Name="State" Type="String" />
                <asp:Parameter Name="Zip" Type="String" />
                <asp:Parameter Name="Phone" Type="String" />
                <asp:Parameter Name="Fax" Type="String" />
                <asp:Parameter Name="Email" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="MWBE"/>
                <asp:Parameter Name="Notes" Type="String" />
                <asp:Parameter Name="Denied" Type="String" />
            </InsertParameters>
            <DeleteParameters>
                <asp:Parameter Name="Id" Type="Int32" />
            </DeleteParameters>
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="%" Name="Id" QueryStringField="Id" Type="Int32" />
            <asp:QueryStringParameter DefaultValue="%" Name="Name" QueryStringField="Name" Type="String" />
        </SelectParameters>
        </asp:SqlDataSource>

I keep getting this error message "An exception of type 'System.NullReferenceException' occurred in BiddersWebVersion.dll but was not handled in user code Additional information: Object reference not set to an instance of an object."

Please help. I barely started learning this. Thanks.

user3736492
  • 3
  • 1
  • 6
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason Nov 12 '14 at 21:24
  • So how would I got about pulling the SCOPE_IDENTITY and setting it as a variable in order to use the Response.Redirect? I actually got this off of a tutorial. – user3736492 Nov 12 '14 at 21:32
  • I have no idea what "SCOPE_IDENTITY" you're referring to. But the link I provided you will tell you everything you need to know about what a NullReferenceException is and how to handle it. – mason Nov 12 '14 at 21:33
  • Thanks. It helped. I have a better understanding because of that link you provided. Supposedly the SCOPE_IDENTITY returns the most recently added record to the database. I'm trying to figure out how to format it and use it to redirect and display the record in another page. – user3736492 Nov 12 '14 at 21:40
  • Is it inserting any record in db? Also why on page1 InsertCommandType="StoredProcedure"? – Aman B Nov 12 '14 at 22:03
  • No. It's not inserting into the db at all. I don't know about the InsertCommandType="StoredProcedure"...I figure it was necessary because I saw it in a tutorial, but so far it isn't working. – user3736492 Nov 12 '14 at 22:11
  • I took off the InsertCommandType="StoredProcedure" and seems to be inserting into the db, but I still don't know how pull the scope_identity and redirect the Id. – user3736492 Nov 12 '14 at 22:17

0 Answers0