Click here to Skip to main content
15,915,094 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i have the following codes inside my console apps c#:

C#
using (conn = new SqlConnection(GeminiConnString))
{
using (command = new SqlCommand("dbo.CZ_InsertIntoProjectManagementOutstandingExcel", conn))
{
        conn.Open();
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@startDate", StartDate);
        command.Parameters.AddWithValue("@endDate", EndDate);

        command.ExecuteNonQuery(); //<-- error here

    //Sto-Pro Details
    SqlDataAdapter da = new SqlDataAdapter(command);
    DataSet ds = new DataSet();
    da.Fill(ds);
    dt = ds.Tables[0];

    conn.Close();
}
}


whenever i run it, it would prompt me an error as the title above, below are my stored procedure. Stored procedure runs fine, only has error in the c# code:

SQL
ALTER PROCEDURE [dbo].[CZ_InsertIntoProjectManagementOutstandingExcel]
-- Add the parameters for the stored procedure here
@STARTDATE VARCHAR(MAX),
@ENDDATE VARCHAR(MAX)
	
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'
DROP TABLE CustomizationYesNo
DROP TABLE SystemDetermination
DROP TABLE ProjectName
DROP TABLE CustName
DROP TABLE RequesterName
DROP TABLE VersionNumber
DROP TABLE GetTargetDate
DROP TABLE GetActualDate
DROP TABLE ProjectManagement

CREATE TABLE CustomizationYesNo(IssueID VARCHAR(100), YesNo VARCHAR(100))
INSERT [CustomizationYesNo]
SELECT issueid, CASE WHEN projectcode IN (''CESS'', ''CHRM'') THEN ''Yes'' ELSE ''No'' END as Customization 
FROM [dbo].[gemini_issuesview]

CREATE TABLE SystemDetermination(IssueID VARCHAR(100), Systems VARCHAR(100))
INSERT [SystemDetermination]
SELECT
i.issueid, 
CASE SUBSTRING(projectname, 1, 1) 
WHEN ''E'' THEN ''ESS''
WHEN ''F'' THEN ''HRMS''
ELSE ''PayFlex'' 
END AS System 
FROM [dbo].[gemini_projects] p, [dbo].[gemini_issuesview] i
WHERE p.projectid = i.projectid

CREATE TABLE ProjectName(IssueID VARCHAR(100), ProjectName VARCHAR(100))
INSERT [ProjectName]
SELECT i.issueid, projectname 
FROM [dbo].[gemini_projects] p,[dbo].[gemini_issuesview] i
WHERE p.projectid = i.projectid 

CREATE TABLE CustName(IssueID VARCHAR(100), CustomerName VARCHAR(100))
INSERT [CustName]
SELECT i.issueid, fielddata 
FROM [dbo].[gemini_customfielddata] c, [dbo].[gemini_issuesview] i 
WHERE customfieldid = 200 AND c.issueid = i.issueid 

CREATE TABLE RequesterName(IssueID VARCHAR(100), ReqName VARCHAR(100))
INSERT [RequesterName]
SELECT i.issueid, firstname 
FROM [dbo].[gemini_users] u , [dbo].[gemini_issuesview] i 
WHERE u.userid = i.reportedby

CREATE TABLE VersionNumber(IssueID VARCHAR(100), VersionsNum VARCHAR(100))
INSERT [VersionNumber]
SELECT  i.issueid, MAX(v.versionnumber) AS Versions
FROM [dbo].[gemini_versions] v, [dbo].[gemini_issuesview] i
WHERE i.projectid = V.projectid
GROUP BY i.issueid
ORDER BY i.issueid

CREATE TABLE GetTargetDate(IssueID VARCHAR(100), TargetDate VARCHAR(100))
INSERT [GetTargetDate]
SELECT  issueid, fielddata 
FROM [dbo].[gemini_customfielddata]
WHERE customfieldid = 215

CREATE TABLE GetActualDate(IssueID VARCHAR(100), ActualDate VARCHAR(100))
INSERT [GetActualDate]
SELECT issueid, fielddata 
FROM [dbo].[gemini_customfielddata]
WHERE customfieldid = 217



