Hello,

I need to transliterate the names of employees that are fetched from my database and write the transliterated values back into the database... For this I need to run a javascript code in a loop for as many no of records as in the database....

The code I am currently using is

//CODE BEHIND

Dim cmd As New SqlCommand("select empname from tbl_emp", conobject)
            Dim dr As SqlDataReader
            conobject.Open()
            dr = cmd.ExecuteReader
            Dim i As Integer = 1

            While dr.Read
             Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "b" & i, "<script>conurdu('" & dr(0) & "');</script>")
                Response.Write (txt_result.Text)
                i = i + 1
            End While
            conobject.Close()

//JAVASCRIPT

        <script type="text/javascript">

        function conurdu(fieldname) {
        google.language.transliterate([fieldname], "en", "ur", function(result) {

            if (!result.error) 
            {
             if (result.transliterations && result.transliterations.length > 0 && result.transliterations[0].transliteratedWords.length > 0) 
                {
                    document.getElementById(<%=txt_result.ClientID%>).value = result.transliterations[0].transliteratedWords[0] ;
                }
            }
        });
    }
</script>

For example, there are five records in the database, only the last one gets written in the txt_result textbox...
Any ideas???

Member Avatar for stbuchok

Wouldn't it be better to make an AJAX call for the list of names and then loop through for translation and once the translation is done, make another AJAX call to update?

Also on line 10, you are missing quotes for the id that you are looking for.

try this instead: document.getElementById('<%=txt_result.ClientID%>')

One last thing, which is about you actual issue. You are only getting the last one because each time you write to txt_result, it is overriding the last value. use += instead of = on line 10.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.