Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

querystring query with code behind

Status
Not open for further replies.

ifntech

Programmer
Nov 4, 2004
80
US
What is the proper syntax for retrieving querystring in db query, if the querystring is in code behind?
Code that compiles but does not retrieve

<script runat = "server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim myvalue as String = Request.QueryString("value")

End Sub
</script>

<asp:SqlDataSource ID="SqlDataSource1 runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString1 %>" ProviderName="<%$ ConnectionStrings:testConnectionString1.ProviderName %>"
SelectCommand="SELECT table1.column1 FROM table1 WHERE table1.column1 = N'@myvalue')">

<SelectParameters>
<asp:QueryStringParameter Name="myvalue" QueryStringField="value" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

<asp:FormView ID="FormView" runat="server" DataSourceID="SqlDataSource">

<ItemTemplate>
<asp:Label ID="labell" runat="server" Text='<%# Bind("column2") %>'>
</asp:Label>
</ItemTemplate>

</asp:FormView>
 
Have you put a break point inside of your <script> block to see if it is getting hit?
 
Also you can change a parameter in this event:
Code:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
   e.Command.Parameters("myvalue").Value = Request.QueryString("value")
End Sub

Jim
 
Your code has errors in it too

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString1 %>" ProviderName="<%$ ConnectionStrings:testConnectionString1.ProviderName %>"
SelectCommand="SELECT table1.column1 FROM table1 WHERE table1.column1 = N'@myvalue')">

<asp:FormView ID="FormView" runat="server" DataSourceID="SqlDataSource1">
 
I was able to fix the problem. The builder of SQLDatabaseSource query gave me an option of choosing column1="QueryString" and column2="Session", when I was building the query using " choose data source " while creating a new SqlDatabaseSource binding.

The code I have so far and it works-
<!-- in master -->
<script runat = "server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Session.Contents("mysession") = "session"


End Sub
</script>

<!-- in asp.net page -->
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString1 %>" ProviderName="<%$ ConnectionStrings:testConnectionString1.ProviderName %>"
SelectCommand="SELECT * FROM [table1] WHERE (([table1.column1] = @column1) AND ([id] = @id))">
<SelectParameters>
<asp:SessionParameter DefaultValue="session" Name="column2" SessionField="session"
Type="String" />
<asp:QueryStringParameter DefaultValue="value" Name="column1" QueryStringField="value"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top