Domanda

I have a medicalcentre table and windowadmin table which have relationship with each other using mcID. I display mcType and mcCentre first which exist in medicalcentre table to a gridview, after that I use outer join to display winusername field and winPassword field in windowsadmin table to a gridview. What I want to achieve is to use the delete function to delete a record. But I got error, see the 3rd image.

My windowsadmin and medicalcentre table enter image description here

My windowsadmin and medicalcentre table relationship enter image description here

My form enter image description here

My Error enter image description here

private void LoadMedicalCentreRecords()
        {

            //retrieve connection information info from App.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
            //STEP 1: Create connection
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            //STEP 2: Create command
            string strCommandText = "SELECT cen.mcid, mcType, mcCentre, win.winUsername, win.winPassword FROM MEDICALCENTRE AS cen";
            strCommandText += " LEFT OUTER JOIN WINDOWSADMIN as win on cen.mcid = win.mcid";
            MedicalCentreAdapter = new SqlDataAdapter(strCommandText, myConnect);

            //command builder generates Select, update, delete and insert SQL
            // statements for MedicalCentreAdapter
            //SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(MedicalCentreAdapter);
            // Empty Employee Table first
            MedicalCentre.Clear();
            // Fill Employee Table with data retrieved by data adapter
            // using SELECT statement
            MedicalCentreAdapter.Fill(MedicalCentre);

            // if there are records, bind to Grid view & display
            if (MedicalCentre.Rows.Count > 0)
                grdMc.DataSource = MedicalCentre;
        }




private int DeleteMedicalCentreRecord()
        {
            // no row in GridView selected
            if (currentRow == null)
            {
                return 0;
            }
            //retrieve connection information info from App.config
            string strConnectionString = ConfigurationManager.ConnectionStrings["SACPConnection"].ConnectionString;
            //STEP 1: Create connection
            SqlConnection myConnect = new SqlConnection(strConnectionString);
            //STEP 2: Create command
            string strCommandText = "DELETE FROM WINDOWSADMIN WHERE winID=@WINID; DELETE FROM MEDICALCENTRE WHERE mcID=@MCID;";
            SqlCommand deleteCmd = new SqlCommand(strCommandText, myConnect);

            // Cell - contains employee ID
            int winID = Convert.ToInt32(currentRow.Cells[0].Value);
            int mcID = Convert.ToInt32(currentRow.Cells[0].Value);
            deleteCmd.Parameters.AddWithValue("@WINID", winID);
            deleteCmd.Parameters.AddWithValue("@MCID", mcID);           
            //// STEP 3: open connection and retrieve data by calling ExecuteReader
            myConnect.Open();
            //STEP 4: execute command
            int result = deleteCmd.ExecuteNonQuery();
            // STEP 5: Close
            myConnect.Close();
            return result;
        }
È stato utile?

Soluzione

I think you need to change your DELETE commands to

string strCommandText = @"DELETE FROM WINDOWSADMIN WHERE mcID=@MCID; 
                          DELETE FROM MEDICALCENTRE WHERE mcID=@MCID;";

This ensure that every win user that has a releationship with the medical center that you want to delete will be deleted from the WindowsAdmin table

In other words, you can delete every record you like in the WINDOWSADMIN table but you cannot delete a record in the MEDICALCENTRE table if there is a record in the WINDOWSADMIN table that still REFERENCES the record in medicalcentre that you want to delete

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top