Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Query Problems

Reply
Thread Tools

Query Problems

 
 
Don Kim
Guest
Posts: n/a
 
      09-07-2004
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


 
Reply With Quote
 
 
 
 
Chris R. Timmons
Guest
Posts: n/a
 
      09-07-2004
"Don Kim" <> wrote in
news:lXj%c.12648$. 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/
 
Reply With Quote
 
 
 
 
Don Kim
Guest
Posts: n/a
 
      09-07-2004
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET won't retrieve query results that depend on union query Eric Nelson ASP .Net 5 02-04-2009 10:51 PM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM
xpath query query David Gordon XML 2 05-18-2005 03:33 PM
CAML Query: Multiple Query Fields Issue Jon F. ASP .Net Web Services 0 05-12-2004 08:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57