Click here to Skip to main content
15,886,036 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to get records from database using a text box value contains multiple comma separated resources like (DEO,DPO,PROGRAMMER) based on this each resource type i need to bind data from database to grid view

i tried this query
C#
var Obj_SalaryAndTaxInfo = (from p in t.Tbl_Designations where words.Contains(p.ResourceType) select p).FirstOrDefault();


but in this case i got only one record

What I have tried:

C#
protected void RetriveInvoice_Click(object sender, EventArgs e)
{
    List<getsal> getsalaries = new List<getsal>();

    try
    {
        //int sequenceNumber = 5;
        //string id = string.Format("PT{0}", sequenceNumber.ToString().PadLeft(13, '0'));

        var Otsi_orders = (from p in t.Tbl_OtsiCreatedWorkOrders where p.WorkOrderNO == ddlWorkOrder.SelectedValue select p).FirstOrDefault();

        GetInvoiceOnMonth_Result Invoice_Month = t.GetInvoiceOnMonth(ddlWorkOrder.SelectedValue, DLMonth.Text, DLYear.Text).FirstOrDefault();
        if (Otsi_orders != null && Invoice_Month != null)
        {
            decimal SalaryPerDay=0;
            BtnPrintSalaryList.Visible = true;
            //code Added
            var resourceType = t.Tbl_HMWSSBWorkOrders.Select(a =&gt; a).Where(a =&gt; a.WorkOrderNO == ddlWorkOrder.SelectedValue).FirstOrDefault();
            var s = resourceType.Resource_Type;

            char[] delimiterChars = { ',' };
            string NewString = s;
            string strresource = NewString.TrimEnd(delimiterChars);

            string[] words = strresource.Split(delimiterChars);

            Tbl_Designations Obj_SalaryAndTaxInfo = new Tbl_Designations();
           // Obj_SalaryAndTaxInfo = (from p in t.Tbl_Designations where words.Contains(p.ResourceType) select p).FirstOrDefault();
            foreach (string item in words)
            {
                Obj_SalaryAndTaxInfo = t.Tbl_Designations.Select(x =&gt; x).Where(x =&gt; x.ResourceType == item).FirstOrDefault();
                ////Obj_SalaryAndTaxInfo = (from p in t.Tbl_Designations where words.Contains(p.ResourceType) select p).Single();
                 lblDesignationonpageload.Visible = true;
                Decimal SalaryPerresource = Convert.ToDecimal(Obj_SalaryAndTaxInfo.SalaryPerResource);
                SalaryPerDay = (SalaryPerresource / (Convert.ToDecimal(Invoice_Month.NumberofDaysInMonth)));

                Decimal finalPay = Convert.ToDecimal(Invoice_Month.Total_Days_Worked * SalaryPerDay);

                txtNumberofResources.Text = Otsi_orders.NumberOfrequiredResources.ToString();
                txtProvidedResources.Text = Otsi_orders.ProvidedResources.ToString();

                TxtSalary.Text = Obj_SalaryAndTaxInfo.SalaryPerResource.ToString();

                LblWorkedDays.Text = Invoice_Month.Total_Days_Worked.ToString();
                LblFinalPay.Text = Math.Round(Convert.ToDecimal(finalPay), 0).ToString();
                lblDesignation.Text = "Work Order For " + item;

                Random UniqueNumber = new Random();

                //***Taxes forinvoice calculattion

                // TxtInvoiceNo.Text = "OTSIPL/HMWSSB/" + DLMonth.SelectedValue + "-" + DLYear.SelectedValue + "/" + Convert.ToInt32(UniqueNumber.Next());

                TxtPanno.Text = Obj_SalaryAndTaxInfo.PANNO;
                TxtServiceTaxNo.Text = Obj_SalaryAndTaxInfo.ServiceTAXNO;
                txtEmployershareEPF.Text = Obj_SalaryAndTaxInfo.EPF;
                txtEmployershareESI.Text = Obj_SalaryAndTaxInfo.ESI;
                txtAgencyCommission.Text = Obj_SalaryAndTaxInfo.AgencyComission;
                txtServiceTAX.Text = Obj_SalaryAndTaxInfo.ServiceTax;
                TxtSbc.Text = Obj_SalaryAndTaxInfo.SBC;

                //** Taxes for print **//

                txtpono.InnerText = ddlWorkOrder.SelectedValue;

                txtprojectname.InnerText = Otsi_orders.Resources_Type;
                WoYear.InnerText = DateTime.Now.ToString("yy") + "-" + DateTime.Now.AddYears(1).ToString("yy");
                wono.InnerText = ddlWorkOrder.SelectedValue;
                DesignationType.InnerText = item;
                getsalaries = GetEmployeMotnhlySalary(SalaryPerDay,item);
            }

            Session["PerdaySal"] = SalaryPerDay;
            InvoiceTable.Visible = true;
            BtnGenerateInvoice.Visible = true;
            ddlWorkOrder.Enabled = false;
            DLMonth.Enabled = false;
            DLYear.Enabled = false;
            RetriveInvoice.Visible = false;
            BtnModifySearch.Visible = true;
            InVoiceTemplete.Visible = true;
            InvoiceGridView1.DataSource = getsalaries;
            InvoiceGridView1.DataBind();
            InvoiceGridView1.HeaderRow.TableSection = TableRowSection.TableHeader;
            InvoiceGridView1.Visible = true;

          // var Obj_SalaryAndTaxInfo = t.Tbl_Designations.Where(p =&gt; words.Contains(p.ResourceType));
           // string strResourceType = Obj_SalaryAndTaxInfo.ResourceType

            // lblDesignationonpageload.Visible = true;
            //Decimal SalaryPerresource = Convert.ToDecimal(Obj_SalaryAndTaxInfo.SalaryPerResource);
            //var SalaryPerDay = (SalaryPerresource / (Convert.ToDecimal(Invoice_Month.NumberofDaysInMonth)));

            //Decimal finalPay = Convert.ToDecimal(Invoice_Month.Total_Days_Worked * SalaryPerDay);

            //txtNumberofResources.Text = Otsi_orders.NumberOfrequiredResources.ToString();
            //txtProvidedResources.Text = Otsi_orders.ProvidedResources.ToString();

            //TxtSalary.Text = Obj_SalaryAndTaxInfo.SalaryPerResource.ToString();

            //LblWorkedDays.Text = Invoice_Month.Total_Days_Worked.ToString();
            //LblFinalPay.Text = Math.Round(Convert.ToDecimal(finalPay), 0).ToString();
            //lblDesignation.Text = "Work Order For " + Otsi_orders.Resources_Type;

            //Random UniqueNumber = new Random();

            ////***Taxes forinvoice calculattion

            //// TxtInvoiceNo.Text = "OTSIPL/HMWSSB/" + DLMonth.SelectedValue + "-" + DLYear.SelectedValue + "/" + Convert.ToInt32(UniqueNumber.Next());

            //TxtPanno.Text = Obj_SalaryAndTaxInfo.PANNO;
            //TxtServiceTaxNo.Text = Obj_SalaryAndTaxInfo.ServiceTAXNO;
            //txtEmployershareEPF.Text = Obj_SalaryAndTaxInfo.EPF;
            //txtEmployershareESI.Text = Obj_SalaryAndTaxInfo.ESI;
            //txtAgencyCommission.Text = Obj_SalaryAndTaxInfo.AgencyComission;
            //txtServiceTAX.Text = Obj_SalaryAndTaxInfo.ServiceTax;
            //TxtSbc.Text = Obj_SalaryAndTaxInfo.SBC;

            ////** Taxes for print **//

            //txtpono.InnerText = ddlWorkOrder.SelectedValue;

            //txtprojectname.InnerText = Otsi_orders.Resources_Type;
            //WoYear.InnerText = DateTime.Now.ToString("yy") + "-" + DateTime.Now.AddYears(1).ToString("yy");
            //wono.InnerText = ddlWorkOrder.SelectedValue;
            //DesignationType.InnerText = Otsi_orders.Resources_Type;

            //GetEmployeMotnhlySalary(SalaryPerDay);
            //Session["PerdaySal"] = SalaryPerDay;
            //InvoiceTable.Visible = true;
            //BtnGenerateInvoice.Visible = true;
            //ddlWorkOrder.Enabled = false;
            //DLMonth.Enabled = false;
            //DLYear.Enabled = false;
            //RetriveInvoice.Visible = false;
            //BtnModifySearch.Visible = true;
            //InVoiceTemplete.Visible = true;
        }
        else
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "", "&lt;script&gt;alert('Timesheet is not Available for this work order on selected month & year')&lt;/script&gt;");
            txtEmployershareEPF.Text = " ";
            txtEmployershareESI.Text = " ";
            txtAgencyCommission.Text = "";
            txtServiceTAX.Text = " ";
            txtNumberofResources.Text = "";
            txtProvidedResources.Text = "";
            TxtSalary.Text = "";
            LblFinalPay.Text = "";
            LblWorkedDays.Text = "";
        }
    }
    catch (Exception ex)
    {
    }
}
Posted
Updated 20-May-16 7:11am
v11

