Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am searching for a string like xxxx.xxxx in an sql database, but I don't get back any values. My code is:
SQL Stored Procedure:
SQL
ALTER PROCEDURE [dbo].[GetUserId] 
	-- Add the parameters for the stored procedure here
	@Name VarChar
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT *
	from dbo.Users
	where @Name like ADName 
END


ASP.Net Code:
C#
SqlConnection Connection9 = new SqlConnection(ConfigurationManager.ConnectionStrings["HayConnectionStringTest"].ToString());
SqlCommand SqlCmd9 = new SqlCommand("GetUserId");

SqlCmd9.Connection = Connection9;
SqlCmd9.CommandType = CommandType.StoredProcedure;

SqlCmd9.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar));

SqlCmd9.Parameters["@Name"].Value = valami[1].Trim();//Session["username"];

DataTable DTable9 = new DataTable("DTable");
SqlDataAdapter SqlAdapter9 = new SqlDataAdapter(SqlCmd9);

try
{
    Connection9.Open();
    SqlAdapter9.Fill(DTable9);
}
catch (Exception ec)
{
    Label1.Text = ec.ToString();
}
finally
{
    Connection9.Close();
}

If i hardcode the value, the next stored procedure runs fine. I guess I miss something about searching strings, but I don't know where am I wrong. THX for the helps, and sorry for my english.
Posted
Comments
[no name] 29-Oct-14 4:35am    
Could you explain this:
SELECT * from dbo.Users where @Name like ADName.
What is your column name.. I did not get @Name
Shouldnt it be where ADName like @Name. Correct me if I am wrong. I didnt get it
George Jonsson 29-Oct-14 4:44am    
Should be correct if there is a column with the name ADName in the table.
Add as solution instead.
kirk651 29-Oct-14 4:45am    
There is a column with ADName, and it works fine in debug, the problem occurs, if I publish the project.
Mathew Soji 29-Oct-14 4:38am    
Debug and check what is the value passed @ line SqlCmd9.Parameters["@Name"].Value = valami[1].Trim();//Session["username"];
kirk651 29-Oct-14 4:44am    
In debug it works fine, but when i publish, i don't get back values, I've checked the parameter @Name, it's what it's supposed to be, but the datatable has 0 rows. In debug, it works fine, it only happens, if I publish the code.

Hi,

i have to say you like clause usage

while using like we normally use wildcard

Ex :

select * from dbo.Users where ADName like @Name + '%'

Output :

IF @Name =Si then Query Returns

Siva
Sidharth
Sindhu . . .

I guess you miss the wildcard .

Please Refer

http://www.w3schools.com/sql/sql_wildcards.asp[^]


Thank you !
Siva Rm K
 
Share this answer
 
Try this

SQL
ALTER PROCEDURE [dbo].[GetUserId] 
	-- Add the parameters for the stored procedure here
	@Name VarChar(200)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
        -- Insert statements for procedure here
	   SELECT *
	   from dbo.Users
	   where  ADName like @Name

        SET NOCOUNT OFF;
END


I guess your name parameter expects some length values to be defined. and additionally do your name is exactly same as stored in column or you need likely names, then you have to use the wild card(e.g %).
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900