如何使用带有计时问题的IsPostBack for Update命令

时间:2014-03-23 13:00:13

标签: c# asp.net sql ispostback

我有asp.net detailsview将数据插入到付款分配表中,但是我想运行一个更新命令,将数据更新到s_transaction_enquiry表中,这是在C#中完成的。当我运行代码时,update命令首先在insert命令之前运行,因此不会将数据放入s_transaction_enquiry表。

我创建了update命令,以便在用户单击详细信息视图中的插入按钮时运行。 insert命令链接到detailview的Sql Data源。

我被告知我可以使用" IsPostBack" page_load中的属性但确定如何做到这一点,是否有人可以帮助我?

      protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            string conn = "";
            string sqlCOmmand = "UPDATE s_transaction_enquiry, payment_allocation SET s_transaction_enquiry.payment_amount = payment_allocation.payment_amount, s_transaction_enquiry.payment_ref = payment_allocation.payment_ref, s_transaction_enquiry.payment_received_date = payment_allocation.payment_received_date WHERE payment_allocation.s_invoice_numer = s_transaction_enquiry.s_invoice_number AND payment_allocation.account_number = s_transaction_enquiry.account_number";
            conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
            UpdateRow(conn, sqlCOmmand);
        }


    }

    public void UpdateRow(string connectionString, string insertSQL)
    {
        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {

            OleDbCommand command = new OleDbCommand(insertSQL);


            command.Connection = connection;

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }

从数据源插入语句:

  <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>" DeleteCommand="DELETE FROM [payment_allocation] WHERE [payment_ref] = ?" ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>" SelectCommand="SELECT * FROM [payment_allocation]" 
             InsertCommand="INSERT INTO [payment_allocation] ([payment_ref], [account_number], [account_ref], [allocate_date], [payment_amount], [payment_received_date], [s_invoice_numer]) VALUES (?, ?, ?, ?, ?, ?, ?)">

调用insert语句的按钮:

  <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert" Text="Create"/>

1 个答案:

答案 0 :(得分:0)

每次操作后都应关闭连接。你已经在&page; page_load&#39;上打开了一个连接。但是没有关闭它,并且因为page_load也会在每次回发时执行,这意味着每次单击按钮时,都会尝试再次打开连接而不关闭它。

还注意到page_loadbutton_click事件处理程序之前执行,因此每次单击按钮时都会打开连接而不关闭,并尝试在单击处理程序中再次打开它。

OleDbConnection是一个一次性对象,这意味着它实现了.dispose()函数,这也意味着它可以在using()语句中使用。

using()语句创建一个新对象并在之后处理它。 您在微软网站上的c#中打开/关闭/处理OleDbConnection有一个非常好的解释:OleDbConnection Class

基本上为了使它适应你的代码,你会想要做这样的事情, 为方便起见,首先将数据库处理放在一个单独的函数中,然后从page load ::

调用它
    protected void Page_Load(object sender, EventArgs e)
    {
        string conn = "";
        string sqlCOmmand= "inset blablabla into blablabla";
        conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();


        InsertRow(conn,sqlCOmmand);


    }


//taken from microsoft website
public void InsertRow(string connectionString, string insertSQL)
{
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        // The insertSQL string contains a SQL statement that 
        // inserts a new row in the source table.
        OleDbCommand command = new OleDbCommand(insertSQL);

        // Set the Connection to the new OleDbConnection.
        command.Connection = connection;

        // Open the connection and execute the insert command. 
        try
        {
            connection.Open();
            command.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        // The connection is automatically closed when the 
        // code exits the using block.
    }
}

好的,在这部分清楚之后,转到下一部分:

我假设您只想在页面加载时插入数据库?而不是每按一次按钮。因此,如果首次加载并且回发,则需要指示asp.net仅执行代码。您可能希望将page_load函数更改为:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string conn = "";
            string sqlCOmmand = "inset blablabla into blablabla";
            conn = ConfigurationManager.ConnectionStrings["Conn"].ToString();
            InsertRow(conn, sqlCOmmand);
        }
    }

对于你的点击处理程序,你应该创建另一个函数UpdateRow并使用using()语句执行相同的操作并将其放在你的点击处理程序中