1

I have two tables

EmpInf
    EmpId,
    EmpName,
    Salary,
    DepartNumber.

Dept
    DeptNo,
    Deptname,

I also have a listview1 and dropdownlist1 which is bound to EmpInf.EmpName

While passing a particular query

FilterControl.DataClasses1DataContext obj = new DataClasses1DataContext();
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
    var a = from r in obj.EmpInfs join s in obj.Dept1s on r.DeptNumber equals s.DeptNo where r.EmpName == "'" + DropDownList1.SelectedValue + "'" select s;

    ListView1.DataSource = a;
    ListView1.DataBind();   
}

Whenever I select a particular name from a dropdownlist, it returns No data was returned. What particular code am I missing or is there any other error?

3
  • Where is it showing No data was returned? In the page or is it an error you're getting? Commented Jul 3, 2012 at 12:35
  • OK, but where is this message shown? In the page or is it an error? Commented Jul 3, 2012 at 12:39
  • at the page sir, while i am selecting the value from the dropdownlist, instead of returning the value, it is returning NO Data Was Returned. i have tried the same query in the sqlserver, it runs there. Commented Jul 3, 2012 at 12:46

2 Answers 2

1

Put a breakpoint in SelectedIndexChanged1 and look at the value of SelectedValue to make sure DropDownList1.SelectedValue has the Employee name. You can also try DropDownList1.SelectedText.

string selected = DropDownList1.SelectedValue.ToString();

// e = employee | d = department
var query =
            from   e in obj.EmpInfs
            join   d in obj.Dept1s on e.DeptNumber equals d.DeptNo
            where  e.EmpName == "'" + DropDownList1.SelectedValue + "'"
            select d;

Change this line:

where  e.EmpName == "'" + DropDownList1.SelectedValue + "'"

to this one:

where  e.EmpName == selected

OK, my last attempt here... Do this before databinding:

ListView1.DataSource = query.ToList();
Sign up to request clarification or add additional context in comments.

9 Comments

Sir i want to select the department name as DeptName while selecting the particular Employee Name as EmpInf from the dropdownlist.
OK... sorry for the confusion. Will adapt the code accordingly.
sir, already checked that part of breakpoint, but no help, error continue to persist.
yes sir, i have bound the EmpInf.EmpName with the dropdownlist.
@ Leniel Macaferi. thanks sir, that particular problem is solved. but as i have used linqdatasource control and provided the controlname to the Listview Id as DataSourceID="LinqDataSource1" and on a same time i am trying to use ListView1.DataSource = a; ListView1.DataBind(); . so at a runtime it throws an exception to use either datasource or datasourceID remove either of them?
|
0

Most likely the problem here is with your addition of the single quotes on the search criteria in your Where clause:

var a = from r in obj.EmpInfs 
        join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
        where r.EmpName == "'" + DropDownList1.SelectedValue + "'" 
        select s; 

Assuming DropDownList1.SelectedValue is "Smith" then your generated sql will be along the lines of:

SELECT *
FROM <tables>
WHERE EmpName = ''Smith''

Notice the double single quotes. To double check this, put a breakpoint after your query is generated and then call .ToString() on it to get the equivalent TSQL. To fix this, remove the "'" from your LINQ query as that will be automatically added for string parameters:

var a = from r in obj.EmpInfs 
        join s in obj.Dept1s on r.DeptNumber equals s.DeptNo 
        where r.EmpName == DropDownList1.SelectedValue 
        select s;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.