DataSource Controls :: Accessing FK From A Query Via Entity Query

Jan 21, 2010

I've got a query such as

Dim MediaQuery =
From m
In dB.DOWNLOADS _Where m.ID = id _Select

which returns a record from the database. One of the fields in the record is a FK to another table. I need to read this value to be able to set a dropdown based on the ID but I can't work out how to access it. For a standard record I can just do the following txtTitle.Text = MediaQuery.FirstOrDefault().TITLE

However with the foreign key it doesn't work like that. I've tried drpGroup.SelectedIndex = MediaQuery.FirstOrDefault().DOWNLOAD_GROUPS.ID where DOWNLOAD_GROUPS is the FK field but it returns Object reference not set to an instance of an object. If you're simply wanting to read some values from a single db record in the entitiy framework and one is a foreign key how should I go about getting the value?

View 2 Replies


Similar Messages:

DataSource Controls :: SQL Query Re-Write In Linq To Entity?

Jan 26, 2010

Need to figure out the correct way to do a simple SUM function in Linq, but also manipulate one of the feilds coming back by doing a Substring on it, here is the original SQL query:

[Code]....

View 1 Replies

DataSource Controls :: LINQ And C# - Accessing More Then 2 Tables In A Query

Apr 23, 2010

As the title sugests, I am missing a (basic?) concept as I teach myself asp.net, linq, and C#. It has been awhile since I've done anything object oriented, so I don't know how much that is impacting this gap in my understanding. Anyhow, here is what I am trying to do. I have three SQL Server tables: PERSON, POSITION, AREA. There is a 1 to many relationship between PERSON and POSITION, and a 1 to 1 relationship between POSITION and AREA. Given the following code I verify who the person is who has logged into the browser:

[Code]....

I want to be able to return all of the AREAs the person is able to access, based on the position they currently hold. I am able to easily do this in a SQL Server query, and I am sure I can do this with a SQL stored procedure once I pass the EMPLOYEE_ID to the procedure. However, I would rather do this in the page_load event, and load the returned results into gridview1 for the viewer. The issue I am running into is in building the LINQ query. When I try to build it, I can only get access to two of the three tables, depending on which table I select with db. Is it possible to access all three tables in the same query?

View 10 Replies

DataSource Controls :: LINQ To Entity Query - Getting Data From The RDBMS

Jan 1, 2010

if LINQ to Entity queries the EDMX class or its .CSDL or SSDL XML on its way to getting data from the RDBMS?

View 8 Replies

DataSource Controls :: Entity Query To Validate Ntry Not Already In A Table

Jun 16, 2010

I have a button click event that adds a record to a table based on the userid and threadid the user is viewing. I am a bit new to entity so I am stumbling around. I have to where conditions

ThreadID == threadid
UserId == userIdPosts

I have this query but do not know how to execute it and if I have it as is I get this in the yellow screen of death Both the theadid and userIdPosts have data in them.

var query = context.TrackLists.First(t => t.ThreadID == threadid);
context.TrackLists.First(t => t.UserId == userIdPosts);

Sequence contains no elements

Line 90: // find is user has already tracked this thread.
Line 91: var query = context.TrackLists.First(t => t.ThreadID == threadid);
Line 92: context.TrackLists.First(t => t.UserId == userIdPosts);

View 3 Replies

Forms Data Controls :: Entity Datasource And Query Extender Using LISTVIEW

Mar 9, 2011

I have a user control, named productslist.ascx. On pruductslist.ascx, I am using Entity Datasource and a QUERY EXTENDER and LISTview. I have a page Defualt.aspx. On Defualt.aspx, I have a TEXTBOX with a SUBMIT button for searching the site. Everthing works fine so far, AS LONG as the results of the Search is posted back to Default.aspx. What I want is for the results of the search to be posted on a different page.
So I created a new page, Results.aspx and set the postback url to it.

What I want is that when a user clicks the Submit button, after entering a search string, the text the entered is used to execute the query on Productslist.ascx and the results displayed on RESULTS.ASPX. Again, everything works fine as long as the results are posted on same page hosting the usercontrol, BUT I want the postback url to be a different page. I am coding in VB and VS 2010.

View 3 Replies

DataSource Controls :: SqlDataSource And Parameters Query / Trying To Accomplish Is Building Dynamic Query

Aug 22, 2010

1. I have a GridView on my page and it uses sqldatasource with parameterized query. What I want to do is, on page load (where nothing has been selected so no parameter supplied), I want it to query everything (something like SELECT * FROM [this_table]) but since my SelectCommand is something like

