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
Vx_ChemicalAsked:
Who is Participating?
 
drypzCommented:
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

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

0
 
rajvjaCommented:
Hi,

Try giving the column names in the select list instead of *
0
 
Vx_ChemicalAuthor Commented:
hi rajvja, that gives me the same error
0
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.