I am trying to run this code but I get the error "The parameterized query expects the parameter @faid which was not supplied". At least based on my knowledge, this code looks good. I am using VS 2010 on Windows 7 with SQLEXPRESS as backend.
Thanks in advance.
string getDataQuery;
lcFaid = "70464917-967b-4796-9483-3b0b4b004a3e";
SqlConnection sqlConnection1 = new SqlConnection(ccsConnectionString);
DataSet data = new DataSet();
data.Locale = System.Globalization.CultureInfo.InvariantCulture;
getDataQuery =
"SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";
SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
masterDataAdapter.SelectCommand = new SqlCommand();
masterDataAdapter.SelectCommand.Connection = sqlConnection1;
masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
SqlDbType.UniqueIdentifier, 36, "fai开发者_高级运维d").SourceVersion = DataRowVersion.Original;
masterDataAdapter.SelectCommand.CommandText = getDataQuery;
masterDataAdapter.Fill(data, "famain");
I see that you never use the variable lcFaid
. Perhaps you meant to use that instead of the literal string "faid"
(which not a GUID at all)?
masterDataAdapter.SelectCommand.Parameters.Add("@lcFaid",
SqlDbType.UniqueIdentifier, 36, lcFaid).SourceVersion = DataRowVersion.Original;
Not sure about this - but I would
- first set the command's query text (which defines what parameter there will be)
- only then add the parameters to the collection
Something like this:
string getDataQuery =
"SELECT customer,custtrack,ackdate FROM famain WHERE faid = @lcFaid";
SqlDataAdapter masterDataAdapter = new SqlDataAdapter();
masterDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
masterDataAdapter.SelectCommand = new SqlCommand();
masterDataAdapter.SelectCommand .Connection = sqlConnection1;
// first set the query text
masterDataAdapter.SelectCommand.CommandText = getDataQuery;
// after that, define the parametesr
masterDataAdapter.SelectCommand.Parameters
.Add("@lcFaid", SqlDbType.UniqueIdentifier, 36, "faid").SourceVersion = DataRowVersion.Original;
Also, as "Gabe" has pointed out in his comment - the error message references a parameter @faid
, but you're defining and setting a parameter called @lcFaid
- why??
精彩评论