One of my field values only can successfully display on the first row. the subsequent row doesn't

StackOverflow https://stackoverflow.com/questions/21498806

  •  05-10-2022
  •  | 
  •  

Question

I have a appointment form which will display data in a gridview based on the appointment table. Appointment table have relationship with patient and medicalcentre tables. I wrote the select statement so that patientID will be changed to pFirstName in patient table, and mcID will be changed to mcCentre in the medicalcentre table. Weird thing is pFirstName have no problem, but mcCentre doesn't display after the first row. If I didn't outer join and leave it as mcID, it will display mcID fine on all rows.

MY DATAGRID for appointment form before outer join, display mcID

enter image description here

MY DATAGRID for appointment form after outer join, from mcID changed to mcCentre

enter image description here

MY APPOINTMENT, PATIENT, MEDICALCENTRE TABLE

enter image description here

//MY appointment form after outer join, from mcID changed to mcCentre codes

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Data;

public partial class member_viewappointment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == false)
        {
            // call BindGridView
            bindGridView();

        }
    }

    private void bindGridView()
    {
        int ID = Convert.ToInt32(Session["ID"].ToString());
        //get connection string from web.config
        string strConnectionString = ConfigurationManager.ConnectionStrings["sacpConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "SELECT aStatus, aDate, aTime, aContact, aHeight, aWeight, med.mcCentre, pat.pFirstName from appointment AS app ";
        strCommandText += " LEFT OUTER JOIN MEDICALCENTRE as med on app.appointmentid = med.mcid";
        strCommandText += " LEFT OUTER JOIN PATIENT as pat on app.patientid = pat.patientid ";
        //strCommandText += " WHERE app.patientid = " + ID.ToString();

        try
        {
            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            myConnect.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            DataTable dt = new DataTable();
            dt.Load(reader);
            grdViewAppointment.DataSource = dt;
            grdViewAppointment.DataBind();
            lblResult.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblResult.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            myConnect.Close();
        }

    }
}
Was it helpful?

Solution

You join MEDICALCENTRE table on app.appointmentid = med.mcid

LEFT OUTER JOIN MEDICALCENTRE as med on app.appointmentid = med.mcid

which does not look right. It looks like you meant to join on app.mcid = med.mcid

LEFT OUTER JOIN MEDICALCENTRE as med on app.mcid = med.mcid
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top