Depending on the precise error message, either the
Contacts
table doesn't contain one or more of the referenced columns (
FirstName
,
MiddleName
,
LastName
,
EmailAddress
,
CompanyID
,
IsDelete
); or the
dbo.ClientName
function doesn't exist, or is not accessible; or there is an error in the
dbo.ClientName
function; or your row doesn't contain a
Client#
cell; or the
Client#
cell contains an unexpected value.
Without seeing your database and the actual query you're trying to execute, we can't tell you precisely what the problem is.
However, removing the SQL Injection vulnerability is simple, and would eliminate the last possibility (an invalid value):
const string query = "SELECT RTRIM(dbo.ClientName(FirstName,MiddleName,LastName)) + ',' + EmailAddress As Contact FROM Contacts WHERE CompanyID = {0} And (IsDelete Is Null Or IsDelete = 0)";
object companyID = ((System.Windows.Forms.DataGridViewComboBoxCell)grvJobList.Rows[grvJobList.CurrentRow.Index].Cells["Client#"]).Value;
DataGridViewComboBoxCell ContactCmb = new DataGridViewComboBoxCell();
using (EFDbContext db = new EFDbContext())
{
var contacts = db.Database.SqlQuery<string>(query, companyID);
ContactCmb.DataSource = contacts.ToList();
}
EDIT: Based on the exception message, the problem is with the
ValueMember
and
DisplayMember
properties on your column. These point to properties on the items within the data source collection. But the items are simple
string
values, which won't have a property called
Contact
.
Probably the simplest option is to create a class to represent the data:
public sealed class ContactName
{
public string Contact { get; set; }
}
Then use that class in your query:
using (EFDbContext db = new EFDbContext())
{
var contacts = db.Database.SqlQuery<ContactName>(query, companyID);
ContactCmb.DataSource = contacts.ToList();
}
ContactCmb.ValueMember = nameof(ContactName.Contact);
ContactCmb.DisplayMember = nameof(ContactName.Contact);