I tried this code in C# winforms but its not working like MSSQL ,
when i select data and using string_agg in SQL its working , but in C# forms its not working and
show the data on multiple lines
This is the code :
private void BtnSearch_Click(object sender, EventArgs e)
{
if (chkCash.Checked == false && chkCovid.Checked == false)
{
btnCash.Enabled = false;
BtnPrint.Enabled = true;
string sql = @" SELECT distinct a.patient_no as 'File No' ,
a.Patient_name as 'Patient Name' ,
b.order_id as 'Order Id' ,
c.custid as 'Clinic No',
c.custname as 'Clinic Name' ,
e.TestId as 'Test Id',
string_agg(e.testname, ',') as 'Test',
b.order_date as 'Order Date',
b.lab_no as 'Lab No'
FROM patients a , lab_orders b ,customers c , order_details d , labtests e
where a.patient_no = b.patient_no
and b.custid = c.custid
and b.order_id = d.order_id
and d.testid = e.testid
and c.CustId > 1
and d.TESTID <> 6438
and cast(b.order_date as time) between '01:00:00' and '23:50:50' ";
string condition = "";
string orderby = "";
orderby += " ORDER BY c.custid";
string groupby = "";
groupby += " group by a.patient_no,a.Patient_name , b.order_id , c.custid , c.custname , b.order_date , b.lab_no, e.TestId ";
DateTime fromDate;
DateTime toDate;
if (!DateTime.TryParse(dtFromDate.Value.ToString(), out fromDate))
{
System.Windows.Forms.MessageBox.Show("Invalid From Date");
}
else if (!DateTime.TryParse(dtToDate.Value.ToString(), out toDate))
{
System.Windows.Forms.MessageBox.Show("Invalid to Date");
}
else
{
condition += " and cast(b.order_date as date) between '" + fromDate + "' and '" + toDate + "'";
}
DataTable dt = data.fireDatatable(string.Format(sql + condition + groupby + orderby));
OrdersDataGridView.DataSource = dt;
OrdersDataGridView.Refresh();
}
Can I use it in winforms ?
Example : in SQL SERVER when I run the SELECT the output for orders and tests like this :
Test order_id
CBC,TSH,LDL 100
In C# when run the above code the output for order multiple lines and not one row for each order in case multiple tests ordered in one order :
Test order_id
CBC 100
TSH 100
LDL 100