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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Url Parameter VB.NET

Status
Not open for further replies.

gtjr92

Programmer
May 26, 2004
96
What exactly do I need to do to create a url parameter in ASP.NET with VB.net
I know a little like I need to do something like
Sub RedirectURL
Dim WKID as Listbox.selecteditem.value
Responseredirect(URL thesiteWKID=?)
End Sub
Then I know I need to define the parameter some how
<Parameter>
@WKID = something.

I want the parameter WKID to run in a query
say select * from Tablename where WKID=@WKID

Anyway as you can see I am a novice so me typing this example may make things more confusing.
Thanks
 
Response.Redirect("otherPage.aspx?FName=" & Server.URLEncode(varFName) & "&LName=" & Server.URLEncode(varLName))
 
What you will need to do first is redirect the user to a second page like:
Code:
Response.Redirect("SecondPage.aspx?WKID=" & Server.UrlEncode(ListBox.SelectedItem.value))
Then, on your second page, get the querystring and add it to a parameter. You can then run the query. e.g.
Code:
        Dim objConnection As SqlConnection = New SqlConnection(ConnectionString)
        objConnection.Open()
        Dim objCommand As SqlCommand = New SqlCommand("select * from Tablename where WKID=@WKID", objConnection)
        objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))
        Dim SqlDataReader As SqlDataReader = objCommand.ExecuteReader()
Remember to add Imports System.Data.SQLClient to the top of your page if you are using SQL Server like I used in the example above.


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
what do I need to do to set up the parameters for the @WKID?
Don't I need to also do something like if WKID is Nothing then WKID "" or something to that effect??

Thanks
 
You can just wrap the above code up so it only runs if the quersytring exists. e.g.
Code:
        If Server.UrlDecode(Request.QueryString("WKID")) <> "" Then
            Dim objConnection As SqlConnection = New SqlConnection(ConnectionString)
            objConnection.Open()
            Dim objCommand As SqlCommand = New SqlCommand("select * from Tablename where WKID=@WKID", objConnection)
            objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))
            Dim SqlDataReader As SqlDataReader = objCommand.ExecuteReader()
        End If
If you are having trouble with the basics of simply checking if a string exists, I would suggest starting with something simpler that doesn't involve database inserts. There are lots of good free resources (such as htt://aspnet.4guysfromrolla.com for example) that can help you get to grips with ASP.NET.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Ok Bare with me here I am almost there. LEt me explain more
I have a listbox on a page when the list box is clicked it then runs a sub sublist change. When this happens is passes the url parameter to the URL, however it does not run the query that is in the Sublist Change Sub Routine.
It just reloads the list box.
Basically the url directs it to itself so the list box will remain on the page with the data.
Here is my code.

Code:
 Sub SublistChange (S As Object, E as EventArgs)
                    Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB"
                    Dim WKID as Integer = formlist.selecteditem.value
                    Dim URL as String = "gbctithe_redir.aspx?wkid=" & WKID
                    Dim SqlforGrid As String = "Select [Weeks].[Date]As Weeks_Date,[Tithe].[People Id],[People].[Last_NM],[People].[First_Nm]," & _
                    "[Tithe].[Tithe],[Tithe].[Education],[Tithe].[Missions]," & _
                    "[Tithe].[Special],[Tithe].[Gift_Item],[Tithe].[Gift_Value],[Tithe].[TRID],[tithe].[wkid] " & _
                    "From Weeks,People,Tithe Where [Weeks].[Wkid] = [Tithe].[wkid] And " & _
                    "[People].[ID] = [Tithe].[People ID] And [Weeks].[WKID]=@WKID Orderby [People].[Last_NM],[People].[First_Nm]"

Response.Redirect(URL)
 If Server.UrlDecode(Request.QueryString("WKID")) <> "" Then
                     Dim objConnection As New OledbConnection(connectionstring)
                      Dim objCommand As New OledbCommand(SqlforGrid,objconnection)
                       objConnection.Open()
                        dgGBC.DataSource =objCommand.ExecuteReader()
                        dgGBC.DataBind()

                   objConnection.Close
                   End IF
                   End Sub
 
OK things to check are:

1) You have the event handler set up correctly for the list box
2) Your redirect the user before you run the SQL
3) You have a parameter in your SqlforGrid string yet you don't actually add any parameters
4) You have "orderby" written as one word rather than two