1 solution

The .FirstOrDefault() is designed to give exactly one result!
Either the first successful record/value that meets the condition or the default for that type of record/value: that's usually null or zero.

(By the way: There's plenty of other things wrong with this code. Some are just inefficiencies, others... )
 
Share this answer
 
v2
Comments
Member 123CC 19-May-16 2:16am    
ok ,then what method can i use for this solution
Matt T Heffron 19-May-16 2:36am    
Get rid of the .FirstOrDefault()
That means that Obj_SalaryAndTaxInfo will not be a single instance of that type, it will be an IEnumerable<theType>
So you'll need to foreach over Obj_SalaryAndTaxInfo and do the appropriate steps with each of the instances in the enumerable.
Only you can determine what the "appropriate steps" are. You have your requirements, we don't.
Member 123CC 19-May-16 3:00am    
ya tried that also,but I don't have an idea how to declare this Obj_SalaryAndTaxInfo,besed on this variable only we retrieve data so
Matt T Heffron 19-May-16 13:59pm    
So, if there are multiple records that match the words, how do you want to display them? I don't see anything in your code that looks like a grid type display.
Matt T Heffron 20-May-16 13:13pm    
OK, I see you updated the "What I have tried" code...
(I fixed the formatting...)
You haven't said what's wrong (or right) with that new code!

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