Abhishek gupta
Abhishek gupta

Reputation: 463

query regarding DatasourceId and DataSource

two tables are there

EmpInf
    EmpId,
    EmpName,
    Salary,
    DepartNumber.

Dept
    DeptNo,
    Deptname,

i have a linqdatasource control such as

<asp:LinqDataSource ID="LinqDataSource1" runat="server" EntityTypeName="" 
    ContextTypeName="FilterControl.DataClasses1DataContext" EnableDelete="True" 
    EnableInsert="True" EnableUpdate="True" OrderBy="DeptName"  
    TableName="Dept1s" Select="new (DeptNo, DeptName)">
</asp:LinqDataSource>

a listview

<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1"
   style="z-index: 1; left: 10px; top: 34px; position: absolute; height: 63px; width: 293px" 
    >
    <AlternatingItemTemplate>
        <tr style="">
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="ReadOnly" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="ReadOnly" />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="UpdateButton" runat="server" CommandName="Update" 
                    Text="Update" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Cancel" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="Edit" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="Edit" />
            </td>
        </tr>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" style="">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <InsertItemTemplate>
        <tr style="">
            <td>
                <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" 
                    ValidationGroup="Insert" />
                <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                    Text="Clear" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="Insert" 
                    ValidationGroup="Insert" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="Insert" 
                    ValidationGroup="Insert" />
            </td>
        </tr>
    </InsertItemTemplate>
    <ItemTemplate>
        <tr style="">
            <td>
                <asp:DynamicControl runat="server" DataField="DeptNo" Mode="ReadOnly" />
            </td>
            <td>
                <asp:DynamicControl runat="server" DataField="DeptName" Mode="ReadOnly" />
            </td>
        </tr>
    </ItemTemplate>

and tried to implement this listview thorugh dynamic data and used a dynamic data manager control

<asp:DynamicDataManager ID="DynamicDataManager1" runat="server" >
<DataControls>
<asp:DataControlReference ControlID="ListView1" />
</DataControls>
</asp:DynamicDataManager>

i have taken a dropdownlist, it is bound to EmpInf.Empname and when i select the partcular Employee name from a dropdownlist i want to bind the The department name.

at the back end

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.DeptName;



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



        }

during a runtime, it throws an exception,it shows either use DataSourceID="LinqDataSource1" or use the datasource a the back end. remove one of them. while doing so nothing is returned during the process. how shall i make this run, need suggestion. thanks for assistance.

Upvotes: 0

Views: 350

Answers (2)

Abhishek gupta
Abhishek gupta

Reputation: 463

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.DatasourceId=null;
        ListView1.DataSource = a;
        ListView1.DataBind();



    }

It can also be solve by writing listview.DatasourceId=null in above code

Upvotes: 0

codingbiz
codingbiz

Reputation: 26396

One way to solve that is remove the DataSourceID from the ListView control and bind the data in page_load

protected void Page_Load(object sender, EventArgs e)
{

   if(!Page.IsPostBack)
   {
      //Bind datasource to ListView here
   }
}

everything should work now

Upvotes: 1

Related Questions