Click here to Skip to main content
15,885,810 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to display data in
DataGridViewComboBoxCell
but it is saying that it was unable to find field

Quote:
Field called Contacts does not exist
at System.Windows.Forms.DataGridViewComboxBoxCell.InitializeDisplayMemberPropertyDescriptor


I am combining 2 field when using select query and given name as "Contact" and
fetching data from "Contacts" table

What I have tried:

C#
string Query = "SELECT  RTRIM(dbo.ClientName(FirstName,MiddleName,LastName)) +','+ EmailAddress as Contact FROM  Contacts WHERE  CompanyID=" + ((System.Windows.Forms.DataGridViewComboBoxCell)grvJobList.Rows[grvJobList.CurrentRow.Index].Cells["Client#"]).Value.ToString() + " And (IsDelete Is null Or IsDelete = 0 )";

DataGridViewComboBoxCell ContactCmb = new DataGridViewComboBoxCell();

using (EFDbContext db = new EFDbContext())
{
	var GetData = db.Database.SqlQuery<string>(Query);
	ContactCmb.DataSource = GetData.ToList();
}

ContactCmb.ValueMember  = "Contact";
ContactCmb.DisplayMember = "Contact";
ContactCmb.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox;
grvJobList.Rows[e.RowIndex].Cells[e.ColumnIndex] = ContactCmb;

"Contacts" table stucture
SQL
CREATE TABLE [dbo].[Contacts](
    [ContactsID] [int] IDENTITY(1,1) NOT NULL,
    [CompanyID] [int] NULL,
    [FirstName] [nvarchar](50) NULL,
    [MiddleName] [nvarchar](30) NULL,
    [LastName] [nvarchar](50) NULL,
    [ContactTitle] [nvarchar](50) NULL,
    [MobilePhone] [nvarchar](30) NULL,
    [EmailAddress] [nvarchar](50) NULL,
    [Notes] [nvarchar](max) NULL,
    [SpecialRiggerNUM] [nvarchar](50) NULL,
    [MasterRiggerNUM] [nvarchar](max) NULL,
    [SpecialSignNUM] [nvarchar](50) NULL,
    [MasterSignNUM] [nvarchar](max) NULL,
    [Prefix] [nvarchar](20) NULL,
    [Suffix] [nvarchar](20) NULL,
    [Address] [nvarchar](255) NULL,
    [City] [nvarchar](50) NULL,
    [State] [nvarchar](30) NULL,
    [PostalCode] [nvarchar](20) NULL,
    [Country] [nvarchar](30) NULL,
    [HomePhone] [nvarchar](30) NULL,
    [WorkPhone] [nvarchar](30) NULL,
    [FaxNumber] [nvarchar](20) NULL,
    [AlternativePhone] [nvarchar](30) NULL,
    [FieldPhone] [nvarchar](30) NULL,
    [Pager] [nvarchar](20) NULL,
    [Accounting] [bit] NULL,
    [IsChange] [bit] NULL,
    [IsNewRecord] [bit] NULL,
    [IsDelete] [bit] NULL,
    [ChangeDate] [datetime] NULL,
    CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED
    (
        [ContactsID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Posted
Updated 1-Sep-21 23:34pm
v5
Comments
Devendra Sarang 1-Sep-21 23:53pm    
Right now main issue is not sql injection.

Main issue is that "DataGridViewComboBoxCell" display member is saying "Contact"
filed is not found.

I will improve query once this issue solve.
Richard Deeming 2-Sep-21 3:43am    
Don't say we didn't warn you when your database is stolen or modified, and you're facing multi-million dollar fines for not securing your data properly. 🤦‍♂️
Devendra Sarang 2-Sep-21 4:43am    
Bro sorry to say but please reply regarding my current issue.

I am not right now interested in sql injection

If you have solution to my current problem then help me

I am not ignoring your valuable reply. I am just try to focus on "DataGridViewComboBoxCell" issue. Once it was solved, then will start working on your suggestion too
Richard Deeming 2-Sep-21 4:52am    
Since you haven't provided the structure of your database, nor the complete details of the exception, nobody can tell you what the problem is.

All we can tell you is that your code has a critical security vulnerability which is so easy to exploit that a 3 year old child can do it[^].

But if you're not interested in having dangerous mistakes in your code pointed out, then you've still got a lot of learning to do.

1 solution

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):
C#
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:
C#
public sealed class ContactName
{
    public string Contact { get; set; }
}
Then use that class in your query:
C#
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);
 
Share this answer
 
v2
Comments
Devendra Sarang 2-Sep-21 5:13am    
"Contacts" table stucture

CREATE TABLE [dbo].[Contacts](
[ContactsID] [int] IDENTITY(1,1) NOT NULL,
[CompanyID] [int] NULL,
[FirstName] [nvarchar](50) NULL,
[MiddleName] [nvarchar](30) NULL,
[LastName] [nvarchar](50) NULL,
[ContactTitle] [nvarchar](50) NULL,
[MobilePhone] [nvarchar](30) NULL,
[EmailAddress] [nvarchar](50) NULL,
[Notes] [nvarchar](max) NULL,
[SpecialRiggerNUM] [nvarchar](50) NULL,
[MasterRiggerNUM] [nvarchar](max) NULL,
[SpecialSignNUM] [nvarchar](50) NULL,
[MasterSignNUM] [nvarchar](max) NULL,
[Prefix] [nvarchar](20) NULL,
[Suffix] [nvarchar](20) NULL,
[Address] [nvarchar](255) NULL,
[City] [nvarchar](50) NULL,
[State] [nvarchar](30) NULL,
[PostalCode] [nvarchar](20) NULL,
[Country] [nvarchar](30) NULL,
[HomePhone] [nvarchar](30) NULL,
[WorkPhone] [nvarchar](30) NULL,
[FaxNumber] [nvarchar](20) NULL,
[AlternativePhone] [nvarchar](30) NULL,
[FieldPhone] [nvarchar](30) NULL,
[Pager] [nvarchar](20) NULL,
[Accounting] [bit] NULL,
[IsChange] [bit] NULL,
[IsNewRecord] [bit] NULL,
[IsDelete] [bit] NULL,
[ChangeDate] [datetime] NULL,
CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED
(
[ContactsID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Richard Deeming 2-Sep-21 5:17am    
That would seem to eliminate the first possibility - all of the referenced columns appear to exist.

So what is the exact error message?
Devendra Sarang 2-Sep-21 5:22am    
this one.

https://i.imgur.com/wqa35Az.png
Richard Deeming 2-Sep-21 5:38am    
OK, so the error is that the items you've bound to are just string instances; they don't have a property called Contact.

See my updated solution for a fix.
Devendra Sarang 2-Sep-21 5:58am    
Nope. creating sealed class and use of nameof is also giving same error

But i have made one small thing.

I have remove both "DisplayMember" and "valuemember" line and
then data is showing perfectly in "DataGridViewComboBoxCell"

but now issue is i am performing update statement so how to update that contact field



This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900