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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.