简体   繁体   中英

Stored Procedure not Inserting New Row

I have a stored procedure that should be inserting a new row in a table for every row in a form I have on an application, but instead is concatenating the values and storing them into one row in SQL Server.

What's strange is I have ANOTHER stored procedure that does exactly what it's supposed to do, take every row in a form and inserts each row into a SQL Server table as unique rows.

For the life of me, I cannot find where/how the two are creating different outputs - both should be performing the same.

Here is the stored procedure that works.

Sub:

Private Sub AddHOMEFunds(ByVal HomeID As Integer)
    clearHOMEFunds(HomeID)
    Dim i As Integer
    For i = 0 To Request.Form.Count - 1
        If InStr(Request.Form.Keys(i), "HOMEprojectName") > 0 Then
            Dim connProperties As String = Globals.connstring
            Dim sql As String = "spAddHOMEFunds"
            Dim objConn As New SqlConnection(connProperties)
            objConn.Open()
            Dim cmd As New SqlCommand(sql, objConn)
            With cmd
                .CommandType = Data.CommandType.StoredProcedure
                .Parameters.AddWithValue("@HOMEID", HomeID)
                .Parameters.AddWithValue("@HOMEprojectName", Request.Form.Item(i))
                .Parameters.AddWithValue("@HOMEfiscalYear", Request.Form.Item(i + 1))
                .Parameters.AddWithValue("@HOMEfundingReceived", Request.Form.Item(i + 2))
                .Parameters.AddWithValue("@HOMEunitsDeveloped", Request.Form.Item(i + 3))
                .Parameters.AddWithValue("@HOMEremainingFunds", Request.Form.Item(i + 4))
                .Parameters.AddWithValue("@HOMEfinalDraw", Request.Form.Item(i + 5))
            End With
            cmd.ExecuteNonQuery()
            objConn.Close()
        End If
    Next
End Sub

Stored procedure:

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[spAddHOMEFunds]    Script Date: 05/20/2014 11:25:53 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spAddHOMEFunds]
            @HOMEID int
           ,@HOMEprojectName varchar(max)
           ,@HOMEfiscalYear varchar(max)
           ,@HOMEfundingReceived varchar(max)
           ,@HOMEunitsDeveloped varchar(max)
           ,@HOMEremainingFunds varchar(max)
           ,@HOMEfinalDraw varchar(max)
AS
BEGIN   
    SET NOCOUNT ON;
    INSERT INTO [dbo].[HOMEFunds]
           ([HOMEID]
           ,[HOMEprojectName]
           ,[HOMEfiscalYear]
           ,[HOMEfundingReceived]
           ,[HOMEunitsDeveloped]
           ,[HOMEremainingFunds]
           ,[HOMEfinalDraw])
     VALUES
            (@HOMEID
           ,@HOMEprojectName
           ,@HOMEfiscalYear
           ,@HOMEfundingReceived
           ,@HOMEunitsDeveloped
           ,@HOMEremainingFunds
           ,@HOMEfinalDraw)

END

And here is the stored procedure that does NOT work.

Sub:

Private Sub AddHOMEFundsReceived(ByVal HomeID As Integer)
    clearHOMEFundsReceived(HomeID)
    Dim i As Integer
    For i = 0 To Request.Form.Count - 1
        If InStr(Request.Form.Keys(i), "FundsReceivedProjectName") > 0 Then
            Dim connProperties As String = Globals.connstring
            Dim sql As String = "spAddHOMEFundsReceived"
            Dim objConn As New SqlConnection(connProperties)
            objConn.Open()
            Dim cmd As New SqlCommand(sql, objConn)
            With cmd
                .CommandType = Data.CommandType.StoredProcedure
                .Parameters.AddWithValue("@HOMEID", HomeID)
                .Parameters.AddWithValue("@FundsReceivedProjectName", Request.Form.Item(i))
                .Parameters.AddWithValue("@FundsReceivedYearReceived", Request.Form.Item(i + 1))
                .Parameters.AddWithValue("@FundsReceivedSource", Request.Form.Item(i + 2))
                .Parameters.AddWithValue("@FundsReceivedAmount", Request.Form.Item(i + 3))
                .Parameters.AddWithValue("@FundsReceivedOutcome", Request.Form.Item(i + 4))
                .Parameters.AddWithValue("@FundsReceivedFundsRemaining", Request.Form.Item(i + 5))
                .Parameters.AddWithValue("@FundsReceivedPercentRemaining", Request.Form.Item(i + 6))
            End With
            cmd.ExecuteNonQuery()
            objConn.Close()
        End If
    Next
End Sub

Stored procedure:

USE [DB]
GO
/****** Object:  StoredProcedure [dbo].[spAddHOMEFundsReceived]    Script Date: 05/20/2014 10:44:19 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spAddHOMEFundsReceived]
            @HOMEID int
           ,@FundsReceivedProjectName varchar(max)
           ,@FundsReceivedYearReceived varchar(max)
           ,@FundsReceivedSource varchar(max)
           ,@FundsReceivedAmount varchar(max)
           ,@FundsReceivedOutcome varchar(max)
           ,@FundsReceivedFundsRemaining varchar(max)
           ,@FundsReceivedPercentRemaining varchar(max)
AS
BEGIN   
    SET NOCOUNT ON;
    INSERT INTO [dbo].[HOMEFundsReceived]
           ([HOMEID]
           ,[FundsReceivedProjectName]
           ,[FundsReceivedYearReceived]
           ,[FundsReceivedSource]
           ,[FundsReceivedAmount]
           ,[FundsReceivedOutcome]
           ,[FundsReceivedFundsRemaining]
           ,[FundsReceivedPercentRemaining])
    VALUES  (@HOMEID
           ,@FundsReceivedProjectName 
           ,@FundsReceivedYearReceived 
           ,@FundsReceivedSource 
           ,@FundsReceivedAmount 
           ,@FundsReceivedOutcome 
           ,@FundsReceivedFundsRemaining 
           ,@FundsReceivedPercentRemaining)
END

Again, the working stored procedure creates a new row for each form row on my application.

The stored procedure that does not work, is concatenating and creating comma-separated values for each form row all in one table row in SQL Server.

Simply put, I need the stored procedure that is not working correctly to do EXACTLY the same as the working stored procedure.

Any help appreciated!

It turns out the issue was not with the stored procedure or sub calling the stored procedure.

The issue was the ID tags for the inputs in the table were being duplicated, resulting in the call for that ID to merge all of the cell values in that column together. Weird, I did not realize this was even possible but I do see some use with that approach.

Nonetheless, scratch this one up as a fat-finger coding error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM