C# 从onclick c调用2个方法#
我一直试图通过点击按钮来传递2个SQL查询,所以这是我一直试图做的,如下所示,但它不起作用…请帮助。。。提前谢谢 这就是前面的代码C# 从onclick c调用2个方法#,c#,sql,ms-access-2013,C#,Sql,Ms Access 2013,我一直试图通过点击按钮来传递2个SQL查询,所以这是我一直试图做的,如下所示,但它不起作用…请帮助。。。提前谢谢 这就是前面的代码 背后的代码如下 protected void btnWedRecInsert_Click1(object sender, EventArgs e) { string Function = ddlFunction.Text; string FunctionDate = txtFunctionDate.Text; string Functi
背后的代码如下
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();
}
}
将onClick事件更改为btn{NameOfYourButton}单击,然后单击
public void btn_{NameOfYourButton}Click() {
btnWedRecInsert_Click1();
btnWedRecInsert_Click2();
}
将onClick事件更改为btn{NameOfYourButton}单击,然后单击
public void btn_{NameOfYourButton}Click() {
btnWedRecInsert_Click1();
btnWedRecInsert_Click2();
}
您应该将标记更改为:
<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click" />
为了便于阅读,您可能应该将insert函数的名称更改为
insertWedding1();
insertWedding2();
到目前为止,我看不出这两个InsertFunction之间有什么区别。也许你以后会换衣服,但它们很相似。用它们制作一个带有参数的函数,这样你就不必写那么多代码了
您应该将标记更改为:
<asp:Button ID="btnWedRecInsert" runat="server" Text="Insert" OnClick="btnWedRecInsert_Click" />
为了便于阅读,您可能应该将insert函数的名称更改为
insertWedding1();
insertWedding2();
到目前为止,我看不出这两个InsertFunction之间有什么区别。也许你以后会换衣服,但它们很相似。用它们制作一个带有参数的函数,这样你就不必写那么多代码了
但这显示了两个不同的按钮点击事件。。。。不是1。。当您1使用
OnClick=“btnwedrecincert\u Click1;btnwedrecincert\u Click2;”时抛出任何错误时发生了什么?
?这些单击事件是否在其他地方重复使用,以致某个地方有一个按钮仅触发一个事件,另一个按钮仅触发另一个事件或其他事件?无论哪种方式,我都建议将这些事件的逻辑提取到它们自己的方法中。然后,您的每个事件方法都可以有一行调用相应的方法,如果您需要另一个调用它们的事件,则只需创建一个新的事件方法来调用两个提取的方法。是的,我在编译时遇到一个错误,但我需要的是一次单击按钮执行两个查询,如果需要,请显示另一种方式wont workUse@Peter Morris post,使用OnClick=“{fuctionName};”
调用它,但它显示了两个不同的按钮单击事件。。。。不是1。。当您1使用OnClick=“btnwedrecincert\u Click1;btnwedrecincert\u Click2;”时抛出任何错误时发生了什么?
?这些单击事件是否在其他地方重复使用,以致某个地方有一个按钮仅触发一个事件,另一个按钮仅触发另一个事件或其他事件?无论哪种方式,我都建议将这些事件的逻辑提取到它们自己的方法中。然后,您的每个事件方法都可以有一行调用相应的方法,如果您需要另一个调用它们的事件,则只需创建一个新的事件方法来调用两个提取的方法。是的,我在编译时遇到一个错误,但我需要的是一次单击按钮执行两个查询,如果需要,请显示另一种方式习惯于使用@Peter Morris post,使用OnClick=“{fuctionName};”来调用它
非常感谢,差异在于我插入了不同文本框中的值,如一天的接待和另一天的婚礼。非常感谢,差异在于我插入了不同文本框中的值,如一天的接待和另一天的婚礼