Showing posts with label queries. Show all posts
Showing posts with label queries. Show all posts

Wednesday, March 21, 2012

Problem with %....% in the Parameterized Queries

I am using MS SQL 2000.I am writing a simple search statement in the stored procedure which goes like this
Create Procedure.[dbo].[SelectSearch]
(
@.searchtext nvarchar(100)
)
SELECT * FROM TableName WHERE ColumnName LIKE @.searchtext

i want to enter the value in place of @.searchtext as '%strsearch%'
and i'm trying to enter the value of @.searchtext using Parameterized Queries like this

objcmd = new SqlCommand ("SelectSearch", objConn);
objcmd.CommandType = CommandType.StoredProcedure;
objcmd.Parameters.Add("@.searchtext", "'%" + strsearch + "%'");

but its not working out and giving the error message as
Procedure SelectSearch has no parameters and arguments were supplied

Is this the correct way to add the parameters ? Or
How to add' % on both sides of strsearch thro parameterized queries
Thank you very much in advance


You could simply append the %'s to the value.
strsearch = "%" + strsearch + "%";
objcmd = new SqlCommand ("SelectSearch", objConn);
objcmd.CommandType = CommandType.StoredProcedure;
objcmd.Parameters.Add("@.searchtext" );
Also I'd recommend adding the size of the paremeter to avoid problems later on.
|||

Thank you very much mate .

I was struggling to solve this problem atlast i got the answer . Thnx once again

Saturday, February 25, 2012

Problem using SelectParameters with Oracle Queries

Hi,

I have a GridView which is bound to a SqlDataSource that connects to Oracle. Here's the code:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:OracleConnectionString%>" ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName%>" SelectCommand="SELECT QUIZ.TITLE FROM QUIZ WHERE (QUIZ.USERNAME = @.UserName)"
<SelectParameters>
<asp:SessionParameter Name="UserName" SessionField="currentUser" Type="String" />
</SelectParameters
</asp:SqlDataSource>

As you can see I'm trying to pass the value of the "currentUser" session variable to the query. I get an error message "ORA-xxx Illegal name/variable". Where am I going wrong? I tested the connection by placing a specific value instead of the "@.UserName" and it worked.

That is because Oracle Server doesn't use @. as a prefix for a parameter, it uses a colon (:).

Take a look at this example:

http://www.oracle.com/technology/oramag/oracle/05-sep/o55odpnet.html

Problem using SELECT statement on Access Column Name

Im writing a VB program that queries an MS Access db. The column name is CONTACT#. Using the SQL statement...
"SELECT * FROM CLIENT WHERE CONTACT# = '1'"
Produces an error. I cannot change the name of the column name is there anyway around this?Try this:
SELECT * FROM CLIENT WHERE [CONTACT#] = '1'
:eek: