2013-11-20 2 views
0

나는 2 개의 함수를 가지고 있으므로, function1이 완료되면 함수 2가 실행되고 싶습니다. 그렇지 않으면 함수 2가 실행되지 않도록해야합니다. 실행 (Didnot Execute !!).fun1 완료되면 fun2, 그렇지 않으면 fun2 않습니다.

public void SaveDuplicatCourse() 
{ 
    if(con.State !=ConnectionState.Open) 
     con.Open(); 
    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
     IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 
    foreach (int i in IDs) 
    { 
     try 
     { 
     SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses 
              values(" + i + "," + CCID + ")", con); 
     com.ExecuteNonQuery(); 
     } 
     catch (Exception) 
     { 
     MessageBox.Show("They are exist"); 
     } 
    } 
    con.Close(); 
} 

public void Save() 
{ 
    SqlCommand com = new SqlCommand(@"Delete from students 
            where 
            Course_ID = " + ID, con); 
    con.Open(); 
    com.ExecuteNonQuery(); 

    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value!=null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
     IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 

    foreach (int i in IDs) 
    { 
     com.CommandText = "Insert into students values(" + i + "," + ID + ")"; 
     com.ExecuteNonQuery(); 
    } 
    con.Close(); 
} 

나는 그것이 방법을 가르쳐주세요

private void buttonSaveChanges_Click(object sender, EventArgs e) 
{ 
    SaveDuplicatCourse(); 
    Save(); 
} 

아래로 ButtonClick에서이 함수를 호출 : 아래로 서로 코드,

내 기능 1 SaveDuplicatCourse()에 전화 기능 2 Save()라고 끝날거야.

+3

나는 당신이 무슨 말을하는지 모른다. '실행되지 않았다 '?? 뭐야 ...? –

+3

이러한 함수가 void 대신 bool을 반환하도록합니다. – MikeTheLiar

+1

첫 번째 함수에서'bool'을 리턴합니까? –

답변

1

function1에 boolean 값을 추가하십시오. false를 반환하면 function2를 실행하지 마십시오. true를 리턴하면 function2를 실행하십시오. 예 아래

boolean saveDuplicateCourseCompleted = false; 

private void buttonSaveChanges_Click(object sender, EventArgs e) 
{ 
    if (saveDuplicateCourseCompleted == true) 
     Save(); 
    else 
     // or do something else.   
} 

참고 :

public boolean SaveDuplicatCourse() 
{ 
    if(con.State !=ConnectionState.Open) 
     con.Open(); 
    List<int> IDs = new List<int>(); 
    foreach (DataGridViewRow r in dataGridViewStudents.Rows) 
    { 
     if (r.Cells[0].Value != null && bool.Parse(r.Cells[0].Value.ToString())) 
     { 
      IDs.Add(int.Parse(r.Cells[1].Value.ToString())); 
     } 
    } 
    foreach (int i in IDs) 
    { 
     try 
     { 
      SqlCommand com = new SqlCommand(@"Insert into DuplicateCourses 
      values(" + i + "," + CCID + ")", con); 
      com.ExecuteNonQuery(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("They are exist"); 
      saveDuplicateCourseCompleted=false// Now this is right way 
     } 
    } 
    con.Close(); 

} 
+0

지금은 잘 작동하므로^_^감사합니다. –

1

예제에서 비동기 적으로 발생하는 내용은 표시되지 않으므로 코드가 원하는 방식으로 실행되어야합니다. 이제 성공적으로 실행하는 것에 대해 질문하는 경우 (실패한 행이 없어도 행이 삭제되지 않음), 이는 다른 이야기이며 다양한 다양한 방식으로 처리 될 수 있습니다. 그러나 우리가 그 길을 떠나기 전에 이미 원하는 결과를 얻고있는 것 같습니다. 중단 점을 설정하고 각 코드 행을 단계별로 실행 순서를 확인하십시오.

+0

죄송합니다,하지만 내 코드가 실행되지 않습니다. –

관련 문제