In ASP.net 4.5 C# using AJAX UpdatePanel,
I am trying to have textbox outputOwnDept fill, from a database, based on the text entered in textbox inputOwner. Also, I am trying to have this search triggered when a user starts typing inside inputOwner.
What am I doing wrong?
Here is my aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="col-lg-6">
<div class="well bs-component">
<fieldset>
<legend>Ownership and Support</legend>
<div class="form-group">
<label for="inputOwner" class="col-lg-2 control-label">IT Owner's Network ID:</label>
<div class="col-lg-10">
<asp:TextBox ID="inputOwner" runat="server" OnTextChanged="inputOwnerTextChanged"></asp:TextBox>
</div>
</div>
</fieldset>
</div>
</div>
<div class="col-lg-6">
<div class="well bs-component">
<fieldset>
<div class="form-group">
<label for="outputOwnDept" class="col-lg-2 control-label">Owner's Department:</label>
<div class="col-lg-10">
<asp:TextBox ID="" runat="server" ReadOnly="true"></asp:TextBox>
</div>
</div>
</fieldset>
</div>
</div>
</ContentTemplate>
<triggers><asp:AsyncPostBackTrigger ControlID="inputOwner" EventName="TextChanged" /></triggers>
</asp:UpdatePanel>
Here is my code behind:
public void inputOwnerTextChanged(object sender, EventArgs e)
{
DataTable owndept = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["PEOPLESOFT"].ConnectionString))
{
try
{
string NetworkID = inputOwner.Text;
SqlDataAdapter adapter = new SqlDataAdapter("select max(vw.DEPTNAME) as DEPTNAME from sysadm.PS_PH_ACTIVE_EE_VW vw where vw.NETWORK_ID like '%" + NetworkID + "%';", con);
adapter.Fill(owndept);
outputOwnDept.Text = (string)DataBinder.Eval(owndept, "DEPTNAME");
outputOwnDept.DataBind();
}
catch (Exception ex)
{
// Handle the error
}
}
}
The problem was: -I needed autopostback = "True"
-since I was trying to connect to an oracle db I needed the below added to my code behind: using Oracle.ManagedDataAccess.Client; using Oracle.ManagedDataAccess.Types;
-I also needed to use different functions to connect to an oracle database
-I needed to change my connection string in the Web.config for Oracle
-Also, I needed to remove the ';' from my SQL line for Oracle
-aspx:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="col-lg-10">
<div class="well bs-component">
<fieldset>
<legend>Ownership and Support</legend>
<div class="form-group">
<label for="inputOwner" class="col-lg-2 control-label">IT Owner:</label>
<div class="col-lg-10">
<asp:TextBox ID="inputOwner" runat="server" OnTextChanged="inputOwnerTextChanged" autopostback="True" placeholder="IT Owner's Network ID"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label for="outputOwnDept" class="col-lg-2 control-label">Owner's Department:</label>
<div class="col-lg-10">
<asp:Label ID="outputOwnDept" runat="server" Text="" BackColor="#F0F0F0"></asp:Label>
</div>
</div>
</fieldset>
</div>
</div>
</ContentTemplate>
<triggers><asp:AsyncPostBackTrigger ControlID="inputOwner" EventName="TextChanged" /></triggers>
</asp:UpdatePanel>
code-behind:
protected void inputOwnerTextChanged(object sender, EventArgs e)
{
DataTable owndept = new DataTable();
using (OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["EMPLOYMENTEXP.HRPRD"].ConnectionString))
{
try
{
string NetworkID = inputOwner.Text;
OracleDataAdapter adapter = new OracleDataAdapter("select max(vw.DEPTNAME) as DEPTNAME from sysadm.PS_PH_ACTIVE_EE_VW vw where vw.NETWORK_ID = '" + NetworkID + "'", con);
adapter.Fill(owndept);
outputOwnDept.Text = owndept.Rows[0].Field<String>(0);
}
catch (SqlException ex)
{
outputOwnDept.Text = ex.ToString();
}
}
}
Web.config:
<add name="EMPLOYMENTEXP.HRPRD" connectionString="DATA SOURCE=host:port/database;USER ID=xxx;PASSWORD=xxx;" providerName="Oracle.ManagedDataAccess.Client" />