Elena E
Elena E

Reputation: 85

SqlException: Ambiguous column name

I am getting this error when the code below is run:

Ambiguous column name 'AppointmentID'.
Exception Details: System.Data.SqlClient.SqlException: Ambiguous column name 'AppointmentID'.

Source Error:
Line 68: adapter.Fill(tbl);

This is the function that causes the error:

public Transaction Payment
{
    get
    {
        return Db.Transaction.FindWhere("[AppointmentID]=@0 AND [Type]='paym'", SqlDb.Params(this.ID));
    }
}

Transaction structure:

public partial class Transaction : SqlRow
{
    public int CID { get; set; }

    public DateTime Date { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public decimal Amount { get; set; }
    public decimal Balance { get; set; }
    public int? AppointmentID { get; set; }
    public int LocationID { get; set; }
}

What could be the cause?

Upvotes: 2

Views: 7523

Answers (2)

Look over your query. The column AppointmentID in multiple table.

You must use table aliases look there

For Example :

CREATE TABLE Table1 
(    
   ID int,    
   AppointmentID int,    
   FirstName  varchar(255)
 );

 CREATE TABLE Table2 
 (    
    ID int,    
    AppointmentID int,    
    LastName  varchar(255)
 );

 Select A.FirstName , A.AppointmentID  , B.LastName , B.AppointmentID  
   From Table1 A, Table2 B   
  Where A.AppointmentID  = B.AppointmentID ;

Upvotes: 2

Viktor Bahtev
Viktor Bahtev

Reputation: 4908

The exception says it all. There is more than one column with name AppointmentID in different tables. You can either use table aliases or use [TableName].[AppointmentID] in your query.

You should share your SQL code for more concrete help.

Upvotes: 5

Related Questions