Populating a Form's TextBoxes from a DB Query

At times, we need to populate TextBoxes in a Form with Data received from a query. This form data can then be used to update the table or pretty much anything else you wanted to do, at that point.

This sample uses the SQL Server flavor of the Northwind Databse to populate a listbox with the last names of employees (EmployeeID is the Value field for the listbox). Based on the selection from that Listbox, the user clicks the Edit Record button to populate the form fields. The Textbox population takes place by using Variables generated by a DataReader, much like in the Classic ASP days.

The Update button is for show only – no actual updating is covered in this sample.

To get this to work on your system, just copy the code below into a new page, change your connection string information.

To see a the second part of this sample, with full editing capabilities, go to:
Updating/Editing a Chosen DataBase Record

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<html>
<head>
<meta name="GENERATOR" Content="ASP Express 2.1">
<title>Untitled</title>
<script language="VB" runat="server">
Dim intWdth, intID as integer
Dim strFirst, strLast, strTitle as string
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=Northwind"
Dim sql as string = "Select EmployeeID, FirstName, LastName from Employees"
Dim conn as New SQLConnection(strConn)
Dim Cmd as New SQLCommand(sql, conn)
Dim objDR as SQLDataReader
conn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
list1.datasource=objDR
list1.DataValueField="EmployeeID"
list1.datatextfield="LastName"
list1.databind
list1.selectedindex=0
conn.Close()
End If
End Sub

Sub EditRecord(Source as Object, E as EventArgs)
Dim strConn as string = "server=YourServer;uid=UID;pwd=PWD;database=Northwind"
Dim sql as string = "Select EmployeeID, FirstName, LastName, Title from Employees Where EmployeeID = " & list1.selecteditem.value
Dim conn as New SQLConnection(strConn)
Dim objDR as SQLDataReader
Dim Cmd as New SQLCommand(sql, conn)
conn.Open()
objDR=Cmd.ExecuteReader(system.data.CommandBehavior.CloseConnection)
While objDR.Read()
intID=objDR("EmployeeID")
strFirst=objDR("Firstname")
strLast=objDR("LastName")
strTitle=objDR("Title")
End While
page.databind()
label1.text=""
label2.text=""
End Sub

Sub doUpdate(Source as Object, E as EventArgs)
label1.text="Successfully updated — – "
label2.text="(There is no REAL update here – that will be left for another sample)"
‘ here, you would put your actual code to do whatever you need to, with the populated fields.
End Sub
</script>
</head>
<body>
<form Name="form1" runat="server">
<table border="1" width="100%"> <tr>
<td align="center"width="145" valign="Top"><b><i>Choose from List:</i></b><br>
<asp:ListBox id="list1" Width="125px" height="150px" runat="server" /><br>
<asp:button id="button1" Text="Edit Record" onclick="EditRecord" runat="server" />
</td>

<td align="Left" valign="Top"><b>ID : </b><%# intID %><br>
<b>FirstName : </b> <asp:textbox id="frmFirstName" Text="<%# strFirst %>" runat="server" /><br>
<b>LastName : </b><asp:textbox id="frmLastName" runat="server" Text="<%# strLast %>" /><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Title : </b><asp:textbox id="frmTitle" text="<%# strTitle %>" runat="server" /><br>
<br>
<asp:button id="button2" Text="Update" onclick="doupdate" runat="server" />
<i><asp:Label ID="label1" forecolor="red" runat="server" /></i><br>
<i><asp:Label ID="label2" runat="server" /></i>
</td>
</tr>
</table>
</form>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>