Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I've made a stored procedure in SQL server 2008, however, when i try to use in it C# (visual studio), it returns no rows (he doesn't return anything).

The Connection string is correct becouse,

This works:
SqlConnection connection = new SqlConnection("Data Source=LT004512\\SQLEXPRESS;Initial Catalog=TestDatabase;Integrated Security=SSPI;");
SqlCommand command = new SqlCommand();
DataTable data = new DataTable();
command.CommandText = "SELECT * FROM item WHERE item_ID = @hoi";
command.Connection = connection;
command.Parameters.AddWithValue("@hoi", 1);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
data.Load(reader);
connection.Close();
DeGridView.DataSource = data;


but THIS doesn't:
SqlConnection connection = new SqlConnection("Data Source=LT004512\\SQLEXPRESS;Initial Catalog=TestDatabase;Integrated Security=SSPI;");

      SqlCommand command = new SqlCommand("usp_GetItemWithID",connection);
      command.CommandType = CommandType.StoredProcedure;
      connection.Open();
      command.Parameters.AddWithValue("@hetid", 1);
      DataTable InstellingTabel = new DataTable();
      SqlDataReader informatie = command.ExecuteReader();
      connection.Close();
      InstellingTabel.Load(informatie);
      DeGridView.DataSource = InstellingTabel;


My stored procedure looks like this:

USE [TestDatabase]
GO
/****** Object:  StoredProcedure [dbo].[usp_GetItemWithID]    Script Date: 03/21/2011 11:31:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Name:  usp_GetItemWithID
Description:  Gets the item with the requested ID
Author:  Amateurgrammer
Modification Log: None
Description                  Date         Changed By
Created procedure            3/21/201           None
*/
ALTER PROCEDURE [dbo].[usp_GetItemWithID]
@hetid int
AS
SELECT * FROM item
WHERE item_ID=@hetid


But when i execute the stored procedure in SQL server it works just fine, so i think that won't be the problem.

Sorry for the S***load of information i gave, but i'm new here and unfamiliar with asking questions about this stuff.
Posted

Just skimmed through the code, but have you tried moving the
connection.Close();
after loading the datatable?
 
Share this answer
 
Comments
Amateurgrammer 21-Mar-11 6:47am    
HOW CAN I HAVE MISSED THAT >.<
Well, thank you for pointing that out, this must be one of those scenario's were i was approaching the problem in a FAR to difficult way.

Thanks again :)
Wendelius 21-Mar-11 6:48am    
No problem at all :) Believe me I've been in the same situation sooo many times.
Toniyo Jackson 21-Mar-11 8:03am    
Good call +5
Wendelius 21-Mar-11 8:54am    
Thanks :)
Dalek Dave 21-Mar-11 8:54am    
Good Answer.
You can't call connection close until after you've read the data from the SqlDataReader object. If you haven't already, put your code in a try/catch/finally block, and close the connection in the finally portion.
 
Share this answer
 
Comments
Toniyo Jackson 21-Mar-11 8:02am    
Best Solution +5
SQL Profiler can help you to track requests to your database.
 
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