468,719 Members | 1,770 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

Query Problems

Hi,
I'm trying to port the IGo Portal on the gotdotnet site from vb.net to c#.
I'm having problems with the following:

public DataSet GetAnnouncements(int moduleId) {

// Create Instance of Connection and Command Object
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("SELECT ItemID,
CreatedByUser, CreatedDate, Title, MoreLink, MobileMoreLink, ExpireDate,
Description FROM Portal_Announcements WHERE ModuleID = ? AND ExpireDate >
?", myConnection);

// Mark the Command
myCommand.SelectCommand.CommandType = CommandType.Text;

// Add Parameters
SqlParameter parameterModuleId = new SqlParameter("@ModuleID",
SqlDbType.Int, 4);
parameterModuleId.Value = moduleId;
myCommand.SelectCommand.Parameters.Add(parameterMo duleId);

// Add Parameters
SqlParameter parameterExpireDate = new SqlParameter("@ExpireDate",
SqlDbType.DateTime);
parameterExpireDate.Value = DateTime.Now;
myCommand.SelectCommand.Parameters.Add(parameterEx pireDate);

// Create and Fill the DataSet
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);

// Return the DataSet
return myDataSet;
}

I get this error: System.Data.SqlClient.SqlException: Line 1: Incorrect
syntax near '?'.
Does anyone have any ideas? Thanks.

- Don Kim
Nov 18 '05 #1
2 1218
"Don Kim" <in**@nospam.donkim.info> wrote in
news:lX*******************@newssvr21.news.prodigy. com:
Hi,
I'm trying to port the IGo Portal on the gotdotnet site from
vb.net to c#. I'm having problems with the following:

public DataSet GetAnnouncements(int moduleId) {

// Create Instance of Connection and Command Object
SqlConnection myConnection = new
SqlConnection(ConfigurationSettings.AppSettings["connectionString
"]);
SqlDataAdapter myCommand = new
SqlDataAdapter("SELECT ItemID,
CreatedByUser, CreatedDate, Title, MoreLink, MobileMoreLink,
ExpireDate, Description FROM Portal_Announcements WHERE ModuleID
= ? AND ExpireDate > ?", myConnection);

// Mark the Command
myCommand.SelectCommand.CommandType =
CommandType.Text;

// Add Parameters
SqlParameter parameterModuleId = new
SqlParameter("@ModuleID",
SqlDbType.Int, 4);
parameterModuleId.Value = moduleId;
myCommand.SelectCommand.Parameters.Add(parameterMo dul
eId);

// Add Parameters
SqlParameter parameterExpireDate = new
SqlParameter("@ExpireDate",
SqlDbType.DateTime);
parameterExpireDate.Value = DateTime.Now;
myCommand.SelectCommand.Parameters.Add(parameterEx pir
eDate);

// Create and Fill the DataSet
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet);

// Return the DataSet
return myDataSet;
}

I get this error: System.Data.SqlClient.SqlException: Line 1:
Incorrect syntax near '?'.

Does anyone have any ideas? Thanks.


Don,

Parameters in SQL commands used in the System.Data.SqlClient
namespace are referenced by name, not position. Therefore, you need
to replace the "?" placeholders in the SELECT statement with the
parameter name:

SqlDataAdapter myCommand = new SqlDataAdapter(
@"SELECT ItemID, CreatedByUser, CreatedDate, Title, MoreLink,
MobileMoreLink, ExpireDate, Description FROM
Portal_Announcements WHERE ModuleID = @ModuleID AND
ExpireDate > @ExpireDate", myConnection);

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 18 '05 #2
> SqlDataAdapter myCommand = new SqlDataAdapter(
@"SELECT ItemID, CreatedByUser, CreatedDate, Title, MoreLink,
MobileMoreLink, ExpireDate, Description FROM
Portal_Announcements WHERE ModuleID = @ModuleID AND
ExpireDate > @ExpireDate", myConnection);

Absolutely correct. Forgot that oledb provider is different from sql.
Thanks.

- Don Kim
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by carverk | last post: by
5 posts views Thread by sulemanzia | last post: by
3 posts views Thread by faceman28208 | last post: by
22 posts views Thread by Stan | last post: by
2 posts views Thread by webhead74 | last post: by
9 posts views Thread by Bob Darlington | last post: by
1 post views Thread by Oskars | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.