Pages

Showing posts with label web service. Show all posts
Showing posts with label web service. Show all posts

Saturday, November 30, 2013

How to bind LINQ data to dropdownlist (TEXT,VALUE)


Let's assume we have a Web Service which will return the results as an
object with the values DDLTEXT
and
DDLVALUE.
The following will explain how to use LINQ to query and then bind the results to the

DropDownList:
  DataTextField and  DataValueField

ddl.DataSource = wsMethod.Contents.OrderBy(o=>o.DDLTEXT);

//specify result object values
ddl.DataTextField = "DDLTEXT";
ddl.DataValueField = "DDLVALUE";

ddl.DataBind();


How to bind LINQ data to DataGrid

Let's assume we have a Web Service which will return the results as an object for IN and OUT data records.
The following will explain how to use LINQ to query and then bind the results to the asp:DataGrid


C# code:
//query to get currunt month data ordered by LOCAL_TIME then TYPE
 var thisMonthData = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.OrderByDescending(o2 => o2.TYPE)
.Where(m => m.DATE.Substring(0, 6)
== DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eDataGrid.DataSource = thisMonthData;
eDataGrid.DataBind();
 
//query to get currunt month data ordered by LOCAL_TIME for TYPE=IN
var thisMonthDataIn = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.Where(r => r.TYPE == "IN")
.Where(m => m.DATE.Substring(0, 6) == DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eINDataGrid.DataSource = thisMonthDataIn;
eINDataGrid.DataBind();
 
//query to get currunt month data ordered by LOCAL_TIME for TYPE=OUT
var thisMonthDataOut = eData.Contents
.OrderByDescending(o => o.LOCAL_TIME)
.Where(r => r.TYPE == "OUT")
.Where(m => m.DATE.Substring(0, 6) == DateTime.Today.ToString("yyyyMM", arCulture));
 
//Bind Data
eOUTDataGrid.DataSource = thisMonthDataOut;
eOUTDataGrid.DataBind();

 

Markup:


                     
<asp:DataGrid CssClass="gvTbl" ID="eINDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>
 
<asp:DataGrid CssClass="gvTbl" ID="eOUTDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>
<asp:DataGrid CssClass="gvTbl" ID="eDataGrid" runat="server"  AutoGenerateColumns="true">
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle BackColor="#DAF1F2" HorizontalAlign="Right" />
<AlternatingItemStyle BackColor="#ECE9EA" />
<Columns>
</Columns>
</asp:DataGrid>