从onclick调用2个方法c#

时间:2016-07-18 10:51:06

标签: c# sql ms-access-2013

我一直试图通过点击按钮一次传递2个SQL查询,所以这就是我一直在尝试做的事情,如下图所示,但它不起作用....请帮助...提前感谢

所以这是前面的代码

<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 个答案:

答案 0 :(得分:2)

您应该将标记更改为:

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

将您的代码隐藏更改为:

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

为了便于阅读,您可以将插入函数的名称更改为

insertWedding1();
insertWedding2();

截至目前,我看不出两个插件功能之间的任何区别。 mayby你会改变theese,但他们非常相似。如何用参数创建一个函数,这样你就不必编写那么多代码了:)例如。

insertWedding(object paramWedding1);
insertWedding(object paramWedding2);

答案 1 :(得分:1)

将onClick事件更改为btn_ {NameOfYourButton}点击然后

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