I want to call a method that takes in a string for each selected item in a listview.
foreach (string item in lvwPoliciesAvailableForHoldBack.SelectedItems)
{
Database.HoldBackPolicy(item);
}
I’m running into this error:
Unable to cast object of type ‘System.Windows.Forms.ListViewItem’ to type ‘System.String’.
Here’s the “HoldBackPolicy” method:
public int HoldBackPolicy(string PolicyNumber)
{
int result = 0;
SqlCommand cmd = new SqlCommand("spHoldBackPolicy", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PolicyNumber", PolicyNumber);
var returnParameter = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);
returnParameter.Direction = ParameterDirection.ReturnValue;
try
{
conn.Open();
cmd.ExecuteNonQuery();
result = Convert.ToInt32(returnParameter.Value);
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
finally
{
conn.Close();
}
return result;
}
You can’t cast a ListViewItem to a string.. the error says it all.
This line:
Is wrong. It must be this:
And this function call, must be this: