From: Durango2008 on
I am encountering this error and I am not sure why. In my code I create the
sql string and SQLParameter to deal with the SQL variables.

I am not strong with SQL so I may be missing something here.

Any advice is greatly appreciated.



Web.config:

<add key="myDB" value="DBServ1" />

<add key="myInfo" value="select pTime from @XSearhDB@.dbo.tblServInfo where
service= @myData"/>



CS file:
string query = ConfigurationManager.AppSettings["myInfo"];

query = query.Replace("@myDB@", ConfigurationManager.AppSettings["myDB"]);

connection.Open();

SqlCommand cmd = new SqlCommand(query, connection);

SqlParameter param1 = new SqlParameter();

param1.ParameterName = "@myData";

param1.Value = service;

cmd.Parameters.Add(param1);

//cmd.CommandText = "select pTime from DBServ1.dbo.tblServInfo where
service='netcomm'";



SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())

{

dateStamp = reader.GetDateTime(0).ToString();

}



--- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Erland Sommarskog on
Durango2008 (el_durang0(a)yah00.c0m) writes:
> I am encountering this error and I am not sure why. In my code I create
> the sql string and SQLParameter to deal with the SQL variables.
>
> I am not strong with SQL so I may be missing something here.

The way to approach this from the SQL Server side is to use Profiler
to see what you actually send to SQL Server.

Or simply use the debugger to look what is in query...


--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
From: Dan Guzman on
> <add key="myInfo" value="select pTime from @XSearhDB@.dbo.tblServInfo
> where service= @myData"/>

I see a token named "@XSearhDB@" in the config file

> query = query.Replace("@myDB@", ConfigurationManager.AppSettings["myDB"]);

but it is named "@myDB@" in the code. That would explain the error.

Personally, I would use a different token naming scheme (e.g.
"$(XSearhDB)"). That way you'll get a syntax error rather than a missing
variable error if the token replacement fails.


--
Hope this helps.

Dan Guzman
SQL Server MVP
http://weblogs.sqlteam.com/dang/

"Durango2008" <el_durang0(a)yah00.c0m> wrote in message
news:hu4s25$2aoc$1(a)adenine.netfront.net...
> I am encountering this error and I am not sure why. In my code I create
> the sql string and SQLParameter to deal with the SQL variables.
>
> I am not strong with SQL so I may be missing something here.
>
> Any advice is greatly appreciated.
>
>
>
> Web.config:
>
> <add key="myDB" value="DBServ1" />
>
> <add key="myInfo" value="select pTime from @XSearhDB@.dbo.tblServInfo
> where service= @myData"/>
>
>
>
> CS file:
> string query = ConfigurationManager.AppSettings["myInfo"];
>
> query = query.Replace("@myDB@", ConfigurationManager.AppSettings["myDB"]);
>
> connection.Open();
>
> SqlCommand cmd = new SqlCommand(query, connection);
>
> SqlParameter param1 = new SqlParameter();
>
> param1.ParameterName = "@myData";
>
> param1.Value = service;
>
> cmd.Parameters.Add(param1);
>
> //cmd.CommandText = "select pTime from DBServ1.dbo.tblServInfo where
> service='netcomm'";
>
>
>
> SqlDataReader reader = cmd.ExecuteReader();
>
> while (reader.Read())
>
> {
>
> dateStamp = reader.GetDateTime(0).ToString();
>
> }
>
>
>
> --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---