Microsoft
Software
Hardware
Network
Question : Invalid column name '
[email protected]
'
The following is a stored procedure I am calling. This is called from a search page where criteria is specified and then submitted.
USE [dbOIT]
GO
/****** Object: StoredProcedure [dbo].[SearchResults] Script Date: 08/23/2010 18:22:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SearchResults]
@Tag nvarchar(100)
,@Email nvarchar(120)
,@Serial nvarchar(50)
,@EquipID nvarchar(4)
,@Platform nvarchar(4)
,@OS nvarchar(4)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql1 nvarchar(4000)
SELECT @sql1='
SELECT
a.AssetID,a.AssetTag,a.OSI
d,a.ModelI
D,a.Equipm
entTypeID,
a.Platform
ID,a.Email
Address,a.
SerialNumb
er
,p.PlatformName,e.Equipmen
tType,m.Mo
delName
,ltrim(rtrim(a.HDD)) as HDD,ltrim(rtrim(a.RAM)) as RAM,o.OperatingSystem
FROM dbo.Assets a
JOIN dbo.EquipmentTypes e WITH (NOLOCK) on e.EquipmentTypeID=a.Equipm
entTypeID
JOIN dbo.Platforms p WITH (NOLOCK) on p.PlatformID=a.PlatformID
JOIN dbo.Models m WITH (NOLOCK) on m.ModelID=a.ModelID
JOIN dbo.OperatingSystems o WITH (NOLOCK) on o.OSId=a.OSId
Where 1=1'
IF @Tag IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.AssetTag = ' + @Tag
IF @Email IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.EmailAddress = ' + @Email + '"'
IF @Serial IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.SerialNumber = ' + @Serial
IF @EquipID IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.EquipmentTypeID = ' + @EquipID
IF @Platform IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.PlatformID = ' + @Platform
IF @OS IS NOT NULL
SELECT @sql1 = @sql1 + ' AND a.OSId = ' + @OS
EXEC(@sql1)
END
Here's the strange part. I can pass Operating System, Platform, Equipment Type, and Asset Tag with total success. However, when I pass Email Address or Serial #, I get the following:
Invalid column name '
[email protected]
'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlE
xception: Invalid column name '
[email protected]
'.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SqlException (0x80131904): Invalid column name '
[email protected]
'.
]
System.Data.SqlClient.SqlC
onnection.
OnError(Sq
lException
exception, Boolean breakConnection) +1951450
System.Data.SqlClient.SqlI
nternalCon
nection.On
Error(SqlE
xception exception, Boolean breakConnection) +4849003
System.Data.SqlClient.TdsP
arser.Thro
wException
AndWarning
(TdsParser
StateObjec
t stateObj) +194
System.Data.SqlClient.TdsP
arser.Run(
RunBehavio
r runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +2394
System.Data.SqlClient.SqlD
ataReader.
ConsumeMet
aData() +33
System.Data.SqlClient.SqlD
ataReader.
get_MetaDa
ta() +83
System.Data.SqlClient.SqlC
ommand.Fin
ishExecute
Reader(Sql
DataReader
ds, RunBehavior runBehavior, String resetOptionsString) +297
System.Data.SqlClient.SqlC
ommand.Run
ExecuteRea
derTds(Com
mandBehavi
or cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +954
System.Data.SqlClient.SqlC
ommand.Run
ExecuteRea
der(Comman
dBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +162
System.Data.SqlClient.SqlC
ommand.Run
ExecuteRea
der(Comman
dBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +32
System.Data.SqlClient.SqlC
ommand.Exe
cuteReader
(CommandBe
havior behavior, String method) +141
System.Data.SqlClient.SqlC
ommand.Exe
cuteDbData
Reader(Com
mandBehavi
or behavior) +12
System.Data.Common.DbComma
nd.System.
Data.IDbCo
mmand.Exec
uteReader(
CommandBeh
avior behavior) +10
System.Data.Common.DbDataA
dapter.Fil
lInternal(
DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +130
System.Data.Common.DbDataA
dapter.Fil
l(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +287
System.Data.Common.DbDataA
dapter.Fil
l(DataSet dataSet, String srcTable) +92
System.Web.UI.WebControls.
SqlDataSou
rceView.Ex
ecuteSelec
t(DataSour
ceSelectAr
guments arguments) +1297
System.Web.UI.DataSourceVi
ew.Select(
DataSource
SelectArgu
ments arguments, DataSourceViewSelectCallba
ck callback) +19
System.Web.UI.WebControls.
DataBoundC
ontrol.Per
formSelect
() +142
System.Web.UI.WebControls.
BaseDataBo
undControl
.DataBind(
) +73
System.Web.UI.WebControls.
GridView.D
ataBind() +4
System.Web.UI.WebControls.
BaseDataBo
undControl
.EnsureDat
aBound() +82
System.Web.UI.WebControls.
CompositeD
ataBoundCo
ntrol.Crea
teChildCon
trols() +72
System.Web.UI.Control.Ensu
reChildCon
trols() +87
System.Web.UI.Control.PreR
enderRecur
siveIntern
al() +44
System.Web.UI.Control.PreR
enderRecur
siveIntern
al() +171
System.Web.UI.Control.PreR
enderRecur
siveIntern
al() +171
System.Web.UI.Control.PreR
enderRecur
siveIntern
al() +171
System.Web.UI.Control.PreR
enderRecur
siveIntern
al() +171
System.Web.UI.Page.Process
RequestMai
n(Boolean includeStagesBeforeAsyncPo
int, Boolean includeStagesAfterAsyncPoi
nt) +842
Any idea what's going wrong here?
Answer : Invalid column name '
[email protected]
'
I meant the below (correction).
You need to quote it because it is a (n)(var)char
1:
SELECT @sql1 = @sql1 + ' AND a.EmailAddress = ' + QuoteName(@Email,'''') + ''
Random Solutions
Default alias in Exchange 2010
Best routers for creating a VPN tunnel between two offices
Cisco ASA 5505 Remote VPN connection - Can't connect
ATM clocking
How to make simple PHP IF function for the following simple scenario ?
Outlook/Exchange Issue with Emails not showing
time tracking with microsoft project
access list on core switch for restricted guest access
Preparing email output in PHP script
Tools to test IOPS for SAN