Question SelectParameters in a GridView Control

reidacus

New member
Joined
Sep 27, 2012
Messages
2
Programming Experience
Beginner
Hi All

I have a very simple database which I am doing some testing on. It has 1 table (called Table1) and within this table is 1 column (Titled Col1). These holds about 5 VARCHAR values. I am trying to create a simple webpage interface for this table which will allow me to enter a word and then at the click of a button, return all the rows that contain this word. I've done some looking around and experimenting with SelectParameters, however I am having trouble connecting the parameter to the textbox at the top of my webpage.

If anyone has any thoughts I would be very grateful. I'm a newbie to Visual Studio and the language.

Below is the code I have so far. This is from the Default.aspx page

<%
@PageLanguage="VB"AutoEventWireup="false"CodeFile="Default.aspx.vb"Inherits="_Default" %>
<!
DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>Untitled Page</title>

</head>

<
body>


<formid="form1"runat="server">


<div>


<asp:LabelID="Label1"runat="server"Text="Enter Search Item"></asp:Label>


<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox><br/>


<br/>


<asp:ButtonID="Button1"runat="server"Text="Search"/><br/>


<br/>


<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataSourceID="SqlDataSource1">


<Columns>


<asp:BoundFieldDataField="Col1"HeaderText="Col1"SortExpression="Col1"/>


</Columns>


</asp:GridView>


<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ ConnectionStrings:ConnectionString %>"


ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"SelectCommand="SELECT Col1 FROM TABLE1 WHERE Col1 = @item">


<SelectParameters>


<asp:ParameterName="item"Type="String"/>


</SelectParameters>


</asp:SqlDataSource>




</div>


</form>

</
body>

</
html>


The following code is from the Default.aspx.vb page.



Partial Class Default
Inherits System.Web.UI.Page

Protected
Sub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles Button1.Click

SqlDataSource1.SelectParameters(
"item".DefaultValue = _
"%" + TextBox1.Text + "%"

EndSub

End Class

Many thanks everyone.
 
Back
Top