SELECT * FROM [this_table] WHERE [this_column] = @someParameters AND [that_column] = @someParameters.

Can I play around with default value to achieve something like that but how ? Now, when the page loads, it doesn't show anything (No Gridview).

2. On my page, I made something like (username, gender, address, and more) and one single search button. That means, no single control enable auto postback. What I am trying to accomplish is building dynamic query

(if username specifed -> SELECT * FROM [this_table] WHERE [username] LIKE @username).

If both username and gender are specified (SELECT * FROM [this_table] WHERE [username] LIKE @username AND [gender] = @gender) and you know the rest. How can I do this using GridView and SqlDataSource ? To my knowledge, I can only specify one SELECT statement in a sqldatasource.

View 11 Replies

DataSource Controls :: Update Query Not Working When Using Query Builder To Configure Table Adapter

Jan 15, 2010

[code]...

This query works perfectly on the query analyser.

But when configuring the Table adapter ,I try executing the query and i get 0 rows affected.

What could I be getting wrong in this case.

NB:Existing GalleryID has been supplied.

View 1 Replies

DataSource Controls :: Can't Find Query's / By Right Clicking On Database Created A Query In Server Explorer?

May 17, 2010

By right clicking on my database i created a query in server explorer. But where are this query stored can't find them back. I should aspect that their is a folder query's like there is a folder tables but this isn't the case.

View 10 Replies

DataSource Controls :: How To Convert Sql Query Into Linq Query

Mar 10, 2010

select Groupid,GroupName,onorusername from palgroup where groupid in (select distinct Groupid

View 5 Replies

ADO.NET :: LINQ Entity Framework Query - Construct "nested" Query?

Jan 22, 2011

I have a web app for our golf club. When I compute handicap index for each golfer, I have to select the most recent scores and then a subset of those scores depending on how many rounds of golf the golfer has played. All the scores are entered into a single SQL Express table called "Rounds". Verbally, this is what I'm trying to do:

1) select the twenty most recent golf scores (sort on date descending, "take(20)") [if less than 20 records, then select all available];

2) for this set of records, select the 10 lowest scores (or smaller number if golfer has less than 20 rounds);

3) compute the average round differential for the subset of records, etc. to calculate handicap index (this step is working ok...)

My current VB code has this LINQ query (which is flawed -- it selects the lowest handicap differential scores of ALL records for the filtered user):

[Code]....

How do I modify this query to accomplish items 1) & 2) above? It seems this should be simple, but my experience with queries is still limited.

View 2 Replies

Forms Data Controls :: Query Working In Query Builder But Not In Webpage?

Jul 13, 2010

I'm encountering a strange problem. Here is the scenario. I have built a query that accepts a parameter (WHERE LIKE clause).

I've tested this within the query builder and it returns exactly the number I would expect.

I then go back to my webpage and add the following controls:

Dropdown

Gridview

the user to make a selection from the dropdown. Postback on the dropdown is enabled so the page reloads and the Gridview displays the filtered results (via an objectdatasource). I've set the parameter to the dropdown control.

The trouble is when I run the page I make the selection in the dropdown no results are displayed. Even though the text displayed in the dropdown control is exactly the same as I inputted into the query builder when testing it.

View 3 Replies

ADO.NET :: Can Get A Value Or Null From An Entity Query

Mar 28, 2011

Dim p = From i In context.persons
Order By i.fileNum
Select i.ID, i.fileNum, i.firstName, i.lastName, i.state, i.prPenPals.FirstOrDefault.startProfile, i.prPenPals.FirstOrDefault.endProfile

where for every person, prPenPal can have exactly one or zero records? I can do the above, but when I try to use p as the datasource of a ListView I get The cast to value type 'DateTime' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
I tried using

Select i.inmateID, i.fileNum, i.firstName, i.lastName, i.state, i.prLegals.FirstOrDefault.startProfile.ToString,
i.prLegals.FirstOrDefault.endProfile.ToString

but that didn't work. I got a blue squiggly line "Range variable name cannot match the name of a member of the 'Object' class" error I want the date if the record exists, and Null or Nothing if it dosen't.

View 1 Replies

C# - Rewrite SQL Query Using Linq To Entity

Jul 12, 2010

Hi guys I need to rewrite the sql query below using Linq to entity. Unfortunately I don't have a lot of experience of using Linq.

