1

I'm passing a partial name to an aspx page in the url in order to filter a list of names. However, the parameter seems to be completely ignored. The url is FilterPage.aspx?strPartialName=abi

The .Net code is:

<ul id="employees">
  <asp:Repeater ID="rptEmployees" DataSourceID="sqlGetEmployees" runat="server">
    <ItemTemplate>
      <li><%# Eval("Name")%></li>
    </ItemTemplate>
  </asp:Repeater>
  <asp:SqlDataSource
    id="sqlGetEmployees"
    ConnectionString = "dsn=EmpDB;"
    ProviderName = "System.Data.Odbc"
    SelectCommand = "cdlSP_GetEmployees"
    SelectCommandType = "StoredProcedure"
    CancelSelectOnNullParameter="false"
    runat="server">
    <asp:SelectParameters>
      <asp:QueryStringParameter Name="strPartialName" DbType="String" Direction="Input" 
        QueryStringField="strPartialName" DefaultValue="" ConvertEmptyStringToNull="True" />
    </asp:SelectParameters>
  </asp:SqlDataSource>
</ul>

And the SQL stored procedure is:

ALTER PROCEDURE [dbo].[cdlSP_GetEmployees] @strPartialName varchar(100) = Null
AS
BEGIN
  SET NOCOUNT ON
  DECLARE @sql VARCHAR(1600)
--Get all records is strPartialName is null
  IF @strPartialName IS NULL
  BEGIN
    SELECT LN + ' ' + FN AS Name
    FROM dbo.Employees
    ORDER BY Name
  END
  ELSE
  BEGIN
    SELECT LN + ' ' + FN AS Name
    FROM dbo.Employees
    WHERE LN LIKE '%' + @strPartialName + '%' OR FN LIKE '%' + @strPartialName + '%'
    ORDER BY Name
  END
END

I added in a Response.Write ("strPartialName=" & Request.QueryString("strPartialName") to the page and it shows as "abi" as expected. I also modified the stored procedure to see if the "Null" condition was firing instead of the ELSE condition (I modified the code to only return the last name). Sure enough, the data being returned was just the employee last names, meaning the strPartialName was being passed as null instead of as "adi". I can't for the life of me figure out why? Anyone?

0

1 Answer 1

0

on the parameter your missing the size. size="100". and the type is SqlDbType.VarChar (this is codebehind form.not sure how to put this in tag form. i assume it is just DbType="varchar") not string

3
  • I already have DbType="String" on my parameter, so that should be OK. Although having Size="100" doesn't affect the passing of the parameter, I did add it anyway. Best practice to prevent someone from trying to add more than 100 characters into the text box. Jul 30, 2014 at 15:35
  • @user3885027 no such thing as Dbtype.String only Dbtype.nvarchar and Dbtype.varchar. And you're using varchar(100)
    – Hillboy
    Jul 31, 2014 at 18:56
  • Unfortunately that isn't it. In .Net, "String" is a member of DbType, "Varchar" is not. You can see all the valid members of DbType here: msdn.microsoft.com/en-us/library/… varchar(100) is what I use the SQL stored procedure. I know there is nothing wrong on the SQL end since I can execute the stored procedure and get the expected results. Aug 2, 2014 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.