[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2189
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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