• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2223
  • Last Modified:

An SqlParameter with parametername "ID" is not contained by this SqlParameterCollection

hi,

i create a sqldataadapter in windows form and create a criteria for a column. I want to add parameter to dataadapter's selectcommand with column name but it gives an error.

Error:
"An SqlParameter with parametername "ID" is not contained by this SqlParameterCollection"


My select command which is created by dataadapter:
this.sqlSelectCommand1.CommandText = "SELECT ID, ad, soyad FROM tablo WHERE (ID = @Param2)";

And my code to add parameter to this command:
sqlDataAdapter1.SelectCommand.Parameters["ID"].Value=textBox1.Text;

i don't want to access to this parameter with @param2 value, i want to access with ID value.

Thanks for help guys..
0
sercanparlak
Asked:
sercanparlak
  • 3
  • 3
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson06.aspx

this.sqlSelectCommand1.CommandText = "SELECT ID, ad, soyad FROM tablo WHERE (ID = @ID)";

SqlParameter param  = new SqlParameter();
    param.ParameterName = "@ID";
    param.Value         = textbox1.text;

this.sqlSelectCommand1.Parameters.Add(param);
0
 
sercanparlakAuthor Commented:
thanks for answer angel but i don't want to use @ID for parameter. i want to use ? for parameter as in my code.

querybuilder in dataadapter i defined my sqlcommand like this :
SELECT ID, ad, soyad FROM tablo WHERE (ID = ?)

now i want to access this parameter with ID name like this :
sqlDataAdapter1.SelectCommand.Parameters["ID"].Value=textBox1.Text;

i am waiting your answers..
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the problem is that there is no name "ID" assigned automatically to the parameter by the querybuilder.

you might try to use the index instead of a name:
sqlDataAdapter1.SelectCommand.Parameters[1].Value=textBox1.Text;

or, as shown above, build the command text and the parameters collection manually (recommended)
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
sercanparlakAuthor Commented:
i can access to the parameter by two ways:

this.sqlSelectCommand1.CommandText = "SELECT ID, ad, soyad FROM tablo WHERE (ID = @Param2)";

and:
sqlDataAdapter1.SelectCommand.Parameters[0].Value=textBox1.Text;

but i want to access as in here :
http://www.startvbdotnet.com/ado/sqlparameters.aspx

like this : sqlDataAdapter1.SelectCommand.Parameters["ID"].Value=textBox1.Text;
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:

this.sqlSelectCommand1.CommandText = "SELECT ID, ad, soyad FROM tablo WHERE (ID = ?)";
0
 
sercanparlakAuthor Commented:
i read an article that say in oledb you can use parameters name when passing but if you're using sqlclient you cann't use parameter's name. Thanks for replies angel...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now