Click here to Skip to main content
15,911,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have problem with receiving EmployeeID. I have stored procedure which returns on one textbox name + surname + docNo like 'Richarson Michael 674UK'. Stroed procedure below:

CREATE PROCEDURE [dbo].[SearchEmployee]
	@employeeSurname varchar(80)

AS
	SELECT (surname+ ' ' + name+ ' ' + DocNo) as surname, EmployeeId
	FROM Employee
	WHERE surname LIKE @employeeSurname+ '%'
RETURN


I have also second method which should return Employees ID from TexBox above. My method reruns always 0. My method is below:

public int returnEmployeeId (string text) {
int employeeId = 0;
string query = "SELECT EmployeeId"
+ " FROM Employee WHERE surname LIKE'%" + text + "%'";
(...) 

return employeeId;
}


What I have tried:

I have tried to create method called returnEmployeeId for different ways. I wanted to use my strored procedure where SqlDataReader returns employeeId, but it always returns 0. I was also trying to create different query but always the same. TextBox where the data is shown has subbmited AutoPostBAck for true. Can someone give me a tip or a clue how to solve my problem.
Thank you in advance.
Posted
Updated 30-Jan-17 11:04am
Comments
[no name] 30-Jan-17 16:05pm    
"I wanted to use my strored procedure", then show us the code where you are calling the stored procedure.
Member 11187739 30-Jan-17 16:25pm    
My method looks like this:
public int returnEmployeeId (string text) {
int employeeId = 0;
SqlConnection connection = CreateConnection.newConnection();
SqlCommand command = new SqlCommand("SearchEmployee", connection);
command.Parameters.AddWithValue("@employeeSurname ", text);
SqlDataReader reader;

using (connection)
{
connection.Open();
using (command)
{
command.CommandType = CommandType.StoredProcedure;


reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
employeeId = int.Parse(reader["EmployeeId"].ToString());
}
}

}
CreateConnection.closeConnection(connection);
}
return employeeId;
}

[no name] 30-Jan-17 19:15pm    
You need to set the command type to stored procedure.

1 solution

Rather than using a stored procdure you should use a table valued function that returns a table

eg

CREATE FUNCTION [dbo].[SearchEmployee]
(@employeeSurname varchar(80))
RETURNS TABLE 
AS
RETURN 
(SELECT (surname+ ' ' + name+ ' ' + DocNo) as surname, EmployeeId
	FROM Employee
	WHERE surname LIKE @employeeSurname+ '%'
)


Thus you can treat it like any normal table

ie

SQL
Select * from Searchemployee ('Bob')
 
Share this answer
 

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