In my asp.net page when I run a query against the database the datagrid get populated with only the records that starts with the first letter for example A good day. But none of the records that starts like this "A fine day" or "A nice day" displays in the datagrid. I tried to replace the Double Quote but it still returns only the records without " quotes. My query looks like this:
Dim queryString As String = "SELECT [Articles].[AN], [Articles].[Department], [Articles].[ArticleHeading], [Articles].[AccessLevel], [Articles].[Status] FROM [Articles] WHERE (([Articles].[AccessLevel] <> 'SysAdmin') AND ([Articles].[Status] = 'Enable') AND (REPLACE([Articles].[ArticleHeading], 'chr(34)', '')) like @.ArticleHeading) ORDER BY [Articles].[ArticleHeading]"
You need to do the replace on both the column AND the value you are comparing, otherwise they will never be equal.eg.
Dim queryString As String = "SELECT [Articles].[AN], [Articles].[Department], [Articles].[ArticleHeading], [Articles].[AccessLevel], [Articles].[Status] FROM [Articles] WHERE (([Articles].[AccessLevel]<> 'SysAdmin') AND ([Articles].[Status] = 'Enable') AND (REPLACE([Articles].[ArticleHeading], 'chr(34)', '')) like REPLACE(@.ArticleHeading),'chr(34)') ORDER BY [Articles].[ArticleHeading]"|||
I tried it but still it does not produce the right results.
My code:
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT [Articles].[AN], [Articles].[Department], [Articles].[ArticleHeading], [Articles].[AccessLevel], [Articles].[Status] FROM [Articles] WHERE (([Articles].[AccessLevel] <> 'SysAdmin') AND ([Articles].[Status] = 'Enable') AND (REPLACE([Articles].[ArticleHeading], 'chr(34)', '')) like (REPLACE(@.ArticleHeading, 'chr(34)', ''))) ORDER BY [Articles].[ArticleHeading]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dbParam_articleHeading As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
dbParam_articleHeading.ParameterName = "@.ArticleHeading"
dbParam_articleHeading.Value = Request.QueryString("Alphabet")& "%"
dbParam_articleHeading.DbType = System.Data.DbType.String
dbCommand.Parameters.Add(dbParam_articleHeading)
Using a sql query I manage to get the right results but what do I need to do in asp.net to get the right results. My SQL code that replaces the ":
SELECT *
FROM Articles
WHERE (REPLACE(ArticleHeading, '"', '') LIKE 'A%')
ORDER BY ArticleHeading
Change:
REPLACE([Articles].[ArticleHeading], 'chr(34)', '')
To:
REPLACE([Articles].[ArticleHeading], chr(34), '')
No comments:
Post a Comment