Click here to Skip to main content
16,016,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i keep getting the error msg Ambiguous column name 'collectorid'in my sql query.
please what is the problem with this query

SQL
SqlDataAdapter adp = new SqlDataAdapter("SELECT a.Collectorid, a.Title, a.Surname, a.Firstname, a.Middlename, a.Gender, a.Dateofbirth, a.Nationality, a.Religion, a.Maritalstatus, a.Spousename, a.Telephone, a.Postaladdress, a.Residentialaddress, a.Hometownaddress from tblCollectorsRegistration a JOIN tblCollectorscontactperson b ON (a.Collectorid = b.Collectorid) WHERE Collectorid = '" + this.txtCollectorid.Text + "' AND conpersonid = (SELECT MIN(conpersonid) FROM tblCollectorscontactperson)", conn);
Posted

In your Where clause, CollectorId and conpersonid were not preceded by either a. or b. table alias. In the example below, I preceded them by a. table alias.
SQL
SqlDataAdapter adp = new SqlDataAdapter("SELECT a.Collectorid, a.Title, a.Surname, a.Firstname, a.Middlename, a.Gender, a.Dateofbirth, a.Nationality, a.Religion, a.Maritalstatus, a.Spousename, a.Telephone, a.Postaladdress, a.Residentialaddress, a.Hometownaddress from tblCollectorsRegistration a JOIN tblCollectorscontactperson b ON (a.Collectorid = b.Collectorid) WHERE a.Collectorid = '" + this.txtCollectorid.Text + "' AND a.conpersonid = (SELECT MIN(conpersonid) FROM tblCollectorscontactperson)", conn);

Also, by using the parameter this.txtCollectorid.Text in a concatenated string, you leave your application open to SQL Injection Attack. All parameters should be passed using the SQLParameter Class. Using SQLParameter Class also improves performance.
 
Share this answer
 
v2
you forgot the table prefix of the Collectorid in the where clause.

Here the corrected version that should work:

SqlDataAdapter adp = new SqlDataAdapter("SELECT a.Collectorid, a.Title, a.Surname, a.Firstname, a.Middlename, a.Gender, a.Dateofbirth, a.Nationality, a.Religion, a.Maritalstatus, a.Spousename, a.Telephone, a.Postaladdress, a.Residentialaddress, a.Hometownaddress from tblCollectorsRegistration a JOIN tblCollectorscontactperson b ON (a.Collectorid = b.Collectorid) WHERE a.Collectorid = '" + this.txtCollectorid.Text + "' AND conpersonid = (SELECT MIN(conpersonid) FROM tblCollectorscontactperson)", conn);
 
Share this answer
 

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