Solved

Retrieve one specific record in C#

Posted on 2011-03-24
8
402 Views
Last Modified: 2012-05-11
The first part of the attached code works and finds a random RecordId.  I also need the EmployeeId.  Since there is only one EmpId for each RcdId, I'm trying to find code to retrieve one record with a RecId of txtRcdId (which is the end result of the code).  
I added the last 2 lines to try and accomplish this but I get the error...
   'Randomizer.TimeClockPlusDataContext' does not contain a definition for 'Tables' and no    
   extension method 'Tables' accepting a first argument of  
   type 'Randomizer.TimeClockPlusDataContext' could be found (are you missing a using
   directive or an assembly reference?)      
I 'm really new to C# and could really use some detailed help.  My environment is Visual Studio, the language is C# and I'm attached to a SQL Server db.
 
//This works...
 var dbTimeClockPlus = new TimeClockPlusDataContext();
            var SearchInfo = (from c in dbTimeClockPlus.EmployeeLists where c.Suspend == false select c.RecordId).FirstOrDefault();
            Random r = new Random();
            int RcdId = SearchInfo == 0 ? 1 : SearchInfo;
            RcdId = r.Next(1, RcdId);
            txtRcdId.Text = Convert.ToString(RcdId);

//This part does not work...
            DataRow[] returnedRows;
            returnedRows = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);

Open in new window

0
Comment
Question by:BobRosas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 14

Expert Comment

by:systan
ID: 35208763
DataRow[] returnedRows;
            returnedRows[1] = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
0
 

Author Comment

by:BobRosas
ID: 35208989
Thank you for your quick response!
I tried your suggestion but still get the same error.  Am I missing a definition somewhere.  I read that sometimes when you get this error it's because you need...
     using System.Linq;
but I have that.  Maybe I'm missing something else?  This is a newly set up form so maybe I'm missing something basic?  
0
 
LVL 14

Expert Comment

by:systan
ID: 35209110
yes_its_a_linQ_usage;

did_you_try;
DataRow returnedRows = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
or
DataRow[] returnedRows;
returnedRows[0] = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:BobRosas
ID: 35209206
I was doing the 2nd way you listed.  So I just tried the 1st way.  They are both giving me the same error.
0
 
LVL 14

Expert Comment

by:systan
ID: 35209308
if_youve_tried_this_?,
DataRow returnedRows = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);

then
there's_a_wrong_in_your_firsts_lines;
//This works...
 var dbTimeClockPlus = new TimeClockPlusDataContext();
            var SearchInfo = (from c in dbTimeClockPlus.EmployeeLists where c.Suspend == false select c.RecordId).FirstOrDefault();
            Random r = new Random();
            int RcdId = SearchInfo == 0 ? 1 : SearchInfo;
            RcdId = r.Next(1, RcdId);
            txtRcdId.Text = Convert.ToString(RcdId);

that_if_your_sure_about;
dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
0
 

Author Comment

by:BobRosas
ID: 35209458
I'm not sure what I'm looking for.  If I run the following code all by itself...
           var dbTimeClockPlus = new TimeClockPlusDataContext();
           var SearchInfo = (from c in dbTimeClockPlus.EmployeeLists  where c.Suspend == false select c.RecordId).FirstOrDefault();
            Random r = new Random();
            int RcdId = SearchInfo == 0 ? 1 : SearchInfo;
            RcdId = r.Next(1, RcdId);
            txtRcdId.Text = Convert.ToString(RcdId);

Then my form displays, I press the button and a random RcdId appear in my text box.

If I add this code to the above code...
          DataRow[] returnedRows;
          returnedRows[0] = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
I get the above error.

If I add just this one line of code instead of the 2 right above....
           DataRow returnedRows = dbTimeClockPlus.Tables["EmployeeLists"].Select(txtRcdId);
I still get the same error.
0
 
LVL 14

Accepted Solution

by:
systan earned 125 total points
ID: 35209633
>>random RcdId appear in my text box

I_think_you_want;
returnedRows.Add
or
a_collection_of_rows?

please_see_the_links;
http://msdn.microsoft.com/en-us/library/system.data.datarowview%28v=VS.80%29.aspx
http://msdn.microsoft.com/en-us/library/system.data.datarowcollection%28v=VS.80%29.aspx
0
 

Author Closing Comment

by:BobRosas
ID: 35209979
Thank you for your time and the links.  Neither one of them look like they can take the result of what I have and display another field from the same record but I do apprecaite you trying and I can try posting another question.  Thanks again!
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question