Populate textbox from database using ajax

TonyReba
TonyReba used Ask the Experts™
on
Hi, I have the following scenario: An SQL 2000 database with a table containing the columns UserID and UserName. A webpage with TextBox1 and TextBox2.

I need to use  AJAX  or Jquey to accomplish the following: When I type the patient ID  in txtPatientId textbox and press the Tab key, txtMRN will populate with the corresponding MRN number.

How do implement this this is what I  have but is not working.


[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetDynamicContent(string contextKey)
    {
        string outputString = string.Empty;
        string strConn = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        SqlConnection dbConnection = new SqlConnection(strConn);
        
        SqlCommand dbCommand = new SqlCommand
        ("SELECT TPM300_PAT_VISIT.vst_ext_id AS VisitID,  FROM TABLE ......"

  return outputString;

Open in new window

<asp:TextBox ID="txtMRN" runat="server" MaxLength="15"></asp:TextBox>
            <asp:DynamicPopulateExtender ID="txtMRN_DynamicPopulateExtender" runat="server" 
                Enabled="True" PopulateTriggerControlID="btn_Go" ServiceMethod="GetDynamicContent" 
                TargetControlID="txtMRN" ContextKey="txtPatientId">
            </asp:DynamicPopulateExtender>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
@TOnyReba,

On the server :
You need a to create a web service to receive the request from the server.
Your web service return the MRN number

[WebMethod]
  public static string GetMRN(string PatientID)
  {
        string outputString = string.Empty;
        string strConn = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        SqlConnection dbConnection = new SqlConnection(strConn);
        
        SqlCommand dbCommand = new SqlCommand
        ("SELECT TPM300_PAT_VISIT.vst_ext_id AS VisitID,  FROM TABLE ......WHERE .... = " + PatientID      
// ... get MRN

        return strMRN;
   }

Open in new window


On the client side just call
<script type="text/javascript">
function getMRN() {
    var PatientID = $get('<%= txtPatientId.ClientID %>');
    if(PatientID.value == '') return;
    PageMethods.GetServerData(PatientID.value, OnSucceeded, OnFailed);
}
 
function OnSucceeded(result, userContext, methodName)  {
    var MRN = $get('<%= txtMRN.ClientID %>');
    MRN.value = result;
}
 
function OnFailed(error, userContext, methodName) {
   alert(error.get_message());
}
</script>

Open in new window

Author

Commented:
what do i use forthe patientID parameter

this are my textboxes
<asp:TextBox ID="txtPatientId" runat="server" MaxLength="15"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ErrorMessage="*" ControlToValidate="txtPatientId"></asp:RequiredFieldValidator>
            <asp:Button ID="btn_Go" runat="server" onclick="btn_Go_Click" Text="Lookup" 
                UseSubmitBehavior="False" CausesValidation="False"        />
        </td>
        <td>
            MRN:
            <asp:TextBox ID="txtMRN" runat="server" MaxLength="15" ReadOnly="True"></asp:TextBox>
            <asp:DynamicPopulateExtender ID="txtMRN_DynamicPopulateExtender" runat="server" 
                Enabled="True" PopulateTriggerControlID="btn_Go" ServiceMethod="GetDynamicContent" 
                TargetControlID="txtMRN">
            </asp:DynamicPopulateExtender>

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
>what do i use forthe patientID parameter

>>>>When I type the patient ID  in txtPatientId textbox and press the Tab key, txtMRN will populate with the corresponding MRN number.

Check line 3 : var PatientID = $get('<%= txtPatientId.ClientID %>');
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Author

Commented:
Where is StrnMRN declared?


[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    public static string GetMRN(string PatientID)
  {
        string outputString = string.Empty;
        string strConn = ConfigurationManager.ConnectionStrings[""].ConnectionString;
        SqlConnection dbConnection = new SqlConnection(strConn);
            
        SqlCommand dbCommand = new SqlCommand
        ("SELECT TPM300_PAT_VISIT.vst_ext_id AS VisitID, TPM300_PAT_VISIT.med_rec_no AS MRN  WHERE  TPM300_PAT_VISIT.vst_ext_id = '" + PatientID + "'", dbConnection);


     -->>>>   return strMRN;?????????????

   }



}

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
it's the MRN value returned by your query (the missing part)

Author

Commented:
can you help with tje declaration I am kinda lost a this point
leakim971Multitechnician
Top Expert 2014

Commented:
SqlDataReader rdr  = cmd.ExecuteReader();
String strMRN;
if(rdr.Read()) {
    strMRN = rdr[1];
}
return strMRN;

Open in new window

Author

Commented:
thanks for helping me so far
 I get this error

  strMRN = rdr[1];

cannot convert object to string..

do I need to cast?
leakim971Multitechnician
Top Expert 2014

Commented:
>do I need to cast?

What about :

 strMRN = rdr.GetString(1);

Author

Commented:
thi line shows unassigned local variable

 return strMRN;
leakim971Multitechnician
Top Expert 2014

Commented:
Did you get a results from you query? Check it first.

Author

Commented:
ok this doesnt give me errors,,,
 while (rdr.Read())
        {
     
           string strMRN = rdr.GetString(1);
        }
        return sb.ToString();

    }
now how do I call this code , what does it trigers it on my aspx ?

Multitechnician
Top Expert 2014
Commented:
try your web service alone and confirm it run fine

the next step is to call it with ajax/page method

>now how do I call this code , what does it trigers it on my aspx ?

It seems you don't want to look my links in your other post...

OK, back to ID:35778108 > On the client side just call

Author

Commented:
I tested the query on separate page using sql datasource and pulls out the data fine, how do I test the web servicer?
leakim971Multitechnician
Top Expert 2014

Commented:
in visual studio, go the web service page tab and run it

Author

Commented:
The web service is on the same page default.cs

not as a separate service...

I just found out the  the Script manager didnt have the

 EnablePageMethods="True   ....

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial