Sql Parameter not found error

J

Jim H

Learning ADO.NET is proving to be difficult.

The last statement below produces the error indicated right
above it (Parameter not found) -- and I have no ideal as to
why. The Count Property for the Parameters Collection
contains 1. What am I overlooking?

// -- Code Block --------------------------------------------------
// Define sqlDataAdapter3
System.Data.SqlClient.SqlDataAdapter sqlDataAdapter3;

// Define sqlSelectCommend3
System.Data.SqlClient.SqlCommand sqlSelectCommand3;

// Create object sqlDataAdapter3
sqlDataAdapter3 = new System.Data.SqlClient.SqlDataAdapter();

// Create object sqlSelectCommand3
sqlSelectCommand3 = new System.Data.SqlClient.SqlCommand();

// Load the desired sql text command into sqlSelectCommand3
sqlSelectCommand3.CommandText = "SELECT FNXApplication, FNXOriginalName, " +
"FNXSource, FNXNewName, FNXComment FROM " +
"FieldNameXRef WHERE (FNXApplication = @FNXApplication)";

// Add one parameter (FNXApplication) to sqlSelectCommand3 <=== Parameter Added
sqlSelectCommand3.Parameters.Add( new System.Data.SqlClient.SqlParameter(
"@FNXApplication", System.Data.SqlDbType.VarChar, 5, "FNXApplication" ));

// Tell sqlDataAdapter3 to use sqlSelectCommand3 as it's SELECT Command
sqlDataAdapter3.SelectCommand = sqlSelectCommand3;

// Produces Error: An SqlParameter with ParameterName 'FNXApplication' is <=== Error
// not contained by this SqlParameterCollection.
sqlDataAdapter3.SelectCommand.Parameters["FNXApplication"].Value = selectedApp;
// -- End Code Block ----------------------------------------------
 
M

Mohammad Samara

What if you replace adding the parameter to:

// Add one parameter (FNXApplication) to sqlSelectCommand3 <===
Parameter Added
sqlSelectCommand3.Parameters.Add("@FNXApplication",
System.Data.SqlDbType.VarChar, 5, "FNXApplication" ));

Does this give you the same error?

Mohamamd.
 
V

vinu

Hi Jim,

Simple mistake, see the highlighted
The parameter name you created with the bellow statement is @FNXApplication

sqlSelectCommand3.Parameters.Add( new System.Data.SqlClient.SqlParameter("@FNXApplication", System.Data.SqlDbType.VarChar, 5, "FNXApplication" ));

but while passing the value you missing you using FNXApplication, missing @

Cheers
Vinu


Jim H said:
Learning ADO.NET is proving to be difficult.

The last statement below produces the error indicated right
above it (Parameter not found) -- and I have no ideal as to
why. The Count Property for the Parameters Collection
contains 1. What am I overlooking?

// -- Code Block --------------------------------------------------
// Define sqlDataAdapter3
System.Data.SqlClient.SqlDataAdapter sqlDataAdapter3;

// Define sqlSelectCommend3
System.Data.SqlClient.SqlCommand sqlSelectCommand3;

// Create object sqlDataAdapter3
sqlDataAdapter3 = new System.Data.SqlClient.SqlDataAdapter();

// Create object sqlSelectCommand3
sqlSelectCommand3 = new System.Data.SqlClient.SqlCommand();

// Load the desired sql text command into sqlSelectCommand3
sqlSelectCommand3.CommandText = "SELECT FNXApplication, FNXOriginalName, " +
"FNXSource, FNXNewName, FNXComment FROM " +
"FieldNameXRef WHERE (FNXApplication = @FNXApplication)";

// Add one parameter (FNXApplication) to sqlSelectCommand3 <=== Parameter Added
sqlSelectCommand3.Parameters.Add( new System.Data.SqlClient.SqlParameter(
"@FNXApplication", System.Data.SqlDbType.VarChar, 5, "FNXApplication" ));

// Tell sqlDataAdapter3 to use sqlSelectCommand3 as it's SELECT Command
sqlDataAdapter3.SelectCommand = sqlSelectCommand3;

// Produces Error: An SqlParameter with ParameterName 'FNXApplication' is <=== Error
// not contained by this SqlParameterCollection.
sqlDataAdapter3.SelectCommand.Parameters["FNXApplication"].Value = selectedApp;

change the above code
to
sqlDataAdapter3.SelectCommand.Parameters["@FNXApplication"].Value = selectedApp;
 
M

Mohammad Samara

Vinu,

"FNXApplication" in the 4th position is a the name of the source
column, it has nothing to do with the parameter.
 

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