Link to home
Start Free TrialLog in
Avatar of Vx_Chemical
Vx_Chemical

asked on

A field or property with the name 'Id' was not found on the selected data source.

I'm working on a gridview, trying to get it to show the value of a table translated into text from another table, if you get what i mean, but it gives me the above error

A field or property with the name 'Id' was not found on the selected data source.

I cant figure out why it tells me that, table does have a column called Id

Heres the code

   <asp:TemplateField HeaderText="Reason For Access">
                                <ItemTemplate>
                                    <asp:Label ID="locationLabel" runat="server" Text='<%# Reason( (int) Eval("reason_Id") ) %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>

and the code behind

 SqlConnection myConnectionSetting = new SqlConnection(ConfigurationSettings.AppSettings["VisitorAccessConnectionString"]);
            myConnectionSetting.Open();
            SqlDataReader myReader = null;
            SqlCommand myCommand = new SqlCommand("SELECT * FROM [ReasonForAccess] WHERE (Id = '" + reason_Id + "')", myConnectionSetting);
            myReader = myCommand.ExecuteReader();
            while (myReader.Read())
            {
                location = myReader["Reason"].ToString();
            }

The table, contains the columns, Id and Reason


Thanks
Avatar of robasta
robasta
Flag of Zimbabwe image

make sure that the table 'ReasonForAccess' has a column named 'ID' because you have thi in the select statement
WHERE (Id = '" + reason_Id + "')"

--from the templatefield, it seems the field may be 'reason_Id', rather than 'Id'. If that is the case,  then change the sql statement to :
WHERE (reason_Id = '" + reason_Id  + "')"

Hi,

Try giving the column names in the select list instead of *
Avatar of Vx_Chemical
Vx_Chemical

ASKER

hi rajvja, that gives me the same error
ASKER CERTIFIED SOLUTION
Avatar of drypz
drypz
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial