OracleTransaction通过下拉列表更新Gridview

时间:2012-06-15 18:02:33

标签: c# asp.net oracle gridview

我试图使用一个下拉列表,我已经绑定到GridView中的一个单元格来调用SQL UPDATE语句来更改GridView本身的记录。以下是代码:

 <asp:TemplateField HeaderText="Send To...">
                <ItemTemplate>
                    <asp:DropDownList ID="StatusDD" runat="server" AutoPostBack="false" OnSelectedIndexChanged="StatusDD_SelectedIndexChanged">
                        <asp:ListItem Value="A">Active</asp:ListItem>
                        <asp:ListItem Value="C">Complete</asp:ListItem>
                        <asp:ListItem Value="I">In Process</asp:ListItem>
                        <asp:ListItem Value="E">Error</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>

这里有代码,这是我的try-catch块在ExecuteNonQuery()上挂起我的页面。这是如何进行更新的正确语法吗?我已经禁用了AutoPostBack,如上所示,但对我来说没有做任何事情:

  protected void StatusDD_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        ToroGeneral toro = new ToroGeneral();
        string connString = toro.GetOracle1ConnectionString();

        using (OracleConnection conn1 = new OracleConnection(connString))
        {
            foreach (GridViewRow row in MMRGrid.Rows)
            {
                Control ctrl = row.FindControl("StatusDD") as DropDownList;
                if (ctrl != null)
                {
                    DropDownList ddl1 = (DropDownList)ctrl;
                    string qString = "";
                     // PreQuery work
                    DropDownList temp = new DropDownList();
                    temp = (DropDownList)row.FindControl("StatusDD");
                    string uKey = row.Cells[12].Text;
                    string tempStr = temp.SelectedValue.ToString().Trim();

                    if (ddl1.SelectedValue == "A")
                    {
                        qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'A' WHERE UNIQUEKEY = '" + uKey + "'";
                    }
                    else if (ddl1.SelectedValue == "E")
                    {
                        qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'E' WHERE UNIQUEKEY = '" + uKey + "'";
                    }
                    else if (ddl1.SelectedValue == "I")
                    {
                        qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'I' WHERE UNIQUEKEY = '" + uKey + "'";
                    }
                    else if (ddl1.SelectedValue == "C")
                    {
                        qString = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = 'C' WHERE UNIQUEKEY = '" + uKey + "'";
                    }
                    else
                    {
                        // Error
                    }

                    // Change the value of the record in the database.
                    conn1.Open();
                    OracleCommand cmd = conn1.CreateCommand();
                    OracleTransaction myTrans;
                    cmd.CommandText = qString;
                    myTrans = conn1.BeginTransaction(IsolationLevel.ReadCommitted);
                    cmd.Transaction = myTrans;

                    if (cmd.Connection.State == ConnectionState.Closed)
                    {
                        cmd.Connection.Open();
                    }

                    try
                    {
                        cmd.ExecuteNonQuery();
                        myTrans.Commit();
                    }
                    catch
                    {
                        myTrans.Rollback();
                    }


                }  // if
            } // foreach
        }

1 个答案:

答案 0 :(得分:0)

你为什么不用:

string updateSql = "UPDATE MATERIALMOVEREQUEST SET PROCESS_FLAG = :parameter1 WHEREUNIQUEKEY = :parameter2";"

OracleCommand cmd = new OracleCommand(updateString, conn1);
cmd.BindByName = true;
cmd.Parameters.Add("parameter1", Convert.ToChar(ddl1.SelectedValue);
cmd.Parameters.Add("parameter2", uKey);
myTrans = conn1.BeginTransaction(IsolationLevel.ReadCommitted);
cmd.Transaction = myTrans;

try
{
     cmd.ExecuteNonQuery();
     myTrans.Commit();
}
catch
{
     myTrans.Rollback();
}