With TempTableName AS
(SELECT [ColumnName],
[ColumnName2],
[ColumnName3],
[ColumnName4],
ROW_NUMBER() OVER (order by ColumnName desc) as RowNumber from TableName )
SELECT
[ColumnName],
[ColumnName2],
[ColumnName3],
[ColumnName4]
FROM TempTableName WHERE ROWNUMBER
Between 10 and 100

View 1 Replies

ADO.NET :: Entity Framework - Query Based On Relationships?

Oct 27, 2010

I have the following related tables:

Users
Id
Username
Email
Password
UserStatuses
Id
Name
Users_UserStatus
Id
User (FK - Users: Id)
Status (FK - UserStatuses: Id)
StatusDate

As you can see, UserStatuses is a reference table with various statuses. Users_UserStatus is a join table between Users and UserStatuses. Using Entity Framework, how can I do a conditional to check if the latest StatusDate is "Pending"? As of now, I have the following:

[Code]....

View 1 Replies

Avoid Error When No Results From Entity Query

Mar 14, 2011

I have a simple Entity query:

[code]....

I then check whether one of the result values is more recent than six months ago using an if..then like so:

If rhcexists.First.SignatureDate > Date.Today.AddMonths(-6) Then
End If

However, if there are no results returned this will return an error. Is there a way for me to tell it to act as if the date is older than six months if there is no value at all? e.g., could I in pseudo do something like:

If Exists(rhcexists.First.SignatureDate, Date.Today.AddMonths(-8)) > Date.Today.AddMonths(-6) Then
End If

View 2 Replies

Execute LINQ Query Using Properties Of Child Entity Set

Nov 25, 2010

I am having a meltdown over something that seems so simple and yet isn't working. Here's my scenario.

I have an object structure of tEvents, which contains properties of an event like a run of concerts. tEvents in turn contains an entity set of tEventOptions, which include properties like EventDate, Cancelled etc. I would like to query a list of tEvents using properties of the tEventOptions, for example filtering by date.

My pageis using a calendar object to show event dates. So I am trying to find if any tEvents in the List coming back from the database match the date of the day element being rendered in my Calendar control like so (in this snip 'data' is List and results from a db query):

[code]....

Except r, my query result var, is always true no matter what data it receives. I have tried the sub query using other properties of tEventOption and get the same 'true' result each time. I know that the data does not reflect this result so I clearly have a problem with the structure of my query, but for the life of me I cannot find a way to resolve it.

View 1 Replies

Simplify LINQ-To-Entity Query, And Make It Dynamic

Dec 4, 2010

I have this query for getting data through Entity Framework, which works today. Thing is, i want to create the query dynamically, i.e build it up in blocks, but i can't figure out how exactly to do it. I believe if i can somehow save one of the values that i retrieve in the beginning i can do it, but now i retrieve it twice (probably a bit slow as well? Unless the compiler fixes it before quering). Makes sense? Here is the LINQ:

[code]....

I.e based on if various variables have values.

View 1 Replies

Show SQL Trace Of LINQ Query To Entity Framework 3.5

Oct 13, 2010

Best way to show the SQL trace of a LINQ query to Entity Framework 3.5? I am using ASP.net and EF 3.5.

Dim dbo As Web.Portal.RBMEntities = New Web.Portal.RBMEntities
Dim Query = From RoleAllocations In dbo.RoleAllocations Where RoleAllocations.user_id = _ID And RoleAllocations.expire_date > Today Select RoleAllocations

Console write the SQL trace?

View 2 Replies

Entity Query - Combine Into One And Avoid Merging The Results

Nov 6, 2010

I can't seem to figure out how to write this query properly. I've tried various combinations but nothing's worked yet. Below is the relevant portion of my database model: I need to select the products that match a given Category and Group, and that match a given Year, Make, Model, submodel. This I've done below:

ItemList = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1").Include("LookupYearMakeModels") From G In P.Groups Where G.Category = Cat And G.Grp = Group From Y In P.LookupYearMakeModels Where Y.Year = YMM.Year And Y.Make = YMM.Make And Y.Model = YMM.Model And Y.Submodel = YMM.Submodel Select P

I now also have to select products that match the Category, and Group but are Universal (Product.Univeral = True). I'm currently writing two queries, the one above and the one below. I'm merging the results of the two by simply using ItemList.AddRange(ItemList2) ItemList2 = From P In gDataContext.Products.Include("Groups").Include("Groups.Category1") where P.Universal From G In P.Groups Where G.Category = Cat And G.Grp = Group Select P But I want to combine both queries into one and avoid merging the results. How can I do it?

