Click here to Skip to main content
15,883,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i have the following codes which is to get data from stored procedure in my database and export it into a datatable. But i have found that the dataset is empty. It fetch no data. Therefore the datatable which is suppose to get the data from dataset is also empty. Below are my codes:

C#
var GeminiConnString = ConfigurationManager.ConnectionStrings["GeminiConnString"].ConnectionString;
        SqlConnection conn = null;
        SqlCommand command = null;
        DataTable dt = new DataTable();

        using (conn = new SqlConnection(GeminiConnString)) 
        {
            using (command = new SqlCommand("dbo.GetGeminiRecordByDate", conn)) 
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@startDate", StartDate));
                command.Parameters.Add(new SqlParameter("@endDate", EndDate));

                conn.Open();
                command.ExecuteNonQuery();

                SqlDataAdapter da = new SqlDataAdapter(command);
                DataSet ds = new DataSet();
                da.Fill(ds);
                dt = ds.Tables[0];

                if (ds.Tables[0].Rows.Count == 0) 
                {
                    Console.WriteLine("Empty");
                }
                foreach (DataRow dRow in dt.Rows) 
                {
                    Console.WriteLine(type);
                    Console.Write(dRow["issueid"].ToString());
                }
                conn.Close();
            } 
        }



Below are my stored procedure codes:

SQL
ALTER PROCEDURE [dbo].[GetGeminiRecordByDate]
	-- Add the parameters for the stored procedure here
	@startDate VARCHAR(MAX), 
	@endDate VARCHAR(MAX)
	
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @SQL NVARCHAR(MAX)
	DECLARE @param NVARCHAR (MAX)
	
	SELECT @param = '@startDate VARCHAR(MAX), 
					 @endDate VARCHAR(MAX)'
					 
	SELECT @SQL = 'SELECT * FROM [rndbug].[dbo].[gemini_issuesview]
				   WHERE CONVERT(varchar(10), created, 120) BETWEEN ('+@startDate+') AND ('+@endDate+')'
	
	EXEC sp_executesql @SQL, @param, @startDate, @endDate
END


FIY, the @SQL and @param is same as above while @startDate is '2014-03-01' and @endDate is '2014-08-01'
Posted
Updated 4-Jun-14 18:11pm
v3
Comments
ArunRajendra 4-Jun-14 23:23pm    
Use the SQL profiler to check the parameters being passed.
Jamie888 4-Jun-14 23:29pm    
i have checked it using sql profiler, the params has been passed in. But it still didnt return any result.
DamithSL 4-Jun-14 23:49pm    
can you update the question with GetGeminiRecordByDate stored procedure code?
Jamie888 4-Jun-14 23:51pm    
i have execute the stored procedure in sql server n it works fine.
Is your ConnectionString correct?

1 solution

C#
command.Parameters.AddWithValue("@startDate", StartDate); //StartDate should be a DateTime object
command.Parameters.AddWithValue("@endDate", EndDate);//EndDate should be a DateTime object

change your SP as below

SQL
ALTER PROCEDURE [dbo].[GetGeminiRecordByDate]
	-- Add the parameters for the stored procedure here
	@startDate DATETIME, 
	@endDate DATETIME
	
AS
BEGIN
	SET NOCOUNT ON;
	
	DECLARE @SQL NVARCHAR(MAX)
	DECLARE @param NVARCHAR (MAX)
	
	SELECT @param = '@startDate DATETIME, 
					 @endDate DATETIME'
					 
	SELECT @SQL = 'SELECT * FROM [rndbug].[dbo].[gemini_issuesview]
				   WHERE created BETWEEN @startDate AND @endDate'
	
	EXEC sp_executesql @SQL, @param, @startDate, @endDate
END


if you need to avoid time part of the datetime, change the SQL as below

SQL
SELECT @SQL = 'SELECT * FROM [rndbug].[dbo].[gemini_issuesview]
				   WHERE CONVERT (DATE,created) BETWEEN CONVERT (DATE,@startDate) AND CONVERT (DATE,@endDate)'


Read How to get Date Part only from DateTime in Sql Server[^]
 
Share this answer
 
v3
Comments
Jamie888 5-Jun-14 0:27am    
@DamithSL, i have changed my stored procedure into ur suggestion, but when i execute it, it shows me "Failed to convert nvarchar into datetime"
Jamie888 5-Jun-14 0:34am    
sry, my mistake the stored procedure can run but each time i run the program, it will always accept datetime. Eg. 01/03/2014 00:00:00, how can i exclude the time?
DamithSL 5-Jun-14 0:43am    
where you get StartDate and EndDate? are they strings?
DamithSL 5-Jun-14 0:43am    
Here time will not be a issue, since you have 00:00:00 as time
Jamie888 5-Jun-14 1:58am    
i am getting my startDate and endDate from App.Config. Eg. <add key="StartDate" value="2014-03-01">

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