Avatar of TonyReba
TonyReba
Flag 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

AJAXASP.NET

Avatar of undefined
Last Comment
TonyReba

8/22/2022 - Mon
leakim971

@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

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

leakim971

>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 %>');
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
TonyReba

ASKER
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

leakim971

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

ASKER
can you help with tje declaration I am kinda lost a this point
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
leakim971

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

Open in new window

TonyReba

ASKER
thanks for helping me so far
 I get this error

  strMRN = rdr[1];

cannot convert object to string..

do I need to cast?
leakim971

>do I need to cast?

What about :

 strMRN = rdr.GetString(1);
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TonyReba

ASKER
thi line shows unassigned local variable

 return strMRN;
leakim971

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

ASKER
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 ?

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
leakim971

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TonyReba

ASKER
I tested the query on separate page using sql datasource and pulls out the data fine, how do I test the web servicer?
leakim971

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

ASKER
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   ....
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23