我一直試圖通過單擊按鈕一次傳遞2個SQL查詢,所以這是什麼一直試圖做如下所示,但它不工作....請幫助...謝謝推進從onclick調用2個方法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();
}
}
但顯示2個不同的按鈕點擊事件....沒有1 ..發生了什麼事時,它是1 – BugFinder
當您使用'的OnClick =拋出任何錯誤 「btnWedRecInsert_Click1; btnWedRecInsert_Click2;」'? –
這些點擊事件是否在其他地方被重用,以至於某處有一個只觸發一個事件的按鈕,而另一個只觸發另一個事件或其他事件?無論哪種方式,我會建議將這些事件的邏輯提取到他們自己的方法中。然後,每個事件方法可以只有一行調用適當的方法,然後如果您需要調用它們的另一個事件,則只需創建一個新的事件方法來調用您的兩個抽取方法。 – Chris