Updating Oracle Table using Stored Procedure from C# Odd bug
I am using a stored procedure to Insert a new user or update their existing information if the user already exists in the Database.
I am grabbing 3 of the Parameters (User name, first name, and last name) from Active Directory and using 4 textboxes to grab the rest (PI Code, phone, email, address).
<asp:TextBox ID="TXT_PI_CODE" runat="server" style="position: absolute; left:120px; top:100px;"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator_PI_CODE" runat="server" ErrorMessage="" ControlToValidate="TXT_PI_CODE"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TXT_EMAIL" runat="server" style="position: absolute; left:120px; top:140px;"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator_EMAIL" runat="server" ErrorMessage="" ControlToValidate="TXT_EMAIL"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator_EMAIL" runat="server" ErrorMessage="Invalid Email Format." ControlToValidate="TXT_EMAIL" style="position: absolute; top:140px; left:300px;" ValidationExpression="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?"></asp:RegularExpressionValidator>
<br />
<asp:TextBox ID="TXT_PHONE" runat="server" style="position: absolute; left:120px; top:180px;"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator_PHONE" runat="server" ErrorMessage="" ControlToValidate="TXT_PHONE"></asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TXT_ADDRESS" runat="server" style="position: absolute; left:120px; top:220px;" Width="200"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator_ADDRESS" runat="server" ErrorMessage="" ControlToValidate="TXT_ADDRESS"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="LBL_REQUIRED" runat="server" Text="*Required Fields" CssClass="labels" style="position: absolute; top:260px;" ForeColor="#FF9900"></asp:Label>
<asp:Button ID="BTN_SUBMIT" runat="server" Text="Submit"
style="position: absolute; top:300px; left:120px;" Width="100" Height="40"
OnClick="BTN_SUBMIT_Click"/>
My Stored Procedure is as follows:
create or replace
PROCEDURE SP_ADD_USER
(
p_USER_NAME IN T_USER.USER_NAME%TYPE,
p_FIRST_NAME IN T_USER.FIRST_NAME%TYPE,
p_LAST_NAME IN T_USER.LAST_NAME%TYPE,
p_PI_CODE IN T_USER.PI_CODE%TYPE,
p_EMAIL IN T_USER.EMAIL%TYPE,
p_PHONE IN T_USER.PHONE%TYPE,
p_ADDRESS IN T_USER.ADDRESS%TYPE
)
AS
BEGIN
INSERT INTO T_USER (USER_ID, USER_NAME, FIRST_NAME, LAST_NAME, PI_CODE, EMAIL, PHONE, ADDRESS)
VALUES (S_USER.NEXTVAL, p_USER_NAME, p_FIRST_NAME, p_LAST_NAME, p_PI_CODE, p_EMAIL, p_PHONE, p_ADDRESS);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
UPDATE T_USER
SET
PI_CODE = p_PI_CODE,
EMAIL = p_EMAIL,
PHONE = p_PHONE,
ADDRESS = p_ADDRESS
WHERE LOWER(USER_NAME) = p_USER_NAME;
COMMIT;
END;
And finally my C# looks like this:
protected void BTN_SUBMIT_Click(object sender, EventArgs e)
{
//Retrieve Username, firstname, and last name from AD.
string STR_USER_ID = ADQuery.ExtractUserName(User.Identity.Name.ToString());
string STR_FIRST_NAME = ADQuery.GetADValue(STR_USER_ID, "givenName");
string STR_LAST_NAME = ADQuery.GetADValue(STR_USER_ID, "SN");
string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using(OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
OracleCommand ora_cmd = new Or开发者_运维技巧acleCommand("SP_ADD_USER", connection);
ora_cmd.BindByName = true;
ora_cmd.CommandType = CommandType.StoredProcedure;
ora_cmd.Parameters.Add("p_USER_NAME", OracleDbType.Varchar2, STR_USER_ID.ToLower(), ParameterDirection.Input);
ora_cmd.Parameters.Add("p_FIRST_NAME", OracleDbType.Varchar2, STR_FIRST_NAME, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_LAST_NAME", OracleDbType.Varchar2, STR_LAST_NAME, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_PI_CODE", OracleDbType.Varchar2, TXT_PI_CODE.Text, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_EMAIL", OracleDbType.Varchar2, TXT_EMAIL.Text, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_PHONE", OracleDbType.Varchar2, TXT_PHONE.Text, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_ADDRESS", OracleDbType.Varchar2, TXT_ADDRESS.Text, ParameterDirection.Input);
ora_cmd.ExecuteNonQuery();
}
Response.Redirect("Default.aspx");
}
The insert portion works fine as a new user, however when attempting to update the information in the same text boxes nothing has updated on the database after the postback. However the oddity is that if I change my C# to
ora_cmd.Parameters.Add("p_PI_CODE", OracleDbType.Varchar2, TXT_EMAIL.Text, ParameterDirection.Input);
ora_cmd.Parameters.Add("p_EMAIL", OracleDbType.Varchar2, TXT_EMAIL.Text, ParameterDirection.Input);
where both of those parameters are populated from the same textbox it will update the PI_CODE
field on the database with what was originally in the email address textbox, but the email address will remain unchanged on the database.
I don't know if there is some bug I am missing or my syntax is off? I've made sure my parameter orders are correct, or what I believe is correct. Anyone seen this before?
Figured out how to fix the textboxes ignoring the changes. Placing the textbox populate code inside of an if(!IsPostback) allowed the program to use the newly updated values in the textbox instead of the old ones. Hope this helps someone.
精彩评论