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" };
I found my way around the problem. Not the best one but acceptable.
Instead of
DS.UpdateCommandI use OracleCommand simply ignoring Exception about setting UpdateCommand.