Search code examples
c#sqlwinformscrystal-reportsdesktop-application

How to set non Table field of SQL into Crystal Report Design and Parameter


I am fetching data by this query

select 
    employee.empcode, employee.fullname,
    count(attendance.Status) 
from 
    employee 
inner join 
    attendance on employee.empcode = attendance.EmpCode 
group by 
    employee.empcode, employee.fullname 
order by 
    employee.empcode

Sql Result

But (No Column Name) is not my SQL table's column - how to put it into Crystal Report Design and Code?

rptAttendance rpt = new rptAttendance();

SqlConnection myConnection = new SqlConnection(connectionString);
SqlCommand MyCommand = new SqlCommand();
SqlDataAdapter myDA = new SqlDataAdapter();
DataSet myDS = new DataSet();

MyCommand.Connection = myConnection;
MyCommand.CommandText = "select employee.empcode, employee.fullname , COUNT(attendance.Status) from employee inner join attendance on employee.empcode = attendance.EmpCode group by employee.empcode, employee.fullname order by employee.empcode";
MyCommand.CommandType = CommandType.Text;

myDA.SelectCommand = MyCommand;
myDA.Fill(myDS, "Attendance");
myDA.Fill(myDS, "Employee");

rpt.SetDataSource(myDS);
CrystalReportViewer1.ReportSource = rpt;

Report Design

enter image description here


Solution

  • you miss the name for the last column, try with:

    select employee.empcode, employee.fullname , COUNT(attendance.Status) countColumn from employee inner join attendance on employee.empcode = attendance.EmpCode group by employee.empcode, employee.fullname order by employee.empcode
    

    Edit: adding structure into report

    i usually create a fake view like:

    create view fakeView as
    select convert(nvarchar(yourrealColumnLength) '') columnName,
           convert(int, 0) column2Name
           .......
    

    then in the right panel inside crystal interface, just link your view to get a structure.

    This could help you: example