简体   繁体   中英

UpdateCommand in Gridview DataSource returns an error ORA-01036: illegal variable name/number

I have a GridView that pulls the data from an Oracle DataBase dynamically.

<asp:GridView ID="gvData" runat="server" DataSourceID="DS" CellPadding="4" 
        ForeColor="#333333" AllowSorting="True" AllowPaging="True" 
        ShowFooter="True" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" AutoGenerateSelectButton="True"
        OnRowDataBound="gvData_RowDataBound"
        OnSelectedIndexChanged="gvData_SelectedIndexChanged" 
        OnRowUpdating="gvData_RowUpdating" OnRowUpdated="gvData_RowUpdated"
        OnRowDeleting="gvData_RowDeleting" OnRowDeleted="gvData_RowDeleted">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" 
            ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#CCCCCC" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

    <asp:SqlDataSource ID="DS" runat="server">
    </asp:SqlDataSource>

It works fine with SELECT/INSERT/DELETE statements, however when it comes to UPDATE statement, it returns an error ORA-01036: illegal variable name/number

protected void gvData_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    double number;
    List<string> text = new List<string>();
    List<string> pkParam = new List<string>();
    TextBox tb = new TextBox();
    LinkButton lb = new LinkButton();

    for (int i = 1; i < gvData.HeaderRow.Cells.Count; i++)
    {
        try 
        {
            lb = (LinkButton)gvData.HeaderRow.Cells[i].Controls[0];
            tb = (TextBox)gvData.Rows[e.RowIndex].Cells[i].Controls[0];

            if (tb.Text.Length > 0 && !Settings.PK.Contains(lb.Text))
                if(Double.TryParse(tb.Text.ToText(), out number))
                    text.Add(lb.Text + " = " + number);
                else
                    text.Add(lb.Text + " = " + "'" + tb.Text.ToText() + "'");
        }
        catch { }
    }

    foreach (string pk in Settings.PK)
    {
        pkParam.Add(pk + " = :" + pk);
        DS.UpdateParameters.Add(
            new Parameter(pk, TypeCode.String, e.Keys[pk].ToString()));
    }

    DS.UpdateCommand = string.Format("UPDATE {0} SET {1} WHERE {2}",
        Settings.TABLE, string.Join(",", text.ToArray()), 
            string.Join(" AND ", pkParam.ToArray()));
 }

The code of RowUpdate doesn't really matter. I would write ANY update statement and it would return the same error. Any ideas, folks? Thank you beforehands!

Sample query:

DS.UpdateCommand = "UPDATE ATDB.CTD_PROBLEM_EDIT_V SET CUTTING_COMMENTS = 'rrr'";

PK:

public static string[] PK = new string[] { "PROBLEM_DEPTH_MD", "API", "BUS_AREA_ID" };

Because dataKeyNames will be added as Parameters to updateCommand automatically,so the parameters count will not be equal between the command_text and the dataSource. You need to remove them before do update

I don't have a dev environment setup at home, so I can't check my suggestion. See if the placing this on your sqlDataSource works for you. ConflictDetection="OverwriteChanges"

Example:

<asp:SqlDataSource ID="DS" ConflictDetection="OverwriteChanges" runat="server">
</asp:SqlDataSource>

UPDATE:

Another possibility is that one of your parameter names is too long. The limit is 31 or 32 characters in length. In the past when I was dynamically creating a parameter list, I would just give the corresponding parameter name a generic name (eg. Param[x]) and increment the parameter name with the loop (eg. Param1, Param2, Param3, and so on for each parameter that needed to be in the statement). Some other suggestions would be to verify that your parameter names being used from your header are valid (ie. no spaces).

My last thought would be that the SqlDataSource is not compatible. Are you using System.Data.OracleClient OR Oracle.DataAccess.Client? Try using a different provider to see if that makes a difference.

See these articles for additional help. That's about all I got. Sorry!

Oracle.DataAccess.Client and SqlDataSource in ASP.NET

update on gridview produces ORA-01036: illegal variable name/number

UPDATE: As suggested above, try to switch your data provider, in this case, try ODP.NET. You will have to install it. It is not a system library. Looking to use ODBC with Oracle may be another option. In any case, you probably are left with no options but to not use anything with System.Data.OracleClient.

Fixed: Oracle 11G and SqlDataSource: The ugly 'ORA-01036: illegal variable name/number' Issue

Oracle Data Provider for .NET

I found my way around the problem. Not the best one but acceptable. Instead of DS.UpdateCommand I use OracleCommand simply ignoring Exception about setting UpdateCommand.

protected void gvData_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    if (e.Exception != null)
        e.ExceptionHandled = true;
}

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