0

I am trying to make a simple program to put data from a database into a javascript array and then display one result in a textbox. Here is what I have so far

        Dim cmd As New IfxCommand("select first 20 fname from table", conn)
    Dim reader As IfxDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
    Dim i As Integer = 0
    While reader.Read()
        ClientScript.RegisterArrayDeclaration("Names", "'" & reader("fname") & "'")
        i += 1


    End While
    Dim cs As StringBuilder = New StringBuilder()
    cs.Append("<script type=""text/javascript\""> function DoIt() {")
    cs.Append("var TheTextBox = document.getElementById(""TextBox1"");")
    cs.Append("TheTextBox.value = Names[0];")
    cs.Append("script>")
    TextBox1.Text = cs.ToString

Here is the asp

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<p>
    <asp:Button ID="Button1" runat="server" Text="Button" onclick = "DoIt"/>
</p>
</form>
</body>
</html>

I am not sure what else I am supposed to do with this.

2
  • Is there any reason why you don't use ASP.NET to display the values in a TextBox? Commented Jun 20, 2012 at 13:25
  • eventually I want to iterate through an array but if I did it through asp it would refresh the page everytime Commented Jun 20, 2012 at 13:27

1 Answer 1

1

So you want to display the first name of your selected 20 in the TextBox initially and maybe lazy-load the other from client side via javascript?

Try this:

Using cmd = New IfxCommand("select first 20 fname from table", conn)
    Dim tbl = New DataTable
    Using reader = cmd.ExecuteReader()
        tbl.Load(reader)
        If tbl.Rows.Count <> 0 Then
            Dim allNames = From row In tbl
                           Select row.Field(Of String)("fname")
            TextBox1.Text = allNames.First
            ' embed names with quotes and separate the strings by comma '
            Dim embeddedNames = From name In allNames Select String.Format("'{0}'", name)
            ClientScript.RegisterArrayDeclaration("Names", String.Join(",", embeddedNames))
        End If
    End Using
End Using

Now you only need to implement the javascript function that switches the names if necessary. You can use String split then:

var allNames = documentGetElementById("Names").value.split(","); 
Sign up to request clarification or add additional context in comments.

6 Comments

i'm getting an error on the clientscript line saying that it can't be converted to 1 dimensional array of string
@user867621: So all works on serverside and you also have already implemented the clientside functionality to iterate the names? Can i see it? Have you debugged javascript (or at least took a look at the source) to see what happens?
i meant it won't let me compile because this line ClientScript.RegisterArrayDeclaration("Names", String.Join(",", allNames)) It has the squigglies under allNames saying what the first comment said
@user867621: Oh yes, i haven't embedded the names with single quotes('John','James','Jim') which is necessary for javascript string-arrays. I've edited my answer accordingly.
it still won't compile.... Error 1 Value of type 'System.Data.EnumerableRowCollection(Of String)' cannot be converted to '1-dimensional array of String'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.