2016-07-18 2 views
0

필자는 버튼을 한 번 클릭하여 2 개의 SQL 쿼리를 전달하려고 시도 했으므로 아래에 표시된 것처럼 ive가 수행하려고 시도한 것이지만 작동하지 않습니다 .... 감사합니다. 사전onclick C에서 두 메서드 호출 #

그래서 이것은 앞의 코드

<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click1; btnWedRecInsert_Click2;" />

코드 뒤에

protected void btnWedRecInsert_Click1(object sender, EventArgs e) 
{ 

    string Function = ddlFunction.Text; 
    string FunctionDate = txtFunctionDate.Text; 
    string FunctionTime = ddlFunctionTime.Text; 
    string groomName = txtGroomName.Text; 
    string groomFatherName = txtGroomFatherName.Text; 
    string groomAge = txtGroomAge.Text; 
    string groomPhone = txtGroomPhone.Text; 
    string groomAddress = txtGroomAddress.Text; 
    string brideName = txtBrideName.Text; 
    string brideFatherName = txtBrideFatherName.Text; 
    string brideAge = txtBrideAge.Text; 
    string bridePhone = txtBridePhone.Text; 
    string brideAddress = txtBrideAddress.Text; 
    string registerName = txtRegisterName.Text; 
    string registerPhone = txtRegisterPhone.Text; 
    string registerAddress = txtRegisterAddress.Text; 
    string referenceName = txtReferenceName.Text; 
    string referencePhone = txtReferencePhone.Text; 
    string referenceAddress = txtReferenceAddress.Text; 



    string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString; 
    OleDbConnection connection = new OleDbConnection(connString); 
    string insertQuery = "INSERT INTO wedding(RegisteredDate, Function, FunctionDate, FunctionTime, groomName, groomFatherName, groomAge, groomPhone, groomAddress, brideName, brideFatherName, brideAge, bridePhone, brideAddress, registerName, registerPhone, registerAddress, referenceName, referencePhone, referenceAddress) VALUES(@Date, @Function, @FunctionDate, @FunctionTime, @groomName, @groomFatherName, @groomAge, @groomPhone, @groomAddress, @brideName, @brideFatherName, @brideAge, @bridePhone, @brideAddress, @registerName, @registerPhone, @registerAddress, @referenceName, @referencePhone, @referenceAddress) "; 
    OleDbCommand command = new OleDbCommand(); 
    command.Connection = connection; 
    command.CommandText = insertQuery; 
    command.CommandType = CommandType.Text; 
    command.Parameters.AddWithValue("@Date", DateTime.Now.Date); 
    command.Parameters.AddWithValue("@Function", Function); 
    command.Parameters.AddWithValue("@FunctionDate", FunctionDate); 
    command.Parameters.AddWithValue("@FunctionTime", FunctionTime); 
    command.Parameters.AddWithValue("@groomName", groomName); 
    command.Parameters.AddWithValue("@groomFatherName", groomFatherName); 
    command.Parameters.AddWithValue("@groomAge", groomAge); 
    command.Parameters.AddWithValue("@groomPhone", groomPhone); 
    command.Parameters.AddWithValue("@groomAddress", groomAddress); 
    command.Parameters.AddWithValue("@brideName", brideName); 
    command.Parameters.AddWithValue("@brideFatherName", brideFatherName); 
    command.Parameters.AddWithValue("@brideAge", brideAge); 
    command.Parameters.AddWithValue("@bridePhone", bridePhone); 
    command.Parameters.AddWithValue("@brideAddress", brideAddress); 
    command.Parameters.AddWithValue("@registerName", registerName); 
    command.Parameters.AddWithValue("@registerPhone", registerPhone); 
    command.Parameters.AddWithValue("@registerAddress", registerAddress); 
    command.Parameters.AddWithValue("@referenceName", referenceName); 
    command.Parameters.AddWithValue("@referencePhone", referencePhone); 
    command.Parameters.AddWithValue("@referenceAddress", referenceAddress); 


    try 
    { 
     connection.Open(); 
     command.ExecuteNonQuery(); 
     lblMessage.Text = "Record inserted successfully"; 
    } 
    catch (Exception ex) 
    { 
     lblMessage.Text = "Unable to insert record"; 
    } 
    finally 
    { 
     connection.Close(); 
    } 
} 


    protected void btnWedRecInsert_Click2(object sender, EventArgs e) 
    { 
     string Function = ddlReception.Text; 
     string FunctionDate = txtReceptionDate.Text; 
     string FunctionTime = ddlReceptionTime.Text; 
     string groomName = txtGroomName.Text; 
     string groomFatherName = txtGroomFatherName.Text; 
     string groomAge = txtGroomAge.Text; 
     string groomPhone = txtGroomPhone.Text; 
     string groomAddress = txtGroomAddress.Text; 
     string brideName = txtBrideName.Text; 
     string brideFatherName = txtBrideFatherName.Text; 
     string brideAge = txtBrideAge.Text; 
     string bridePhone = txtBridePhone.Text; 
     string brideAddress = txtBrideAddress.Text; 
     string registerName = txtRegisterName.Text; 
     string registerPhone = txtRegisterPhone.Text; 
     string registerAddress = txtRegisterAddress.Text; 
     string referenceName = txtReferenceName.Text; 
     string referencePhone = txtReferencePhone.Text; 
     string referenceAddress = txtReferenceAddress.Text; 



     string connString = ConfigurationManager.ConnectionStrings["MandapamDatabase"].ConnectionString; 
     OleDbConnection connection = new OleDbConnection(connString); 
     string insertQuery = "INSERT INTO wedding(RegisteredDate, Function, ReceptionTime, FunctionDate, FunctionTime, groomName, groomFatherName, groomAge, groomPhone, groomAddress, brideName, brideFatherName, brideAge, bridePhone, brideAddress, registerName, registerPhone, registerAddress, referenceName, referencePhone, referenceAddress) VALUES(@Date, @Function, @FunctionDate, @FunctionTime, @groomName, @groomFatherName, @groomAge, @groomPhone, @groomAddress, @brideName, @brideFatherName, @brideAge, @bridePhone, @brideAddress, @registerName, @registerPhone, @registerAddress, @referenceName, @referencePhone, @referenceAddress) "; 
     OleDbCommand command = new OleDbCommand(); 
     command.Connection = connection; 
     command.CommandText = insertQuery; 
     command.CommandType = CommandType.Text; 
     command.Parameters.AddWithValue("@Date", DateTime.Now.Date); 
     command.Parameters.AddWithValue("@Function", Function); 
     command.Parameters.AddWithValue("@FunctionDate", FunctionDate); 
     command.Parameters.AddWithValue("@FunctionTime", FunctionTime); 
     command.Parameters.AddWithValue("@groomName", groomName); 
     command.Parameters.AddWithValue("@groomFatherName", groomFatherName); 
     command.Parameters.AddWithValue("@groomAge", groomAge); 
     command.Parameters.AddWithValue("@groomPhone", groomPhone); 
     command.Parameters.AddWithValue("@groomAddress", groomAddress); 
     command.Parameters.AddWithValue("@brideName", brideName); 
     command.Parameters.AddWithValue("@brideFatherName", brideFatherName); 
     command.Parameters.AddWithValue("@brideAge", brideAge); 
     command.Parameters.AddWithValue("@bridePhone", bridePhone); 
     command.Parameters.AddWithValue("@brideAddress", brideAddress); 
     command.Parameters.AddWithValue("@registerName", registerName); 
     command.Parameters.AddWithValue("@registerPhone", registerPhone); 
     command.Parameters.AddWithValue("@registerAddress", registerAddress); 
     command.Parameters.AddWithValue("@referenceName", referenceName); 
     command.Parameters.AddWithValue("@referencePhone", referencePhone); 
     command.Parameters.AddWithValue("@referenceAddress", referenceAddress); 


     try 
     { 
      connection.Open(); 
      command.ExecuteNonQuery(); 
      lblMessage.Text = "Record inserted successfully"; 
     } 
     catch (Exception ex) 
     { 
      lblMessage.Text = "Unable to insert record"; 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
+0

하지만이 두 가지 버튼 클릭 이벤트를 .... 표시하지 1 .. 당신이'의 OnClick = 사용할 때 1 – BugFinder

+0

이 오류가 던져 질 때 무슨 일이 있었는지 "btnWedRecInsert_Click1, btnWedRecInsert_Click2가;"'? –

+0

이러한 클릭 이벤트는 다른 이벤트 또는 다른 이벤트를 트리거하는 단추와 같은 다른 곳에서 재사용됩니까? 어느 쪽이든 나는이 사건들의 논리를 그들 자신의 방법으로 추출 할 것을 제안 할 것이다. 그런 다음 각 이벤트 메서드는 적절한 메서드를 호출하는 한 줄을 가질 수 있으며 두 이벤트를 호출하는 다른 이벤트가 필요한 경우 추출 된 메서드를 모두 호출하는 새 이벤트 메서드를 만듭니다. – Chris

답변

1

이 변경 onClick 이벤트가 btn_하는 아래 {NameOfYourBu 가독성을 위해 어쩌면 당신의 이름을 변경해야합니다

protected void btnWedRecInsert_Click(object sender, EventArgs e) 
{ 
btnWedRecInsert_Click1(); 
btnWedRecInsert_Click2(); 
} 

: tton}을 클릭 한 다음

public void btn_{NameOfYourButton}Click() { 
    btnWedRecInsert_Click1(); 
    btnWedRecInsert_Click2(); 
} 
+0

덕분에 많은 도움을 주었던 –

2

당신은 당신의 마크 업을 변경해야합니다 있습니다

<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click" /> 

그리고 당신의 코드 숨김 변경 삽입 기능은 다음과 같습니다.

insertWedding1(); 
insertWedding2(); 

현재로서는 두 insert 함수 사이에 차이점을 볼 수 없습니다. mayby ​​당신은 나중에 그들을 바꿀 것이다. 그러나 그들은 아주 유사하다. 매개 변수를 사용하여 함수를 작성하는 방법은 어떨까요? 그렇게 많은 코드를 작성할 필요는 없습니다.

insertWedding(object paramWedding1); 
insertWedding(object paramWedding2); 
+0

고마워요. 차이점은 제가 언젠가는 다른 텍스트 상자에서 값을 삽입하고 다른 날에는 결혼식에 넣고 있습니다. –