HI,
My team is automating a scenario in which we used to do manual queries in SQL and now we are developing application in C#.
The original query that we used to run in SQL was:
"select count(*) as NbLayers, hmn.missingNodeInCStar, a.cbu, a.region, replace(substring(filepath, 2,charindex('\',substring(filepath,2,50))),'\','') as headend, replace(SUBSTRING(FilePath, CHARINDEX('\', SUBSTRING(FilePath, 2, 50)) + 1, 50),'\','') as node from Rogers.dbo.Rogers_NetworkMapFile b right join rogers..v_nodeRegion a on replace(SUBSTRING(FilePath, CHARINDEX('\', SUBSTRING(FilePath, 2, 50)) + 1, 50),'\','') = a.node_id right join rogers..he_mapnode hmn on a.node_id = hmn.node_id where len(filepath)>8 and hmn.missingNodeInCStar = 0 group by b.filepath,hmn.missingNodeInCStar,a.cbu, a.region";
So I added the OUTER select count (NbLayers) to get the count.
When I run it, it gives me an error. Here's the new query that I run.
string temp_query = "select count(NbLayers) from (select count(*) as NbLayers, hmn.missingNodeInCStar, a.cbu, a.region, replace(substring(filepath, 2,charindex(''\'',hsubstring(filepat,2,50))),''\'','''') as headend, replace(SUBSTRING(FilePath, CHARINDEX(''\'', SUBSTRING(FilePath, 2, 50)) + 1, 50),''\'','''') as node from Rogers.dbo.Rogers_NetworkMapFile b right join rogers..v_nodeRegion a on replace(SUBSTRING(FilePath, CHARINDEX(''\'', SUBSTRING(FilePath, 2, 50)) + 1, 50),''\'','''') = a.node_id right join rogers..he_mapnode hmn on a.node_id = hmn.node_id where len(filepath)>8 and hmn.missingNodeInCStar = 0 group by b.filepath,hmn.missingNodeInCStar,a.cbu, a.region ) AS temp_table";
The error that I get is :
Syntax error converting the varchar value '\\PB01\\118947\\' to a column of data type int.\r\n at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)\r\n at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean breakConnection)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()\r\n at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet
bulkCopyHandler, TdsParserStateObject stateObj)\r\n at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)\r\n at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName,
Boolean sendToPipe)\r\n at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()\r\n at WindowsFormsApplication1.Form1.Get_From_SQLServerDB()
How do I proceed ? The columns are of varchar type. I think that shouldn't matter because I just want the count.