My code looks like this:
var settings = ConfigurationManager.ConnectionStrings["InsurableRiskDB"];
string server = ConfigurationManager.AppSettings["Server"];
string cs = String.Format(ConfigurationManager.AppSettings[settings.ProviderName], ConfigurationManager.AppSettings[server]);
SqlConnection connection = new SqlConnection(cs);
string PolicyKeys = "";
for (int i = 0; i < keys.Count(); i++)
{
if (i == keys.Count() - 1)
PolicyKeys += keys[i] ;
else
PolicyKeys += keys[i] + ", ";
}
//have to change the code to pull the user's NBK.
string user = "'DATALOAD'";
const string updateQuery = @"UPDATE [InsurableRisk].[dbo].[Policy]
SET [InsuranceCarrierKey] = @ToKey
,[AuditUser] = @User
,[AuditDate] = SYSDATETIME()
WHERE PolicyKey in (@PolicyKeys) and InsuranceCarrierKey = @FromKey";
using (connection)
{
using (SqlCommand dataCommand = new SqlCommand(updateQuery, connection))
{
dataCommand.Parameters.AddWithValue("@ToKey", toKey);
dataCommand.Parameters.AddWithValue("@User", user);
dataCommand.Parameters.AddWithValue("@PolicyKeys", PolicyKeys);
dataCommand.Parameters.AddWithValue("@FromKey", fromKey);
connection.Open();
dataCommand.ExecuteNonQuery();
connection.Close();
}
}
res = true;
}
catch (Exception ex)
{
MessageBox.Show("There is an error while try in save the changes " + ex.Message, "Error Message", MessageBoxButtons.OKCancel);
res = false;
}
return res;
Now, when i run this code, it says query is unable to execute. It throws and exception stating, it is unable to convert NVarchar to int for variable @PolicyKeys
Any suggestions as to what i am missing in this code?