Bind datagrid to recordset
Hi is there a way to encorporate the following working code into the datagrid example? thank you....
<%
If strSearch <> "" Then
' MapPath of virtual database file path to a physical path.
' If you want you could hard code a physical path here.
strDBPath = Server.MapPath("Contacts.mdb")
' Create an ADO Connection to connect to the sample database using OLE DB.
cnnSearch = Server.CreateObject("ADODB.Connection")
' This line is for the Access sample database:
cnnSearch.Open("Provider=Microsoft.Jet.OLEDB.4.0;D ata Source=" & strDBPath & ";")
' Build our query based on the input.
strSQL = "SELECT * " _
& "FROM [tblEmployee] " _
& "WHERE Name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
& "ORDER BY Name;"
' Execute our query using the connection object. It automatically
' creates and returns a recordset which we store in our variable.
rstSearch = cnnSearch.Execute(strSQL)
' Display a table of the data in the recordset. We loop through the
' recordset displaying the fields from the table and using MoveNext
' to increment to the next record.
%>
<table border="1">
<tr>
<th>Name</th>
<th>Department</th>
<th>Extension</th>
<th>E-mail</th>
</tr>
<%
Do While Not rstSearch.EOF
%>
<tr>
<td><%= rstSearch.Fields("Name").Value %> </td>
<td><%=rstSearch.Fields("Department").Value%></td>
<td><%=rstSearch.Fields("Extension").Value%></td>
<td><%=rstSearch.Fields("E-mail").Value%></td>
</tr>
<%
rstSearch.MoveNext
Loop
%>
</table>
<%
' Close our recordset and connection and dispose of the objects
rstSearch.Close
rstSearch = Nothing
cnnSearch.Close
cnnSearch = Nothing
End If
%>
DATAGRID:
<asp:GridView ID="GridView1" runat="server" CellPadding="2"
ForeColor="#333333" GridLines="None" BorderStyle="None" CellSpacing="2" HorizontalAlign="Center" Width="850px">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Wrap="False" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" Wrap="False" />
<EditRowStyle BackColor="#999999" Wrap="False" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" HorizontalAlign="Center" Wrap="False" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" Wrap="False" />
</asp:GridView>
|