Gridview 数据源中的 UpdateCommand 返回错误 ORA-01036:非法变量名称/编号
我有一个 GridView,可以动态从 Oracle 数据库中提取数据。
<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>
它与 SELECT/INSERT/DELETE 语句一起工作得很好,但是当涉及到 UPDATE 语句时,它会返回错误 ORA-01036: 非法变量名/数字
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()));
}
RowUpdate 的代码并不重要。我会编写任何更新语句,它都会返回相同的错误。 各位有什么想法吗?先谢谢你了!
示例查询:
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" };
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" };
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 dataKeyNames 会自动作为参数添加到 updateCommand 中,因此 command_text 和 dataSource 之间的参数数量不会相等。
您需要在更新之前删除它们
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
我家里没有开发环境设置,所以无法检查我的建议。看看将其放在 sqlDataSource 上是否适合您。 ConflictDetection="OverwriteChanges"
示例:
更新:
另一种可能是您的某个参数名称太长。长度限制为 31 或 32 个字符。过去,当我动态创建参数列表时,我只是给相应的参数名称一个通用名称(例如 Param[x]),并通过循环递增参数名称(例如 Param1、Param2、Param3 等) on 对于语句中需要的每个参数)。其他一些建议是验证标头中使用的参数名称是否有效(即没有空格)。
我最后的想法是 SqlDataSource 不兼容。您使用的是 System.Data.OracleClient 还是 Oracle.DataAccess.Client?尝试使用不同的提供商,看看是否有影响。
请参阅这些文章以获得更多帮助。这就是我所得到的一切。对不起!
ASP.NET 中的 Oracle.DataAccess.Client 和 SqlDataSource
gridview 上的更新产生ORA-01036: 非法变量名/编号
更新:
按照上面的建议,尝试切换您的数据提供商,在这种情况下,请尝试 ODP.NET。您必须安装它。它不是系统库。将 ODBC 与 Oracle 结合使用可能是另一种选择。无论如何,您可能别无选择,只能不使用 System.Data.OracleClient 的任何内容。
已修复! Oracle 11G 和 SqlDataSource:丑陋的“ORA-01036:非法变量名/编号”问题
适用于 .NET 的 Oracle 数据提供程序
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:
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
我找到了解决问题的方法。不是最好的,但可以接受。
我使用 OracleCommand 而不是 DS.UpdateCommand,只是忽略有关设置 UpdateCommand 的异常。
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.