Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i write Join sql query in C# project. I have multiple table & i want to join the table and get the report in excel. i have written the following code but given error. please help me. i am new in this field..

===
C#
protected void Button1_Click(object sender, EventArgs e)
        {
            DataSet dsExport = GetData();
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw =
               new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dsExport;

            //Report Header

            // Get the HTML for the control.
            dgGrid.HeaderStyle.Font.Bold = true;
            dgGrid.DataBind();
            dgGrid.RenderControl(hw);

            // Write the HTML back to the browser.
            Response.ContentType = "application/vnd.ms-excel";
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
            //SqlCommand cmd = new SqlCommand("SELECT * FROM [Membermaster] where actuledate between '" + tbfromdate.Text.Trim() + "' and '" + tbtodate.Text.Trim() + "' ", cnn);
        }

        public DataSet GetData()
        {
            DataSet dataSet = new DataSet();
            SqlDataAdapter dataAdapter = new SqlDataAdapter();
            SqlConnection cnn = new SqlConnection("user id=sa;" +
                                      "password=prodip@km;server=PRODIP;" +
                                       "Trusted_Connection=yes;" +
                                       "database=SBT_001;");
            cnn.Open();
            dataAdapter.SelectCommand = cnn.CreateCommand();
            dataAdapter.SelectCommand.CommandText = "SELECT distinct Loanmst.LoanId,'ACTCRD' as SEGMENT_INDETIFIRE,Loanmst.LoanId,Loanmst.LoanId,DB_Name(),(convert(varchar(10),marketmst.MarketId)+'-'+marketmst.Market)'Center Name',EOMst.EOName'RO Name',REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '/', '') AS [account_report_date],'JLG' as Loan_category,(groupmst.groupcode+'-'+GroupMst.GroupName)'Group_ID',Loanmst.dose'Loan_Cycle_ID'," +
"LoanPurposeMst.Purpose'Loan_Purpose',Loanmst.Status'Account_Status',LoanApplication.AppDate'Application_date',LoanApplication.SanDate'Sanctioned_Date',Loanmst.LoanDt'Date_Opened',Loanmst.ClosingDt'Date_closed',MAX(CollectionDtLoanmst.RecDate)'Date_of_Last_Payment'," +
"LoanApplication.LoanAppAmt'Applied_for_amount',LoanApplication.ApprovedAmt'Loan_amount_sanction',Loanmst.LoanAmt'Total_amount_disbursed',Loanmst.TotalInstNo'Number_of_Installment',Loanmst.RSchedule'Payment_Frequency',Loanmst.InstallmentAmt'Minimum_amt_due'," +
"Round(LoanAmt -(Select isnull(SUM(PrinCollAmt),0) from CollectionDtl Where LoanId  = Loanmst.Loanid And RecDate <= '02/28/2014'),1) 'Current_Balance'," +
"ROUND((select ISNULL(sum(LoandtLoanmst.Resamt),0) from LoanDtl where LoanId  = Loanmst.Loanid and DueDt<= '02/28/2014')-(Select isnull(SUM(CollAmt),0) from CollectionDtl Where LoanId  = Loanmst.Loanid And RecDate <= '02/28/2014'),1)'Amount_Overdue'," +
"(floor (DATEDIFF(DAY, MAX(CollectionDtLoanmst.RecDate), GETDATE())))'DPD'," +
"(select ISNULL(sum(CollectionDtLoanmst.CollAmt),0) from CollectionDtl where LoanId  = Loanmst.Loanid and RecDate<= '02/28/2014' and DescId='G0023')'Write off Amt'," +
"Loanmst.WriteoffDate'Date_of_writeoff',Loanmst.LWaveOffId'Write_of_Reason','' as'Number_of_Meeting_held','' as'No_of_Absentees_in_Meeting','yes'as'Insurance_Indicator' ,'Cr insurance'as 'Type_of_Insurance' ,Loanmst.LoanAmt 'Sum_assured'," +
"CASE WHEN cr.CollDay = '1' THEN 'MON' WHEN cr.CollDay = '2' THEN 'TUE' WHEN cr.CollDay = '3'THEN 'WED'WHEN cr.CollDay = '4'THEN 'THU'WHEN cr.CollDay = '5'THEN 'FRI'WHEN cr.CollDay = '6'THEN 'SAT'" +
"END as Agreed_Meeting_week_day,cr.Colltime'Agreed_Meeting_day_Time','' as Received_for_Future_Use,''as Old_Member_code,'' as old_member_shrt_name,'' as Old_account_NBR," +
"'' as CIBIL_ACT_STATUS,'' as ASSET_CLASIFICATION,Membermst.memberno 'MEMBER_CODE','' as MEMBER_SHRT_NM,'' as ACCOUNT_TYPE,'' as OWNERSHIP_IND," +
"Membermst.memberno 'PARENT_ID','' as EXTRACTION_FILE_ID,'' as SEVERITY" +
"From LoanMst join MemberMst on (membermst.MemberId=Loanmst.MemberId)" +
"join GroupMst on(MemberMst.GroupId=GroupMst.GroupId)" +
  "join MarketMst on(GroupMst.MarketId=MarketMst.MarketId)" +
  "join EOMst on(EOMst.EOId=MarketMst.Eoid)" +
  "join LoanPurposeMst on (Loanmst.PurposeId=LoanPurposeMst.LPurposeId)" +
  "join LoanApplication on (Loanmst.LoanAppId=LoanApplication.LoanAppId)" +
  "join CollectionDtl on (Loanmst.LoanId=CollectionDtLoanmst.LoanId)" +
  "join CollectionRoutine on MarketMst.MarketId=CollectionRoutine.MarketId" +
  "join CollectionRoutine cr on MarketMst.MarketId=cr.MarketId" +
"Where LoanDt between  '02/15/2014' and '02/28/2014' -- <= '02/28/2014'" +
"And (Loanmst.ClosingDt > '02/28/2014' or Loanmst.ClosingDt is null)" +
"And (Loanmst.WriteoffDate > '02/28/2014' or Loanmst.WriteoffDate is null)" +
"And (Loanmst.TrDate<='02/28/2014' Or Loanmst.TrDate Is Null)" +
"group by Loanmst.LoanId,Loanmst.LoanId,Loanmst.LoanId,(convert(varchar(10),marketmst.MarketId)+'-'+marketmst.Market),EOMst.EOName,(groupmst.groupcode+'-'+GroupMst.GroupName),Loanmst.dose," +
"LoanPurposeMst.Purpose,Loanmst.Status,LoanApplication.AppDate,LoanApplication.SanDate,Loanmst.LoanDt,Loanmst.ClosingDt," +
"LoanApplication.LoanAppAmt,LoanApplication.ApprovedAmt,Loanmst.LoanAmt,Loanmst.TotalInstNo,Loanmst.RSchedule,Loanmst.InstallmentAmt," +
"MemberMst.Membername, Loanmst.memberid,Loanmst.LoanNo,Loanmst.loanamt,Loanmst.dose,Loanmst.loandt,Loanmst.WriteoffDate,Loanmst.LWaveOffId,Loanmst.LoanAmt,cr.CollDay,cr.CollTime,MemberMst.MemberNo" +
"ORDER BY EOMst.EOName,marketmst.Market,GroupMst.GroupName";
 dataAdapter.Fill(dataSet);
 dataAdapter.Dispose();

 return dataSet;               
                
                
                
                
  }

       }
        }
Posted
Updated 4-Mar-14 23:58pm
v2
Comments
Ramug10 5-Mar-14 5:58am    
This is not good way of writing sql statements write sql stored procedure instead of this.

OK, there few things you should think of before doing this.

1. Is SSRS an option?
2. Why use these queries in code? Why not go for a stored procedure?
3. Do you really need to use SqlDataAdapter and DataSet?

This is rather huge query and probably a little difficult to identify the error. I would recommend running this query directly on SSMS and share the error details you get from there. Also, if your job contract permits, can you share schema script for tables in query so people can run it locally to help.
 
Share this answer
 
First of all telling us "given error" is not going to help us for detecting the issue.
You need to provide the details of the issue.

Before you do that, I strongly advice you to move this query to a SQL Stored Procedure and pass parameters from code if you want any.

Think of somebody who is going to see your code in your absence. He/she might faint.

Write good code. :)
 
Share this answer
 

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