I really think you need to take my advice and read up on some or the more basic aspects of ASP.NET before tackling anything more complicated.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
Oh i didn't mention clearly that when you click the listbox it does change the actual url to I realized I did not add the objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))

but i added here
Code:
If Server.UrlDecode(Request.QueryString("WKID")) <> "" Then
                     Dim objConnection As New OledbConnection(connectionstring)
                      Dim objCommand As New OledbCommand(SqlforGrid,objconnection)
                 [COLOR=red]       objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))[/color]
                        dgGBC.DataSource =objCommand.ExecuteReader()
                        dgGBC.DataBind()

                   objConnection.Close
                   End IF
                   End Sub
 
In that case see point 2 that I made above. The above code you have written should go in the second page not the SublistChange sub.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
There is no second page this redirects to the same page.

My list box runs a query to populate itself then when a user clicks on it it runs the sublistchange on the same page according to the selected value.

below is my page load code for this same page as the code above is also on

I very much appreciate your help
I have done several asp.net bookks and am familiar with scott mitchell and his books etc. Just having a little trouble putting this url thing together.

Code:
 Sub Page_Load(Source as Object, E as EventArgs)
                   If Not Page.isPostBack then
                   BindData()
                   End IF
                   End Sub

                   Sub BindData
                   Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB;"
                   Dim dbConnection As New OleDbConnection(connectionString)

                   Dim SqlListbox As String = "SELECT [Weeks].[Date] As Weeks_Date ,[Weeks].[WKID] As Weeks_ID,[Weeks].[Week] from Weeks"
                   Dim objConnection As New OleDbConnection(connectionstring)
                   Dim objCommand As New OledbCommand(Sqllistbox, objconnection)
                   objConnection.Open()
                   formlist.Datasource=objCommand.ExecuteReader()
                   formlist.Datatextfield = "Weeks_Date"
                   formlist.DataValueField  = "Weeks_ID"
                   formlist.DataBind()
                   objConnection.Close()
 
So why do you have a Response.Redirect if you are going to the same page? The listbox will post back to the page so if it is a postback sun your select statement, if not bind your listbox.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
well it's a dynamic page depending on what the listbox selected val is.
my list box is bound in the page load sub my binddata sub is what binds the listbox
I tried taking out the response redirect like you suggested then my url did not even change.
My listbox is set to auto postback.

again i appreciate your help.
 
here is all my code
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script runat="server">

              Sub Page_Load(Source as Object, E as EventArgs)
                        If Not Page.isPostBack then
                        BindData()
                        End IF
                        End Sub

'this sub binds data to the listbox
                        Sub BindData
                        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB;"
                        Dim dbConnection As New OleDbConnection(connectionString)
'query for listbox
                        Dim SqlListbox As String = "SELECT [Weeks].[Date] As Weeks_Date ,[Weeks].[WKID] As Weeks_ID,[Weeks].[Week] from Weeks"
                        Dim objConnection As New OleDbConnection(connectionstring)
                        Dim objCommand As New OledbCommand(Sqllistbox, objconnection)
                        objConnection.Open()
                        formlist.Datasource=objCommand.ExecuteReader()
                        formlist.Datatextfield = "Weeks_Date"
                        formlist.DataValueField  = "Weeks_ID"
                        formlist.DataBind()
                        objConnection.Close()

                       End Sub

'This sub takes place when listbox is clicked
Sub SublistChange (S As Object, E as EventArgs)
                         Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB"
                         Dim WKID as Integer = formlist.selecteditem.value
                         Dim URL as String = "gbctithe_redir.aspx?wkid=" & WKID
                         Dim SqlforGrid As String = "Select [Weeks].[Date]As Weeks_Date,[Tithe].[People Id],[People].[Last_NM],[People].[First_Nm]," & _
                         "[Tithe].[Tithe],[Tithe].[Education],[Tithe].[Missions]," & _
                         "[Tithe].[Special],[Tithe].[Gift_Item],[Tithe].[Gift_Value],[Tithe].[TRID],[tithe].[wkid] " & _
                         "From Weeks,People,Tithe Where [Weeks].[Wkid] = [Tithe].[wkid] And " & _
                         "[People].[ID] = [Tithe].[People ID] And [Weeks].[WKID]= @WKID Order by [People].[Last_NM],[People].[First_Nm]" 
 

       Response.Redirect(URL)

If Server.UrlDecode(Request.QueryString("WKID")) <> "" Then
                          Dim objConnection As New OledbConnection(connectionstring)
                          Dim objCommand As New OledbCommand(SqlforGrid,objconnection)
                         objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))
                         dgGBC.DataSource =objCommand.ExecuteReader()
                         dgGBC.DataBind()
                        objConnection.Close
                        End IF
                        End Sub

<asp:ListBox id="formlist" runat="server" Backcolor="DarkGray" ForeColor="DarkGreen" DataTextFormatString="{0:d}" AutoPostBack="True" OnSelectedIndexChanged="SublistChange" Rows="15" Height="200px" Width="101px"></asp:ListBox>

<asp:DataGrid id="dgGBC" runat="server" AllowSorting="True"  AutoGenerateColumns="False" DataKeyField="TRID" OnEditCommand="dgGBCed" OnUpdateCommand="dgGbc_update" OnCancelCommand="dgGbc_Cancelrw" BorderColor="Olive" CellSpacing="1" CellPadding="1" OnitemDataBound="ComputeSum" BorderStyle="Solid" ShowFooter="True">
                            <AlternatingItemStyle forecolor="Navy" borderstyle="Solid" backcolor="Gray"></AlternatingItemStyle>
                            <ItemStyle font-size="10pt" forecolor="Gainsboro" backcolor="Navy"></ItemStyle>
                            <HeaderStyle font-bold="True" horizontalalign="Center" forecolor="Navy" verticalalign="Middle" backcolor="Gray" ></HeaderStyle>
                            <Columns>

                            <asp:EditCommandColumn ButtonType="PushButton" HeaderText="Edit" EditText="Edit" UpdateText="Update" CancelText="Cancel" />
                                <asp:BoundColumn DataField="TRID" Visible="false"></asp:BoundColumn>
                                <asp:BoundColumn DataField="WKID" HeaderText="WKID" ReadOnly="True"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Last_NM" SortExpression="Last_Name" HeaderText="Last Name" ReadOnly="True"></asp:BoundColumn>
                                <asp:BoundColumn DataField="First_NM" HeaderText="First Name" SortExpression="First_Name" ReadOnly="True" ></asp:BoundColumn>
                                <asp:BoundColumn DataField="Tithe" HeaderText="Tithe" DataFormatString="{0:c}" SortExpression="Tithe"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Education" HeaderText="Education" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Missions" HeaderText="Missions" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Special" HeaderText="Special" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Gift_Item" HeaderText="Gift"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Gift_Value" HeaderText="Gift Value" DataFormatString="{0:c}"></asp:BoundColumn>

                            </Columns>
                        </asp:DataGrid>
 
Don't use response.redirect as the listbox will postback.

