Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i have drop down in the form,and an event is fire when any value is selected.
in the database have a table there are two column one is link with drop down against each value of this there are multiple value in the 2nd column, how can i count and display these value in the textboxes of the form.
ASP.NET
<div class="form-group">
           <label class="col-sm-2 control-label">
                   Select Sector:
               </label>
               <div class="col-sm-3">
                  <asp:DropDownList CssClass="form-control" runat="server" MaxLength="7" ID="ddlToSector" OnSelectedIndexChanged="ddlToSector_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
                    </div>
           <label class="col-sm-2 control-label">
               Strenght:
           </label>
           <div class="col-sm-1">
               <asp:TextBox runat="server" ID="txtStrenght" AutoPostBack="true" ></asp:TextBox>
           </div>
       </div>

       <div class="form-group">
           <label class="col-sm-2 control-label">
               OnLeave:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtOnLeave" AutoPostBack="true" ></asp:TextBox>
           </div>
           <label class="col-sm-2 control-label">
               Absent:
           </label>
           <div class="col-sm-3">
               <asp:TextBox runat="server" ID="txtAbsent" AutoPostBack="true"></asp:TextBox>
           </div>
       </div>

c#

C#
protected void ddlToSector_SelectedIndexChanged(object sender, EventArgs e)
{
    var sectorStrength = _service.GetAllEmployeeDuty().Where(x => 
        x.ToSector_Id == SafeConvert.ToInt32(ddlToSector.SelectedValue));
    txtStrenght.Text = sectorStrength.ToString();

    var sectorAbsent = _service.GetAllEmployeeAbsent().Where(x => 
        x.Sector_Id ==SafeConvert.ToInt32(ddlToSector.SelectedValue)).Count();
    txtAbsent.Text = sectorAbsent.ToString();

    var empStatus = _service.GetAllEmployeeStatus().Where(x => 
        x.Rank_Id == SafeConvert.ToByte(ddlToSector.SelectedValue)).Count();
    txtSpecialDuty.Text = empStatus.ToString();

First sectorStrenght work fine ,it return exact value from the table
problem is in EmployeeStatus table in which i have different value against same Sector Id,
i want that when any sector is selected query will check in this table againt Sector ID DIFFERENT status of employee then count it and return in to text box
SQL
Here is EmployeeStatusType table.
USE [CTPHR]
GO

/****** Object:  Table [dbo].[EmployeeStatusType_Id]    Script Date: 11/21/2014 10:03:15 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EmployeeStatusType_Id](
	[Id] [tinyint] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](25) NOT NULL,
	[IsDutable] [bit] NOT NULL,
 CONSTRAINT [PK_EmployeeStatusType_Id] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

this is 2nt table against sector id there are different employeestatustype ,this sector id is used to fire an event .
USE [CTPHR]
GO

/****** Object:  Table [dbo].[EmployeeStatus]    Script Date: 11/21/2014 10:06:35 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[EmployeeStatus](
	[Id] [tinyint] IDENTITY(1,1) NOT NULL,
	[Employee_Id] [varchar](8) NOT NULL,
	[EmpstatusType_Id] [tinyint] NOT NULL,
	[StartDate] [date] NOT NULL,
	[EndDate] [date] NOT NULL,
	[Sector_Id] [smallint] NOT NULL,
 CONSTRAINT [PK_Mp_status] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[EmployeeStatus]  WITH CHECK ADD  CONSTRAINT [FK_EmployeeStatus_EmployeeStatus] FOREIGN KEY([Id])
REFERENCES [dbo].[EmployeeStatus] ([Id])
GO

ALTER TABLE [dbo].[EmployeeStatus] CHECK CONSTRAINT [FK_EmployeeStatus_EmployeeStatus]
GO


[Id]  [Employee_Id] [EmpstatusType_Id][StartDate][EndDate][Sector_Id]
1           333        1                     xxx   xxx        1
2           ddd        1                     xxx   xxx        1
3           www        2                     xxx   xxx        1
4           fff        4                     xxx   xxx        1
5           ddd        3                     xxx   xxx        1

this is the format of database records.there are different value against same sector. how can calculate EmpstatusType_Id ,how can i calculate empstatusType and show them in text box.as explain above
Posted
Updated 20-Nov-14 20:28pm
v5
Comments
Shweta N Mishra 20-Nov-14 14:10pm    
Put some sample records of your table and show what you want to display in your textbox, write the code which you have tried so far
Sajid227 21-Nov-14 1:06am    
like against one sector there are some employee who may be absent,on leave or on medical, i just want show their no in the text box.
/\jmot 20-Nov-14 14:22pm    
is single row-column contains multiple values or multiple row-column containing multiple values??
i mean the values are in s single row or in multiple row??
Maciej Los 20-Nov-14 16:36pm    
We need more details, ex.: sample data.
Sajid227 21-Nov-14 1:06am    
like against one sector there are some employee who may be absent,on leave or on medical, i just want show their no in the text box.

1 solution

What i have understand that you want to count the distinct EmpstatusType_Id against each sector.

Here you go

SQL
select 
     count(distinct EmpstatusType_Id ) 
from EmployeeStatus 
where Sector_Id=@SectorId
 
Share this answer
 
Comments
Sajid227 22-Nov-14 23:44pm    
var empStatus = _service.GetAllEmployeeStatus().Where( X => X.Sector_Id == select count(distinct EmpstatusType_Id ) from EmployeeStatus where Sector_Id=@SectorId);
txtSpecialDuty.Text = empStatus.ToString(); THAT query is not working
how can i get it to work

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