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">
                                    <asp:Label ID="locationLabel" runat="server" Text='<%# Reason( (int) Eval("reason_Id") ) %>'></asp:Label>

and the code behind

 SqlConnection myConnectionSetting = new SqlConnection(ConfigurationSettings.AppSettings["VisitorAccessConnectionString"]);
            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

Who is Participating?
Hi! In here

 while (myReader.Read())
                location = myReader["Reason"].ToString();
reason_Id = Convert.ToInt32(myReader["Reason_ID"]); //your column id here

Open in new window

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  + "')"


Try giving the column names in the select list instead of *
Vx_ChemicalAuthor Commented:
hi rajvja, that gives me the same error
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.