Mike Adkins
Mike Adkins

Reputation: 3

Passing a Request.QueryString to a SelectParameters DefaultValue when the Request.Query String is a Integer

What I want to do seems simple but I am struggling with finding sample codes to work. My application is in ASP.NET VB. I am trying to pass a selected record from a table to a new page and then have the SQL Query select the record so that I can populate the field for editing. So far I am passing the record indexed ID to the second page. I can show that in a label field just for testing. But I am having issues that value to a SQL Datasource Select Command. Below is the code that works if I add a record number 4 to the Default Value. But I need to replace the Default value with a request.querystring.("VenueID"). Most of my attempts show an error that the SQL request could not be converted from a string to in Integer. Here is my code.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Admn-VenueEdit.aspx.vb" Inherits="Admn_VenueEdit" %>

 <!DOCTYPE html>

 <html xmlns="http://www.w3.org/1999/xhtml">



 <head runat="server">
 <title></title>
 </head>
 <body>



 <Form runat="server">
    <%      
        Dim QueryID As String
        QueryID = Request.QueryString("VenueID")
        Dim URLQueryID
        URLQueryID = Convert.ToInt64(QueryID)
        'esponse.Write(QueryID)
        'Dim VenueQueryString As String
        'VenueQueryString = "SELECT * FROM [tblVenue] Where [ID] = " +        Request.QueryString("VenueID")
    %>         

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1" DataKeyNames="Id" Width="180">
    <Columns>
        <asp:TemplateField HeaderText="Destination" ItemStyle-Width="80">
            <ItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Destintion") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company" ItemStyle-Width="100">
            <ItemTemplate>
                <asp:TextBox ID="txtCountry" runat="server" Text='<%# Eval("Company") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM [tblVenue] Where [ID] = @VenueID">
     <SelectParameters>
          <asp:SessionParameter 
              Name="VenueID" 
              SessionField="VenueID" 
              DefaultValue="4"
              />
     </SelectParameters>
 </asp:SqlDataSource>
    </Form> 

 </body>

Upvotes: 0

Views: 904

Answers (1)

Farzin Kanzi
Farzin Kanzi

Reputation: 3435

The problem is here:

<asp:SessionParameter 
          Name="VenueID" 
          SessionField="VenueID" 
          DefaultValue="4"
          />

It always work with number 4. I doesn't matter what your query string is. But if you need the query string to use in data source, There is an easy way to do this in sqldataSource visually configuration. When you going to it's settings, after set connection string, click next button, in next page you have a button Where click to reach this page:

Set query string for sql data source

After doing all, Click add button and close to back. Your data source must be some thing like this:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:FinalDentistConnectionString1 %>" SelectCommand="SELECT * FROM [ArticleComments] WHERE ([Id] = @Id)">
        <SelectParameters>
            <asp:QueryStringParameter Name="Id" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

Upvotes: 1

Related Questions