On the page load check if it is a postback. If it is, run your select statement based on the formlist.selecteditem.value rather than the querystring (as we don't need this anymore since you have now said there is not second page). If it isn't, run your binddata sub.

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
but i need to run it based on the query string because this datagrid can be edited and when it is edited I need that wkid parameter from the url string.
otherwise when i go to edit the data it loses the formlist.selecteditem.value

Here is all my code excluding the datagrid edit and other things not related to this.
Code:
<%@ Page Language="VB" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.Oledb" %>
<script runat="server">

              Sub Page_Load(Source as Object, E as EventArgs)
                        If Not Page.isPostBack then
                        BindData()
                        End IF
                        End Sub

'this sub binds data to the listbox
                        Sub BindData
                        Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB;"
                        Dim dbConnection As New OleDbConnection(connectionString)
'query for listbox
                        Dim SqlListbox As String = "SELECT [Weeks].[Date] As Weeks_Date ,[Weeks].[WKID] As Weeks_ID,[Weeks].[Week] from Weeks"
                        Dim objConnection As New OleDbConnection(connectionstring)
                        Dim objCommand As New OledbCommand(Sqllistbox, objconnection)
                        objConnection.Open()
                        formlist.Datasource=objCommand.ExecuteReader()
                        formlist.Datatextfield = "Weeks_Date"
                        formlist.DataValueField  = "Weeks_ID"
                        formlist.DataBind()
                        objConnection.Close()

                       End Sub

'This sub takes place when listbox is clicked
Sub SublistChange (S As Object, E as EventArgs)
                         Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\GBC\GBCTITHE.MDB"
                         Dim WKID as Integer = formlist.selecteditem.value
                         Dim URL as String = "gbctithe_redir.aspx?wkid=" & WKID
                         Dim SqlforGrid As String = "Select [Weeks].[Date]As Weeks_Date,[Tithe].[People Id],[People].[Last_NM],[People].[First_Nm]," & _
                         "[Tithe].[Tithe],[Tithe].[Education],[Tithe].[Missions]," & _
                         "[Tithe].[Special],[Tithe].[Gift_Item],[Tithe].[Gift_Value],[Tithe].[TRID],[tithe].[wkid] " & _
                         "From Weeks,People,Tithe Where [Weeks].[Wkid] = [Tithe].[wkid] And " & _
                         "[People].[ID] = [Tithe].[People ID] And [Weeks].[WKID]= @WKID Order by [People].[Last_NM],[People].[First_Nm]" 
 

       Response.Redirect(URL)

If Server.UrlDecode(Request.QueryString("WKID")) <> "" Then
                          Dim objConnection As New OledbConnection(connectionstring)
                          Dim objCommand As New OledbCommand(SqlforGrid,objconnection)
                         objCommand.Parameters.Add("@WKID", Server.UrlDecode(Request.QueryString("WKID")))
                         dgGBC.DataSource =objCommand.ExecuteReader()
                         dgGBC.DataBind()
                        objConnection.Close
                        End IF
                        End Sub

<asp:ListBox id="formlist" runat="server" Backcolor="DarkGray" ForeColor="DarkGreen" DataTextFormatString="{0:d}" AutoPostBack="True" OnSelectedIndexChanged="SublistChange" Rows="15" Height="200px" Width="101px"></asp:ListBox>

<asp:DataGrid id="dgGBC" runat="server" AllowSorting="True"  AutoGenerateColumns="False" DataKeyField="TRID" OnEditCommand="dgGBCed" OnUpdateCommand="dgGbc_update" OnCancelCommand="dgGbc_Cancelrw" BorderColor="Olive" CellSpacing="1" CellPadding="1" OnitemDataBound="ComputeSum" BorderStyle="Solid" ShowFooter="True">
                            <AlternatingItemStyle forecolor="Navy" borderstyle="Solid" backcolor="Gray"></AlternatingItemStyle>
                            <ItemStyle font-size="10pt" forecolor="Gainsboro" backcolor="Navy"></ItemStyle>
                            <HeaderStyle font-bold="True" horizontalalign="Center" forecolor="Navy" verticalalign="Middle" backcolor="Gray" ></HeaderStyle>
                            <Columns>

                            <asp:EditCommandColumn ButtonType="PushButton" HeaderText="Edit" EditText="Edit" UpdateText="Update" CancelText="Cancel" />
                                <asp:BoundColumn DataField="TRID" Visible="false"></asp:BoundColumn>
                                <asp:BoundColumn DataField="WKID" HeaderText="WKID" ReadOnly="True"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Last_NM" SortExpression="Last_Name" HeaderText="Last Name" ReadOnly="True"></asp:BoundColumn>
                                <asp:BoundColumn DataField="First_NM" HeaderText="First Name" SortExpression="First_Name" ReadOnly="True" ></asp:BoundColumn>
                                <asp:BoundColumn DataField="Tithe" HeaderText="Tithe" DataFormatString="{0:c}" SortExpression="Tithe"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Education" HeaderText="Education" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Missions" HeaderText="Missions" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Special" HeaderText="Special" DataFormatString="{0:c}"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Gift_Item" HeaderText="Gift"></asp:BoundColumn>
                                <asp:BoundColumn DataField="Gift_Value" HeaderText="Gift Value" DataFormatString="{0:c}"></asp:BoundColumn>

                            </Columns>
                        </asp:DataGrid>
 
If you are just going to keep posting your code then I may as well just keep posting my answer until you take notice of it:
ca8msm said:
run your select statement based on the formlist.selecteditem.value rather than the querystring

--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
did you not read why i do not want to use the formlist selected value in the sql? but i need to run it based on the query string because this datagrid can be edited and when it is edited I need that wkid parameter from the url string.
otherwise when i go to edit the data it loses the formlist.selecteditem.value


I reposted my code because I posted some code that i had not posted before and I figured i would repost it all so you or anyone else wouldn't have to scoll up and reread the code in another post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top