简体   繁体   中英

OracleTransaction Update Gridview via Drop Down List

I am trying to use a drop down list that I have bound to a cell in a GridView to call an SQL UPDATE statement to change a record in the GridView itself. Here is the code below:

 <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>

And here is the code behind, this is where my try-catch block hangs my page at ExecuteNonQuery(). Is this the correct syntax of how to do an update? I disabled the AutoPostBack as you can see above but that didn't do anything for me anyway:

  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
        }

Why don't you use:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM