hi there,
i have a question in LINQ in C#,
i have a webpage and in that i have a button called clicl and theGridview . i have a LINQ to SQL class with the Employee and the USErLogin which the EID in the Employee calss is a foreign key in the USerlogin class,
i doubled click on the button and wrote a simple query to get the values from the Employee table
below is the code

protected void Button1_Click(object sender, EventArgs e)
    {
       var query = from e2 in po.Employees
                    select e2;

        GridView1.DataBind();
        GridView1.DataSource = query;
    }

but when i click the buttonnothis appears or no error , but the mdf file that i am using has values, why is this happening??

please give me a solution
appreciate a lot,
also i am not sure if i am in the right forum,
thanxx
appreciate a lot

Dani AI

Generated

Two separate issues look likely from the thread: binding order and how the Employee/UserLogin relationship is accessed.

As pointed out, the GridView must have its DataSource set before calling DataBind. With LINQ-to-SQL it helps to materialize the query first so any runtime errors surface immediately and the binding is deterministic:

var results = query.ToList();   // force query execution
GridView1.DataSource = results;
GridView1.DataBind();

For the relationship: the designer usually generates a collection navigation property on the principal side (plural, e.g., UserLogins), so employee.UserLogins is a collection — not a single object with fields. To filter by a child row use either a join or test the child collection with Any. Two common patterns:

var q = from e in dc.Employees
        join u in dc.UserLogins on e.EID equals u.EID
        where u.Username == uname && u.Password == pass
        select e;

or

var q = dc.Employees
          .Where(e => e.UserLogins.Any(u => u.Username == uname && u.Password == pass))
          .Select(e => new { e.Name });

Always call ToList() (or otherwise materialize) before binding.

Other practical checks: open the .dbml designer or the generated .designer.cs to confirm the exact association/property names and types (case and pluralization matter). If rows still seem missing, verify the actual MDF file the app is using — Visual Studio often copies the database into the output folder; check the connection string and the file's "Copy to Output Directory" setting, or open the database via Server Explorer to confirm the physical path. Finally, avoid storing plaintext passwords — switch to salted hashes for any real application.

Summary: set DataSource then DataBind (materialize first), use join or Any to query child collections, confirm the dbml mapping and the real MDF file.

Recommended Answers

All 5 Replies

I guess the order should be

protected void Button1_Click(object sender, EventArgs e)
{
    var query = from e2 in po.Employees
                select e2;

    GridView1.DataSource = query; // this should come first.
    GridView1.DataBind();
}

I would like to know if that worked, I am kinda rough around edges when it comes to GridViews :P

I guess the order should be

protected void Button1_Click(object sender, EventArgs e)
{
    var query = from e2 in po.Employees
                select e2;

    GridView1.DataSource = query; // this should come first.
    GridView1.DataBind();
}

I would like to know if that worked, I am kinda rough around edges when it comes to GridViews :P

hey thankxxx
and also my table structure is as below

create table Employee(
EID int primary key,
Name string,
);
create table UserLogin(
ID int primary key,
Username string,
Password string,
EID int foreign key
);

but when i write the below query

var a = from b in po.GetTable<Employee>()
                where [B]b.Userlogins.EID[/B] == b.EID && b.Userlogins.Username == "krishni" && b.Userlogins.Password == "123"
                select b;

       
        GridView1.DataSource = a;
        GridView1.DataBind();

it says the b.Userlogins.EID hasn't EID wher it is bold and the Userlogin parameters does not come as drop down,

why is this??

please can you guide me with this
thanxxxxx

The table name is "UserLogin" or "UserLogins"? or is it the way it has been mapped in DataContext?

The table name is "UserLogin" or "UserLogins"? or is it the way it has been mapped in DataContext?

table name is UserLogin but the visual studio gives the UserLogins as a drop down???

why is that

table name is UserLogin but the visual studio gives the UserLogins as a drop down???

why is that

hey bu the way
is the below code corrent
i tried it in another way

string uname = Login1.UserName;
        string pass = Login1.Password;

        var query = from e1 in po.GetTable<Employee>()
                    from u in po.GetTable<Userlogin>()
                    where (u.Username == uname && u.Password == pass)
                    select e1.Name;

        GridView1.DataSource = query;
        GridView1.DataBind();

thanxxx
appriciate a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.