views:

644

answers:

2

I have an ASP.Net form, where it grabs a value from a textbox:

<asp:TextBox ID="txtID" runat="server" maxlength=9></asp:TextBox>

The ID HAS to be 9 numbers.

After it grabs it, I want to insert it into a database (SQL Server 2005), so I build a parameterized string,

'The Query
cmd.CommandText = "insert into table (aid) values ('@aid')"
cmd.Connection = conn

'Grab the value
cmd.Parameters.add("@aid", SqlDBType.Int).value = txtID.text

'Execute!
cmd.Connection.Open()
cmd.ExecuteNonQuery

However, it doesn't let me. It keeps giving the following error message:

Conversion failed when converting the varchar value '@aid' to data type int.

So I've tried a variety of things:

cmd.Parameters.add("@aid", SqlDBType.Int).value = 999999999
cmd.Parameters.add("@aid", SqlDBType.Int).value = Convert.ToInt16(txtID.text)
cmd.Parameters.add("@aid", SqlDBType.Int).value = Convert.ToInt32(txtID.text)
cmd.Parameters.add("@aid", SqlDBType.Int).value = Convert.ToInt64(txtID.text)

Nothing works. Inside the database, the type is "int".

Any ideas?

+5  A: 

Remove the quotes around @aid in your query, so that it looks like so:

cmd.CommandText = "insert into table (aid) values (@aid)"

Otherwise, you're sending the code mixed messages. Parameters are never enclosed in quotes. They are string literals if they're enclosed in quotes. Additionally, in pure SQL, numbers are not enclosed in quotes, but text values (varchar and the like) are. So, remove the quotes, and the parameter should have no issues being created.

Parameters aren't inserted straight into SQL wholesale. They're plopped in after SQL Server has parsed the query. Therefore, parameters should just be on their own, as they're taken as string literals if they aren't. The parameterization will take care to convert the parameter to the right data type for you. See this post for more on how parameters work behind the scenes.

Eric
Even if it were a varchar parameter, you _never_ encase parameter names in quotes.
Joel Coehoorn
BTW: welcome to the 10K club.
Joel Coehoorn
Aww, looks like you're capped for the day. Well, maybe he'll accept your answer.
Joel Coehoorn
Haha, I've been trying to get that accepted since 8AM for the 10k club. I was waiting for another upvote to edit in the clause about parameters. I didn't want to downvote the answer below to keep this on top, since it's a fairly inaccurate description of what happens.
Eric
That did the trick, thanks!
Anton
Woot! Thank you, Anton!
Eric
The correction is the right one to do, but the reason is that the query is trying to insert the string "@aid" instead of using the parameter. Inside the string literal in the query, @aid is not considered to be a parameter but just text.
Guffa
@Guffa: Sorry I wasn't clear, edited to fix it.
Eric
lol, I should of read Joel's advice more carefully. I just spent another 10 minutes, wondering why the query was truncating values. NEVER encase parameter names in quotes, or it'll interpret them as strings, not parameters, leaving your db with values like, "@username", "@address", etc...
Anton
@Anton: Glad you learned something today! Good luck!
Eric
+2  A: 

Your sql query is the problem. You are trying to do

INSERT INTO TABLE(aid) VALUES('123456789')

You need to drop the quotes and do

INSERT INTO TABLE(aid) VALUES(123456789)
Bela
That's not exactly how parameters are applied, though. The first is actually valid SQL. See: http://stackoverflow.com/questions/1263125
Eric
ah yes, duhThe first would work because it could convert it to an int. I knew the problem was the quotes, but was thinking it was the problem for the wrong reason.
Bela