Tuesday, July 16, 2013

Checkbox selection check and loop through the checkboxlist ,asp.net /c#

 //.aspx code

 <asp:CheckBoxList runat="server"  RepeatColumns="0" Height="250" Width="150"  RepeatDirection="Vertical"
                                                   CssClass="btext" ID="chkLstSkill">
                                               </asp:CheckBoxList>

//.cs code
 string values = "";
if (chkLstSkill.SelectedValue = = "")
                {
       //do something if not selected          
}
else
{

  // We perform a for loop to check if each checkbox is selected then we get the value
                    foreach (ListItem objItem in chkLstSkill.Items)
                    {
                        if (objItem.Selected)
                        {
                            values += objItem.Value;
                            TrainingID = values;
                        
             //Use below if u want only one id at a time else dont use it
              //       values = "";
                //    TrainingID = ""; //
                  
                        }

}

Steps to create Horizontal or pivoted table through rdlc report/asp.net/C# and if suppose selection is dynamic


 //I had a task where i need to display Zone,Areaname,DealerCode,DealerName,EmployeeCode,EmployeeName,Role and tricking part whatever course user selects it should only show that course and its course schedule time.
//For course selection i used Checkbox list

//Codebehind i put a condition for Checkbox selection

 if (chkLstSkill.SelectedValue == "")
                {
}
else
{
ReportViewerDealerStatus.Visible = true;                                                     //Reports Div is Enabled True
                    ReportViewerDealerStatus.Reset();
                    ReportViewerDealerStatus.ProcessingMode = ProcessingMode.Local;



                    ReportDataSource objRDS;
                    LocalReport objLRPT = new LocalReport();
                    objLRPT = ReportViewerDealerStatus.LocalReport;

                    //objLRPT.Refresh();

                    objLRPT.ReportPath = Server.MapPath("../RDLC/TrainingReadinessReport.rdlc");

                    //Set Reports Parametres                                                                                                           
                    ReportParameter[] p ={              
                               new ReportParameter("Title","Some Report")};
                    objLRPT.SetParameters(p);
                    objRDS = new ReportDataSource();

                    objRDS.Name = "DataSet1";
                    BusinessRptTrainingReadiness.Delete(new EntityRptTrainingReadiness
                    {
                        sessionid = Session.SessionID
                    });

                    // We perform a for loop to check if each checkbox is selected then we get the value
                    foreach (ListItem objItem in chkLstSkill.Items)
                    {
                        if (objItem.Selected)
                        {
                            values += objItem.Value;
                            TrainingID = values;
                        
                          //  BusinessRptTrainingReadiness obj
                    BusinessRptTrainingReadiness.Save(new EntityRptTrainingReadiness
                     {
                         TrainingID = TrainingID,
                        sessionid=Session.SessionID
                     });

                  
                    values = "";
                    TrainingID = "";
                  
                        }
                       // objLRPT.DataSources.Add(objRDS);
                        //divReport.Visible = true;
                        //ReportViewerDealerStatus.Visible = true;
                    }
                    sessionid = Session.SessionID;
                                     objRDS.Value = new BusinessRptTrainingReadiness().RptTrainingReadiness(ChannnelID, DealerID, AreaID, ZoneID, TrainingID, sessionid, Role); //Gets procedure from business logic

                    objLRPT.DataSources.Clear();
                    objLRPT.DataSources.Add(objRDS);
                    divReport.Visible = true;
                    ReportViewerDealerStatus.Visible = true;

}

//For this  first created a dummy table
Consisting of TrainingID and sessionid

on checkbox selection it first deleted all the content from the table for the sessionid and inserted user selection. and inside the procedure i call the trainingID from the table using 'in' clause example(MPT.TrainingId IN (select TrainingId from DumyTrainingReadiness where sessionid =@sessionid))







CREATE Procedure Sp_test      
@ChannelID nvarchar(100),       
@DealerID nvarchar(100),         
       
@AreaID nvarchar(100),         
       
@ZoneID nvarchar(100),        
       
@RoleID nvarchar(100),       
@TrainingID nvarchar(100),       
@sessionid nvarchar(100)       
       
as       
       
SELECT Distinct MZ.ZONE,MA.AREANAME as AO,D.Code as DealerCode,D.Name as DealerName,MP1.EmployeeCode,         
(MP1.FirstName + ' ' + MP1.LastName) as Name,MS.SkillName as empRole,MT.Code,MPT.TrainingCompletionDate              
FROM MAS_AREA MA            
INNER JOIN MAS_ZONE MZ ON MA.ZONEID=MZ.ID          
          
INNER JOIN DEALER D ON MA.ID=D.AREACODEID           
INNER JOIN DEALERLOCATION  DL ON D.ID=DL.DEALERID  and DL.IsActive=1         
INNER JOIN DEALERCHANNELMAPPING DM ON D.ID=DM.DEALERID and D.IsActive=1         
inner join ManPowerTraining MPT on DL.ID=MPT.ManPowerId and MPT.IsActive=1         
INNER JOIN LOCATIONMANPOWERASSOCIATON LMA ON LMA.MANPOWERID = MPT.MANPOWERID and  LMA.ENDDATE IS NULL           
inner join  ManPower MP1 on MP1.ID=MPT.ManPowerId and MP1.IsActive=1         
inner join  ManPowerExperience MPE1 on MP1.ID=MPE1.ManPowerId and MPE1.IsActive=1         
inner join  Mas_Skills MS on MS.ID=LMA.SkillID and MS.IsActive=1         
inner join Mas_Training MT on MT.ID=MPT.TrainingId and MT.IsActive=1        
where       
 MPT.TrainingId IN (select TrainingId from DumyTrainingReadiness where sessionid =@sessionid)  -- 23 --COTEK BAISC MODULE - PERSONAL         
 --AND MT.ISACTIVE = 1         
 and       
 (@RoleID=0 OR @RoleID=NULL OR MPE1.SKILLID =@RoleID)-- 8 --COTEK  (role)       
AND DM.CHANNELID=@ChannelID --20         
 AND (@AREAID=0 OR @AREAID=NULL OR MA.ID=@AREAID)         
 AND (@DEALERID=0 OR @DEALERID= NULL OR D.ID=@DEALERID)         
AND (@ZONEID=0 OR @ZONEID=NULL OR MZ.ID=@ZONEID)        
   --Area       
  --Dealer       
 --AND MZ.ID=@ZONEID  --Zone       
        
order BY MZ.ZONE,MA.AREANAME

//

in the rdlc design
use matrix table instead of normal table
Afterbinding it to dataset,in the properties of the matrix table select group by to some id ,in my case i used EmployeeCode.

create disk space

#include "Ifx.h"
 prototype Get_disk_Space();
function Get_disk_Space()
    STRING   szDrive,svResultPath;
    long freespace,i;
    NUMBER   nvResult;
    ///LIST     listInfo;
begin 
   freespace=0;
AskPath ("Select drive:", "C:\\", svResultPath);
   i=0;
   freespace= GetDiskSpace(szDrive);  
  
   if(freespace<i) then
  
   // MessageBox ("not enough space", SEVERE);
    //  Disable (NEXTBUTTON);
 
       endif;
       
         if(freespace>i) then
        Enable (NEXTBUTTON);
        // MessageBox ("enough space,go ahead", INFORMATION);
   
       
       endif;
        ///Enable (FINISHBUTTON);
   
      end;
  
  
  
    // Create a list for system information.
  //  listInfo = ListCreate (STRINGLIST);
    // Get the amount of extended memory.
   // if (GetSystemInfo (EXTENDEDMEMORY, nvResult, svResult) < 0) then
       // szInfo = "Couldn't get EXTENDEDMEMORY info.";
   // else
     //   Sprintf(szInfo, "Extended memory: %d K", nvResult);
 //   endif;
    // Add the information to the list.
  //  ListAddString(listInfo, szInfo, AFTER);
    // Get the boot drive.
   // if (GetSystemInfo (BOOTUPDRIVE, nvResult, svResult) < 0) then
       // szInfo = "Couldn't get BOOTUPDRIVE info.";
   // else
       // Sprintf(szInfo, "Boot drive: %s", svResult);
    //endif;
    // Add the information to the list.
  //  ListAddString(listInfo, szInfo, AFTER);
    // Get info about the CD-ROM.
   // if (GetSystemInfo (CDROM, nvResult, svResult) < 0) then
       // szInfo = "Couldn't get CD-ROM info.";
   // else
       // if (nvResult = 0) then
          //  svResult = "No";
       // else
          //  svResult = "Yes";
      //  endif;
       
       // Sprintf(szInfo, "CDROM: %s", svResult);
    //endif;
    // Add the information to the list.
   // ListAddString(listInfo, szInfo, AFTER);
    // Get the video adapter.
  //  if (GetSystemInfo (VIDEO, nvResult, svResult) < 0) then
       // szInfo = "Couldn't get VIDEO info.";
   // else
       // switch (nvResult)
          //  case IS_UNKNOWN:
            //    szInfo = "VIDEO: UNKNOWN";
          //  case IS_SVGA:
           //     szInfo = "VIDEO: SVGA";
           // case IS_XVGA:
               // szInfo = "VIDEO: XVGA";
           //// case IS_UVGA:
            //    szInfo = "VIDEO: UVGA";
       // endswitch;
   // endif;
    // Add the information to the list.
    //ListAddString(listInfo, szInfo, AFTER);
    // Get number of available colors.
    //if (GetSystemInfo (COLORS, nvResult, svResult) < 0) then
        //szInfo = "Couldn't get COLORS info.";
   // else
       // Sprintf(szInfo, "Number of colors: %d", nvResult);
    //endif;
    // Add the information to the list.
 //   ListAddString(listInfo, szInfo, AFTER);
    // Get the current date.
  //  if (GetSystemInfo (DATE, nvResult, svResult) < 0) then
  //      szInfo = "Couldn't get DATE info.";
   // else
     //   Sprintf(szInfo, "DATE: %s", svResult);
    //endif;
    // Add the information to the list.
    //ListAddString(listInfo, szInfo, AFTER);
    // Get the current time.
  //  if (GetSystemInfo (TIME, nvResult, svResult) < 0) then
      //  szInfo = "Couldn't get TIME info.";
   // else
      //  Sprintf(szInfo, "TIME: %s", svResult);
    //endif;
  //
  //  // Add the information to the list.
   // ListAddString(listInfo, szInfo, AFTER);
   // // Get the operating system.
  //  if (GetSystemInfo (OS, nvResult, svResult) < 0) then
       // szInfo = "Couldn't get Operating System info.";
    //else
        //switch (nvResult)
           // case IS_WINDOWSNT:
             //   szInfo = "OS: Windows NT";
          //  case IS_WINDOWS9X:
               // GetSystemInfo (WINMINOR, nvResult, svResult);
               
              //  if (nvResult < 10) then
                   //  szInfo = "OS: Windows 95";
              //  else
                  //   szInfo = "OS: Windows 98";
             //   endif;
      //  endswitch;
   // endif;
 //   // Add the information to the list.
//    ListAddString(listInfo, szInfo, AFTER);
//    // Display the information.
//    szTitle   = "System Information";
//    szMsg     = "The following is some information related to your system:\n";
   
  //  SdShowInfoList (szTitle, szMsg, listInfo);
 //   ListDestroy(listInfo);
//end;

Example of a procedure using IF condition and it takes multiple training ID at any given time


Create Procedure Sp_test    
           
@ChannelID nvarchar(100),           
@DealerID nvarchar(100),           
@AreaID nvarchar(100),             
@ZoneID nvarchar(100),   
@TrainingID nvarchar(100)  ,
@sessionid nvarchar(100),         
@RoleID nvarchar(100)            
as           
           
declare  @str varchar(1000)           
Begin           
set @str= 'select distinct z1.Zone,A1.AreaName as AO ,D1.ID,D1.Code as dealerCode,D1.Name as DealerName,MPE1.ManPowerId,MP1.EmployeeCode,   
(MP1.FirstName + '''' + MP1.LastName) as Name,   
(select SkillName from dbo.Mas_Skills where ID=MPE1.SkillId) as empRole,MPT.TrainingId,MT.Code,MPT.TrainingCompletionDate   
 from DealerChannelMapping DM1,Dealer D1 ,Mas_Zone Z1,Mas_Area A1,   
  dbo.ManPowerExperience MPE1,   
 ManPower MP1 ,ManPowerTraining MPT,Mas_Training MT ,Mas_Skills MS 
where D1.ID=DM1.DealerID  and Z1.ID=A1.ZoneId  and MS.ID=MPE1.SkillId   
and MPE1.DealerID=D1.ID and MPT.TrainingId=MT.ID and  MS.IsActive=1  and MPE1.IsActive=1
and D1.IsActive=1 and MT.IsActive=1 and MPT.IsActive=1 and DM1.IsActive=1 and MP1.IsActive=1
and Z1.IsActive=1 and A1.IsActive=1 and
MPE1.ManpowerId=MP1.ID '           
           
if (@ChannelID <> 0)            
begin           
set @str= @str + ' and DM1.ChannelID='+@ChannelID            
end           
           
if (@DealerID <> 0)            
begin           
set @str= @str + ' and  DM1.DealerID='+@DealerID             
end           
           
           
if (@ZoneID <> 0)            
begin           
set @str= @str + ' and  Z1.ID='+@ZoneID             
end           
           
if (@AreaID <> 0)            
begin           
set @str= @str + ' and  A1.ID='+@AreaID            
end           
 if (@RoleID <> 0)            
begin           
set @str= @str + ' and  MPE1.SkillId='+@RoleID            
end            
 if (@TrainingID <> 0)            
begin           
set @str= @str + ' and MPT.TrainingId in (select TrainingId from DumyTrainingReadiness where sessionid = '''+@sessionid
    set @str= @str + ''')'       
end            
exec (@str)           
           
end

Procedure which accepts parameters and checks if the parameter is not 0 or null ,gets result based on parameter passed

//This Procedure accepts 5 parameters ,even if null or 0 is passed of any parameters it returs result.
//
Create procedure usp_testsp
@ChannelID nvarchar(100),     
@DealerID nvarchar(100),       
     
@AreaID nvarchar(100),       
     
@ZoneID nvarchar(100),      
     
@RoleID nvarchar(100)     
     
     
as     
     
SELECT Distinct MZ.ZONE,MA.AREANAME as AO,D.Code as DealerCode,D.Name as DealerName,MP1.EmployeeCode,     
(MP1.FirstName + ' ' + MP1.LastName) as Name,MS.SkillName as empRole,MT.Code,MPT.TrainingCompletionDate
FROM MAS_AREA MA        
INNER JOIN MAS_ZONE MZ ON MA.ZONEID=MZ.ID      
      
INNER JOIN DEALER D ON MA.ID=D.AREACODEID       
INNER JOIN DEALERLOCATION  DL ON D.ID=DL.DEALERID  and DL.IsActive=1     
INNER JOIN DEALERCHANNELMAPPING DM ON D.ID=DM.DEALERID and D.IsActive=1     
inner join ManPowerTraining MPT on DL.ID=MPT.ManPowerId and MPT.IsActive=1     
INNER JOIN LOCATIONMANPOWERASSOCIATON LMA ON LMA.MANPOWERID = MPT.MANPOWERID and  LMA.ENDDATE IS NULL       
inner join  ManPower MP1 on MP1.ID=MPT.ManPowerId and MP1.IsActive=1     
inner join  ManPowerExperience MPE1 on MP1.ID=MPE1.ManPowerId and MPE1.IsActive=1     
inner join  Mas_Skills MS on MS.ID=LMA.SkillID and MS.IsActive=1     
inner join Mas_Training MT on MT.ID=MPT.TrainingId and MT.IsActive=1    
where   
       
   
 (@RoleID=0 OR @RoleID=NULL OR MPE1.SKILLID =@RoleID)   
AND DM.CHANNELID=@ChannelID     
 AND (@AREAID=0 OR @AREAID=NULL OR MA.ID=@AREAID)     
 AND (@DEALERID=0 OR @DEALERID= NULL OR D.ID=@DEALERID)     
AND (@ZONEID=0 OR @ZONEID=NULL OR MZ.ID=@ZONEID)    

order BY MZ.ZONE,MA.AREANAME

Calling Required field validator from Code behind

//.aspx page

<asp:dropdownlist appenddatabounditems="true" autopostback="True" datatextfield="ChannelNo" datavaluefield="Id" id="ddlChannel" onselectedindexchanged="ddlChannel_SelectedIndexChanged" runat="server">
 </asp:dropdownlist>
  &nbsp;<asp:requiredfieldvalidator controltovalidate="ddlChannel" cssclass="ErrorMessage" errormessage="Select Channel Name" forecolor="" id="RequiredFieldValidator1" initialvalue="--Select--" runat="server" setfocusonerror="true" validationgroup="GnReport">
</asp:requiredfieldvalidator>

//.cs page

call it inside button click

   if (ddlChannel.Text == "0")
 
{

  RequiredFieldValidator1.Validate();
   
 RequiredFieldValidator1.IsValid = false;
 
 RequiredFieldValidator1.Visible = true;
 
  RequiredFieldValidator1.Enabled = true;
 
  RequiredFieldValidator1.ErrorMessage = "Select Channel Name.";
 
  return;
                }