Link to home
Create AccountLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

asked on

Populate textbox from database using ajax

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

Avatar of leakim971
leakim971
Flag of Guadeloupe image

@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

Avatar of TonyReba

ASKER

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

>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 %>');
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

it's the MRN value returned by your query (the missing part)
can you help with tje declaration I am kinda lost a this point
SqlDataReader rdr  = cmd.ExecuteReader();
String strMRN;
if(rdr.Read()) {
    strMRN = rdr[1];
}
return strMRN;

Open in new window

thanks for helping me so far
 I get this error

  strMRN = rdr[1];

cannot convert object to string..

do I need to cast?
>do I need to cast?

What about :

 strMRN = rdr.GetString(1);
thi line shows unassigned local variable

 return strMRN;
Did you get a results from you query? Check it first.
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 ?

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I tested the query on separate page using sql datasource and pulls out the data fine, how do I test the web servicer?
in visual studio, go the web service page tab and run it
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   ....