I've facing this "invalid column name" error when I try to run my vb.net code. This vb.net will pass some parameters to a stored procedure. But when I try to execute the stored procedure from SQL server management studio, it works.
Below is my code:
LoadData("tbl_Sources", "sPlayerID,'',sPlayerName", "tbl_Dest", "dPlayerID,"",dPlayerName")
------------------------------------------
Public Function LoadData(ByVal frmTblname As String, ByVal frmClmnName As String, ByVal toTblName As String, ByVal toClmnName As String) As Integer
Dim con As New SqlClient.SqlConnection
Dim conManager As New Connection
Dim sqlParams() As SqlParameter = New SqlParameter() { _
New SqlParameter("@fromTable", SqlDbType.NVarChar, 50) With {.Value = frmTblname}, _
New SqlParameter("@fromColumn", SqlDbType.Structured, 4000) With {.Value = "'" + frmClmnName + "'"}, _
New SqlParameter("@toTable", SqlDbType.NVarChar, 50) With {.Value = toTblName}, _
New SqlParameter("@toColumn", SqlDbType.NVarChar, 4000) With {.Value = "'" + toClmnName + "'"} _
}
Dim err As Integer
con = conManager.GetConnection(sConn)
err = ConnectionExec.RunSPReturnInteger(con, "insertUnknownDir", sqlParams)
Return err
End Function
---------------------------------------------------------
Public Function RunSPReturnInteger(ByVal con As SqlClient.SqlConnection, ByVal strSP As String, ByVal ParamArray commandParameters() As SqlClient.SqlParameter) As Integer
Dim retVal As Integer
LogManager.WriteLog("#################################### Start " + strSP + " ####################################")
Try
Dim cmd As New SqlCommand(strSP, con)
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandTimeout = 0
Dim p As SqlClient.SqlParameter
For Each p In commandParameters
p = cmd.Parameters.Add(p)
p.Direction = ParameterDirection.Input
p.SqlDbType = SqlDbType.Structured
Next
retVal = cmd.ExecuteNonQuery()
cmd.Dispose()
Return retVal
LogManager.WriteLog("Try RunSPReturnInteger 1 -> SP Name : " + strSP + " > " + retVal.ToString)
Catch ex As Exception
'MsgBox(ex.Message)
LogManager.WriteLog("Catch RunSPReturnInteger 1-> SP Name : " + strSP + ">> " + ex.Message)
Throw New Exception(ex.Message, ex)
Return -1
End Try
End Function
And here is my store procedure:
ALTER procedure [dbo].[insertUnknownDir] @fromTable nvarchar(50),@fromColumn nvarchar(4000),@toTable nvarchar(50),@toColumn nvarchar(4000)
as
begin
declare @Query nvarchar(4000) print 'exec [insertUnknownDir] '+@fromTable+' ,'+@fromColumn+' ,'+@toTable+','+@toColumn+''
SET @Query ='INSERT INTO ['+@toTable+']('+@toColumn+')
SELECT TOP 15 '+@fromColumn+'
FROM '+@fromTable+'
WHERE profile.sPlayerID = '+@fromTable+'.sPlayerID'
exec sp_executesql @Query
END
Any advice/help is appreciated!