Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I'm creating a view for three tables to view different data based on a Proposal ID.
I've got all of them pulled and everything worked fine but when I tried to join them only two records from a total of 250 appeared (those two records are records I linked with Personal ID to test if the query works)

I'm using MySQL and when I put the query in MySQL Workbench it works fine and pulls all the data, but when I run my project it only shows these two records as I previously mentioned.

What I have tried:

Here is my Context code:

<pre>       public List<PendingModel> GetAllNames()
        {
            List<PendingModel> list = new List<PendingModel>();

            using (MySqlConnection conn = GetConnection())
            {
                conn.Open();
                //MySqlCommand cmd = new MySqlCommand("select * from pendingproposals", conn);
                MySqlCommand cmd = new MySqlCommand("SELECT lname, IRG, IRGDate FROM proposal LEFT JOIN pendingproposals USING(proposal_id)  LEFT JOIN proposal_research_team USING(proposal_id) WHERE proposal.status in ('Unsubmitted','Dept Approval In Process', 'Admin Office In Process', 'JIT Request Received', 'JIT Submitted', 'Submitted to Sponsor', 'Sub 1/4: Awaiting Agreement', 'Sub 2/4: Under Negotiation', 'Sub 3/4: Partially Executed') AND role = 'Lead Principal Investigator';", conn);
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                            if (!DBNull.Value.Equals(reader[2]))
                            {
                                list.Add(new PendingModel()
                                {
                                    PILastName = reader["lname"].ToString(),
                                    IRG = reader["IRG"].ToString(),
                                    IRGDate = (DateTime)reader["IRGDate"]
                                });
                            }
                    }
                }
            }
            return list;
        }


My Controller:

public IActionResult Index()
 {
     PendingContext context = HttpContext.RequestServices.GetService(typeof(PendingTest.Models.PendingContext)) as PendingContext;
     return View(context.GetAllNames());
 }


My View:

@model IEnumerable<PendingTest.Models.PendingModel>

@{
    ViewData["Title"] = "Pending Approvals";
}

<center>class="fa fa-github-alt fa-lg" href="url-here" style="margin: 10px"> Edit
                    </a>
                    <a class="btn btn-danger" href="#">
                        ^__i class="fa fa-heart-o fa-lg"> Delete
                    </a>
                </td>
            </tr>
            }
        </tbody>
    </table>


I would appreciate the help
Posted
Updated 11-Feb-20 8:58am
Comments
Richard Deeming 11-Feb-20 14:58pm    
Which table is the role column defined on?

And why are you filtering out null dates in code, when you could more easily do it in your query?
NextGratus 11-Feb-20 16:04pm    
Role is part of the proposal_research_team table, which I only use to pull out the lname.
For the Null, I get a run time error when I try to execute in which I had to bypass that just so I can make sure it works for now.
NextGratus 11-Feb-20 16:34pm    
whenever I try to pull the data without the if statement I get an 'System.DBNull' to type 'System.DateTime'.' error

I would like all the entries to display even with the null values as I want the user to later on insert them if they are not available using the Edit button I am using in the view.

1 solution

While the query may work in your SQL IDE, is that taking into consideration this if...then clause?
C#
using (var reader = cmd.ExecuteReader())
{
   while (reader.Read())
   {
      if (!DBNull.Value.Equals(reader[2])) // how many rows have NULLs in them?
      {
         list.Add(new PendingModel()
You may want to run this query through you the MySQL Workbench
SQL
SELECT lname, IRG, IRGDate
FROM   proposal
LEFT JOIN pendingproposals       USING(proposal_id)
LEFT JOIN proposal_research_team USING(proposal_id)
WHERE  proposal.status in (
          'Unsubmitted',
          'Dept Approval In Process',
          'Admin Office In Process',
          'JIT Request Received',
          'JIT Submitted',
          'Submitted to Sponsor',
          'Sub 1/4: Awaiting Agreement',
          'Sub 2/4: Under Negotiation',
          'Sub 3/4: Partially Executed' )
AND    role = 'Lead Principal Investigator'
AND    IRGDate IS NOT NULL; -- added condition to check for your null value
 
Share this answer
 
Comments
NextGratus 11-Feb-20 16:23pm    
That actually helped to understand the problem, I am filtering all the other entries because of not accepting Null from the IRGDate, I would like to bypass that and allow view to display Null values as well. (it gave me a DBNull error before that's why I created the if statement)
MadMyche 11-Feb-20 17:14pm    
I use an Extension Method which will convert NULL values to a default [for date it is Date(0)] and then swap the Date(0) at the presentation level

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