I am making a website (noob at this) and am trying to add search results to three divs. But problem arises when adding text to divs from database. It fetches correctly. but I want to convert a string (total three divs, id’s respectively being div1, div2, div3. string being “div” + DivCount.ToString, DivCount being 0 (which loops and +1 is added every loop) into command. Here’s the code, any other method would be appreciated too. Thanks. Code’s below, btw.
Default.aspx.vb
Imports Microsoft.VisualBasic
Imports System.Data.SqlServerCe
Partial Class Pages_Default
Inherits System.Web.UI.Page
'Project Variables
Dim ProjectDirectory As String = AppDomain.CurrentDomain.BaseDirectory.ToString
'ConnectAndAddItemsToDropdownList Dim
Dim SQLCommandString As String
Dim SQLConnectionString As String = "Data Source=" + ProjectDirectory + "App_Data\MainDatabase.sdf; Persist Security Info=False;"
Dim SQLConnection As New SqlCeConnection(SQLConnectionString)
Dim SQLAdapter As SqlCeDataAdapter
Dim SQLReader As SqlCeDataReader
Dim Executor As New MSScriptControl.ScriptControl
Dim DivCount As Integer = 0
Dim DivCurrent As String
Public Sub ConnectAndAddItemsToDropdownList()
DropDownList1.Items.Clear()
SQLConnection.Open()
SQLCommandString = "SELECT BookName FROM [Anthony Horowitz] WHERE (BookName LIKE '" + SearchTextBox.Text + "%')"
Dim SQLCommand As New SqlCeCommand(SQLCommandString, SQLConnection)
SQLReader = SQLCommand.ExecuteReader()
While SQLReader.Read()
DivCount = +1
DivCurrent = "div" + DivCount.ToString
Executor.Language = "VBScript"
Executor.Eval(DivCurrent.ToString + ".InnerText = SQLReader(""Bookname"")")
End While
SQLConnection.Close()
End Sub
Protected Sub SearchTextChanged(sender As Object, e As EventArgs) Handles SearchTextBox.TextChanged
ConnectAndAddItemsToDropdownList()
End Sub
End Class
Default.aspx
<%@ Page Language="VB" CodeFile="Default.aspx.vb" Inherits="Pages_Default" AutoEventWireUp="False"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body id="MainPage" runat="server">
<form id="form1" runat="server">
<asp:TextBox ID="SearchTextBox" Text="Search for a book here..." runat="server" AutoPostBack="true" OnTextChanged="SearchTextChanged"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="17px" Width="105px"></asp:DropDownList>
<div id="div1" runat="server">Nothing Interesting</div>
<div id="div2" runat="server">Nothing Interesting</div>
<div id="div3" runat="server">Nothing Interesting</div>
</form>
</body>
</html>
The result above is “object required: ‘div1′”
You can’t get data like that and put it in elements, for three reasons:
Put server controls in the markup where you want the text:
Get references to the controls and put in an array so that you can easily access them:
Now you can put the data in the controls: