Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used a gridview and linqdatasourse , the gridview will be fill by a linq-stored procedure after clicking a search button.



I did it like below but my grid view is empty.

It seems LinqDataSource2_Selecting does not work.

Please help what is the problem?

C#
public void LinqDataSource2_Selecting(object sender, LinqDataSourceSelectEventArgs e)
{
    _DataContext = new EDMSDataContext();
       
     var subjectFilter = e.WhereParameters["Subject"];
    
    

    var query = _DataContext.spQuickSearchDoc(subjectFilter);


    e.Result = query;
}


C#
protected void btnSearch_Click(object sender, EventArgs e)
    {
        _DataContext = new EDMSDataContext();
        this.LinqDataSource2.WhereParameters["Subject"].DefaultValue = this.txtSearchKeywords.Text;

        GridViewDocuments.Visible = false;
        GridViewDocuments_Search.Visible = true;
        this.GridViewDocuments_Search.DataBind();
        
    }


C#
Stored procedure:

ALTER PROCEDURE [dbo].[spQuickSearchDoc]
@Searchtext varchar(50)=null

AS

select DocId,DocumentNo,Title,Unit
from tblDocuments
where DocumentNo like '%'+@SearchText + '%'
or Title like '%'+@SearchText + '%'
or Unit like '%'+@SearchText + '%'


ASP.NET
<pre>Gridview:

<asp:GridView ID="GridViewDocuments_Search" runat="server" AutoGenerateColumns=False Visible="False" onrowcommand="GridViewDocuments_Search_RowCommand"  
      DataKeyNames="DocID" PageSize="100"  >
        <Columns>
        <asp:TemplateField HeaderText = "Details">
               <ItemTemplate>
          <asp:Button ID ="btn_Show" Text="Details" runat= "server" CommandName= "Details" CommandArgument='<%#
            Container.DataItemIndex%>' />
            </ItemTemplate>
                </asp:TemplateField>
        
           <asp:BoundField DataField="DocumentNo" HeaderText="DocumentNo" SortExpression="DocumentNo" />
            <asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
            
            <asp:BoundField DataField="Docid" HeaderText="Docid" Visible="false" />
            <asp:CommandField ShowEditButton="True" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
    </asp:GridView>

<asp:LinqDataSource ID="LinqDataSource2" runat="server" 
              ContextTypeName="EDMSDataContext" OnSelecting="LinqDataSource2_Selecting">
              <WhereParameters>
        <asp:ControlParameter Name="Subject"
                              ControlID="txtSearchKeywords"
                              PropertyName="Text"
                              Type="String" />
    </WhereParameters>
          </asp:LinqDataSource
>
Posted
Updated 18-Dec-12 4:35am
v2
Comments
Yaseer Arafat 18-Dec-12 17:19pm    
See this http://www.codeproject.com/Articles/26825/Using-LinqDataSource-with-ASP-NET-data-controls-Pa

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900