CREATE TABLE ProjectManagement(Category VARCHAR(100), Systems VARCHAR(100), Module VARCHAR(100), Customization VARCHAR(100), Descriptions VARCHAR(MAX),
Customer VARCHAR(100), Statuss VARCHAR(100), CSD_Requester VARCHAR(100), CSD_Request_Date VARCHAR(100), GeminiID VARCHAR(100), Versions VARCHAR(100),
TargetDate VARCHAR(100), ActualDate VARCHAR(100))
INSERT [ProjectManagement]
SELECT i.typedesc, s.Systems, p.ProjectName, c.YesNo, i.longdesc, n.CustomerName, i.statusdesc, r.ReqName, REPLACE(CONVERT(VARCHAR(100), i.created, 106), '' '', ''-''), i.issuekey, v.VersionsNum, REPLACE(t.TargetDate, ''/'', ''-''), REPLACE(a.ActualDate, ''/'', ''-'') 
FROM [dbo].[gemini_issuesview] i, SystemDetermination s, ProjectName p, CustomizationYesNo c, CustName n, RequesterName r, VersionNumber v, GetTargetDate t, GetActualDate a
WHERE i.issueid = s.IssueID AND i.issueid = p.IssueID AND s.IssueID = p.IssueID
AND i.issueid = c.IssueID AND p.IssueID = c.IssueID AND s.IssueID = c.IssueID
AND i.issueid = n.IssueID AND p.IssueID = n.IssueID AND c.IssueID = n.IssueID AND s.IssueID = n.IssueID
AND i.issueid = r.IssueID AND p.IssueID = r.IssueID AND c.IssueID = r.IssueID AND n.IssueID = r.IssueID AND s.IssueID = r.IssueID
AND i.issueid = v.IssueID AND p.IssueID = v.IssueID AND c.IssueID = v.IssueID AND n.IssueID = v.IssueID AND s.IssueID = v.IssueID AND r.IssueID = v.IssueID
AND i.issueid = t.IssueID AND p.IssueID = t.IssueID AND c.IssueID = t.IssueID AND n.IssueID = t.IssueID AND s.IssueID = t.IssueID AND r.IssueID = t.IssueID AND v.IssueID = t.IssueID
AND i.issueid = a.IssueID AND p.IssueID = a.IssueID AND c.IssueID = a.IssueID AND n.IssueID = a.IssueID AND s.IssueID = a.IssueID AND r.IssueID = a.IssueID AND v.IssueID = a.IssueID AND t.IssueID = a.IssueID   

SELECT  * FROM ProjectManagement	   		
	'
	
IF(@STARTDATE != '' AND @ENDDATE != '')
	SELECT @SQL = @SQL + ' WHERE CONVERT(DATE, CSD_Request_Date) BETWEEN CONVERT(DATE, '+@STARTDATE+') AND CONVERT(DATE, '+@ENDDATE+')'
ELSE
	SELECT @SQL = @SQL

SELECT @SQL = @SQL
EXEC sp_executesql @SQL 
   
END
Posted
Comments
Jamie888 10-Jul-14 23:32pm    
FIY, stored procedure works fine and display the correct result. Just the C# codes has error in it.
Jamie888 10-Jul-14 23:33pm    
StartDate is 01/06/2014
EndDate is 11/07/2014
Prasad Avunoori 10-Jul-14 23:57pm    
What is the datatype of StartDate and EndDate in c#?
Jamie888 11-Jul-14 0:05am    
VARCHAR(MAX), i have tried to change them into DATETIME but it give me error instead say "Cannot convert nvarchar into date format"
Jamie888 11-Jul-14 0:06am    
in c# it is in string but before they are passed into stored procedure i have convert them into date data type

Hi,

1st Change -

in SP, Change parameters as datetime, not in nvarchar or varchar.

2nd

Use below syntax in where

SQL
SELECT @SQL = @SQL + ' WHERE CONVERT(DATE, CSD_Request_Date) BETWEEN CONVERT(DATE, '''+@STARTDATE+''') AND CONVERT(DATE, '''+@ENDDATE+''')'
 
Share this answer
 
Comments
Jamie888 11-Jul-14 2:46am    
a thousand thanks, it works fine after i have add another pair of ' inside the WHERE clause.
Convert your date values to DateTime in your C# code, using DateTime.TryParse or DateTime.TryParseExact, and pass them through as valid DateTime values via the parameters.
Modify your SP to accept Date values directly instead of NVARCHAR and get rid of the conversions inside the SP.

Then it'll work fine...
 
Share this answer
 
Comments
Jamie888 11-Jul-14 2:15am    
sir, i have tried your suggestion and change the @STARTDATE VARCHAR(MAX) INTO @STARTDATE DATETIME and remove the conversion part but it give me this error when i execute the stored procedure:
Msg 8152, Level 16, State 10, Procedure CZ_InsertIntoProjectManagementOutstandingExcel, Line 106
String or binary data would be truncated.
Hello ,
first of all check the parameters datatype in your query . as you have declared the @STARTDATE VARCHAR(MAX),@ENDDATE VARCHAR(MAX) in SP , hence check whether these two objects are string or not .

another way
if you want to fill the DataSet then just put the connectionstring and sqlcommand(as string) in DataAdapter object . It has constructor which directly takes those paramter .

 //one way
  SqlDataAdapter da = new SqlDataAdapter(string command , string connection);
  DataSet ds = new DataSet();
  da.Fill(ds);


//another way
  string cmdString = "Select * FROM table";
  var cmd = new SqlCommand(cmdString, connection);
  SqlDataAdapter da= new SqlDataAdapter(cmd);
  DataSet ds = new DataSet();
  da.Fill(ds);

hope it helps you
 
Share this answer
 
v2

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