Appdev
Appdev

Reputation: 299

How to use if condition inside Linq

I'm Developing an application which is used for Search and Bind the DataGrid. What the user will do is enter the part of the text and hit search button means it should Fetch the data from DB which have like textbox value and bind to datagrid.

But my requirement is:- In my table i have 3 fields namely ID,ChargeName and IorE.and in my datagrid i have columns like id ,Income and Expences. What i need is for eg: if i enter the charge name in textbox like "Service" and hit search means if it should search for the name in the chargeName field once it found if should check in IorE field if value is "I" then the ChargeName should bind to Income Column in Datagrid and if it is "E" then ChargeName should bind to Expence Column in Datagrid. I'm Using LinQ and i dont know how to use such condition in LINQ

Here is my code goes.

using (LQMasterChargeDataContext DB = new LQMasterChargeDataContext())
{
    var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.mCHR_VCName
                            };

    dgSailing.DataSource = Result;
    dgSailing.DataBind();
    int Count = Result.Count();
    if (Count == 0)
    {
        LBLEXcepceValue.BackColor = System.Drawing.Color.LightPink;
        LBLEXcepceValue.Font.Bold = true;
        LBLEXcepceValue.Font.Name = "Times New Roman";
        LBLEXcepceValue.Font.Size = 10;
        LBLEXcepceValue.Text = "NO VESSEL FOUND ! ! !";
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "ALERT", "alert('No Vessel found');", true);
    }

and my aspx page is like:

<asp:UpdatePanel ID="UPChargesGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:DataGrid ID="dgDestination" runat="server" BorderWidth="1px" BorderColor="#FE9B00"
                          BorderStyle="Solid" BackColor="White" Font-Names="Verdana" Font-Size="XX-Small"
                          AutoGenerateColumns="False" ShowFooter="FALSE" CellPadding="3" align="center"
                           Width="700px" OnItemCommand="dgDestination_Select">
            <Columns>
                <asp:BoundColumn DataField="ID" HeaderText="mFDD_mFRD_NUPKId" Visible="False">
                </asp:BoundColumn>
                <asp:BoundColumn DataField="Income" HeaderText="Income"></asp:BoundColumn>
                    <TemplateColumn>
                        <ItemTemplate>
                            <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence").ToString().Substring(0,5) %>' ></asp:Label>
                         </ItemTemplate>
                     </TemplateColumn>
                     <asp:TemplateColumn HeaderText="SAVE">
                         <ItemTemplate>
                             <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
                                   AlternateText="Save" />
                         </ItemTemplate>
                     </asp:TemplateColumn>
                </Columns>
        <PagerStyle HorizontalAlign="Left" ForeColor="#000066" BackColor="White" Mode="NumericPages"></PagerStyle>
      </asp:DataGrid>
    </contenttemplate>
</asp:UpdatePanel>    

Can any on help me to solve this issue.Thanks in advance.

I bounded the DataGrid now what i want is if the value is null then one user control should display and save button(in datagrid) should be Enable.Please Help me to do that.Here is my need represent via image enter image description here

Result

enter image description here

Upvotes: 1

Views: 1371

Answers (1)

Kiran Hegde
Kiran Hegde

Reputation: 3681

You can change the query like this

var Result = from C in DB.TB_MasterCharges
                 where C.mCHR_NUIsActive == 1 && SqlMethods.Like( C.mCHR_VCName,TxtChargeName.Text.Trim())
                 select new { C.mCHR_NUPKId,
                              Income = C.IorE == "I" ? C.mCHR_VCName : string.Empty,
                              Expense = C.IorE == "E" ? C.mCHR_VCName : string.Empty
                            };

Then you can use a bound column for expense like below

<asp:BoundColumn DataField="Expense" HeaderText="Expense"></asp:BoundColumn>

UPDATE I hope I understood your problem correctly. You can toggle the visibility of controls using the code like below

<TemplateColumn>
   <ItemTemplate>
       <asp:Label ID="LBLEXcepceValue" runat="server" Text='<%# Eval("Expence") %>' 
       Visible='<%# Eval("Expence") != null && Eval("Expence") != string.Empty %>' 
       ></asp:Label>
       <asp:TextBox ID="textBoxExpenses" runat="server" Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>'></asp:TextBox>
   </ItemTemplate>
</TemplateColumn>
<asp:TemplateColumn HeaderText="SAVE">
   <ItemTemplate>
      <asp:ImageButton runat="server" ID="IMGBTNSave" ImageUrl="~/AppImages/Save.gif" ToolTip="Save"  CommandName="Save" 
      Visible='<%# Eval("Expence") == null || Eval("Expence") == string.Empty %>
      AlternateText="Save" />
   </ItemTemplate>
</asp:TemplateColumn>

Upvotes: 1

Related Questions