what is the problem with this?

M

m.a

Hello,

I am newbie and I am following this tutorial:



http://www.asp.net/learn/data-access/tutorial-01-cs.aspx

I am using MSVC 2008 and Access database.



I have my own database which is very similar to the database which is used
in this tutorial.



everything seems to be working till I am getting to step 3: Adding
Parameterized Methods to the Data Access Layer.



I cannot create an sql with parameterized data. my sql is as follow:



SELECT ID_Item, Title, [Desc], [image], User_ID
FROM Items
WHERE User_ID = @User_ID



but the system cannot detect that I used a parameter for where clause. it
complains that it can parse code after @ sign.



What is the problem and How can I fix it?



Best regards
 
A

Andy

Hi,

I think the issue is that Access (Jet DB) doesn't understand @User_Id
as a parameter. I think you need to use a ? character instead. What
that means is that you have to add parameter values in the order they
appear in the sql statement.

Andy
 
M

m.a

Thanks Andy,

I tried ? but it did not work. I did some manual changes to the generated
code and I could manage to fix it but the problem is that as soon as I add
new quary to the system, all of my changed codes are overwritten by system
and I will lose all the changes.

Regards


Hi,

I think the issue is that Access (Jet DB) doesn't understand @User_Id
as a parameter. I think you need to use a ? character instead. What
that means is that you have to add parameter values in the order they
appear in the sql statement.

Andy
 
R

rhaazy

Hello,

  I am newbie and I am following this tutorial:

http://www.asp.net/learn/data-access/tutorial-01-cs.aspx

I am using MSVC 2008 and Access database.

I have my own database which is very similar to the database which is used
in this tutorial.

everything seems to be working till I am getting to step 3: Adding
Parameterized Methods to the Data Access Layer.

I cannot create an sql with parameterized data. my sql is as follow:

SELECT     ID_Item, Title, [Desc], [image], User_ID
FROM         Items
WHERE     User_ID = @User_ID

but the system cannot detect that I used a parameter for where clause. it
complains that it can parse code after @ sign.

What is the problem and How can I fix it?

Best regards

You need code to tell it what to replace the parameter with. For
example in C#:
SqlConnection con = GetConnection();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 500;
cmd.CommandText = "SELECT something FROM somewhere WHERE something =
@yourParam";
cmd.CommandText = cmd.CommandText.Replace("@yourParam", actualValue);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top