View 2 Replies

C# - Unable To Construct An Entity (complex) Object From A Query

Jun 8, 2010

I am very new to entity, sql, c#, and asp.net so this might be something easily fixed. I am attempting to display all the inactive products stored in my table called products in a datagrid.

var productQuery = from b in solutionContext.Version
where b.Product.Name == search && b.Product.ActiveNumber > b.VersionNumber
select new Product
{
Name = b.Product.Name,
Description = b.Product.Description,
ID = b.ID,
LastNumber = b.Product.LastNumber,
MiddleNumber = b.Product.MiddleNumber,
RSTATE = b.RSTATE,
ActiveNumber = b.Product.ActiveNumber,
LastModified = b.Product.LastModified,
ParentID = b.Product.ParentID,
ProductType = b.Product.ProductType
};
ProductsGrid.DataSource = productQuery;
ProductsGrid.DataBind();

I am getting this error: $exception {"The entity or complex type 'SolutionsModel.Product' cannot be constructed in a LINQ to Entities query."} System.Exception {System.NotSupportedException}

View 1 Replies

ADO.NET :: [Entity Framework] Strange Error Executing A Sql Query

Nov 29, 2010

I have a strange error, I've tried to search for it before writing this post but I wasn't able to find out a solution yet.

I'm executing a query using ExecuteStoreQuery against a MySQL database, but it throws an exception reporting that there's a syntax error in my SQL.

I've tried to copy&paste the sql query into the MySQL tool and it works nicely, giving the correct results.

I've tried to open manually the connection and using CreateCommand to use it the "old way".

My query is a bit long, 4000 chars more or less, could it be the problem?

View 4 Replies

Visual Studio :: Accessing SQL Server Data Through A Query

Aug 1, 2010

I'm having a hard time really grasping this concept. I've done a lot of development with Microsoft Access, so I'm very familiar with the structure of tables, queries, and all that sort of thing. And maybe that's really hindering me with understanding what to do with ASP.NET/VB.NET development.

I have an ASP.NET web application, and I want to pull a UserName and the date that this user was created and last logged on. UserName is in the ASPNETDB's aspnet_Users table, and date created and last logged on is in the membership table. So in database explorer in Visual Web Developer, I opted to create a "New Query". It brought up the UI and I selected these two tables. Linked them with the primary key of UserID. Then I selected the three necessary fields (UserName, date created, and last logon date). Looks good, viewed the data. But there is no way to save this query to programatically reference it. I want to see what user is logged on, and then look through this query to get the other two pieces of data. But there is no way to save/reference this query.

View 6 Replies

DataSource Controls :: Not Able To Run SQL Query Using If Else?

Apr 21, 2010

I am using Querystring "ddl4" to pass on value of a drop down list to the next page where I am using that value in the WHERE clause of a SQL command. I also need to check whether ddl4 is blank or not which I do using If Else in SQL command. I am not able to run the query where the Querystring ddl4 IS NULL

This is the sample link with the Querystring ddl4 carrying a blank value:

[code]....

View 2 Replies

Entity Framework CTP5 - Code First - Nested Query Error

Jan 3, 2011

I have the following classes:

public class Category
{
public int CategoryId { get; set; }
public string Name { get; set; }
}
public partial class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
this.HasKey(c => c.CategoryId);
this.Property(c => c.Name).IsRequired().HasMaxLength(400);
}
}
public class MyObjectContext : DbContext
{
public MyObjectContext(string connectionStringName)
: base(connectionStringName)
{
}
public DbSet<Category> Categories { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CategoryMap());
base.OnModelCreating(modelBuilder);
}
}

Now when I run the following code I get an exception (GenericArguments[0], 'System.Int32', on 'System.Data.Entity.Internal.Linq.ReplacementDbQueryWrapper`1[TEntity]' violates the constraint of type 'TEntity')

DbDatabase.SetInitializer<MyObjectContext>(new DropCreateDatabaseIfModelChanges<MyObjectContext>());
using (var context = new MyObjectContext("NopSqlConnection"))
{
var query1 = from c in context.Categories
select c.CategoryId;
var test1 = query1.ToList(); //works fine
var query2 = from c in context.Categories
where query1.Contains(c.CategoryId)
orderby c.Name descending
select c;
var test2 = query2.ToList(); //throws the exception
}

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved