Type conversion error using Data Access Application Blocks RRS feed

  • Question

  • User-1683643362 posted
    I am trying to create a method that retrieves a value from my SQL2000 database using a stored procedure. The method I've created so far uses the MS Data Access Application Blocks. I copied the code from the MSDN example on using the Data Access Application Blocks ( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp ). The code copied word for word doesn't compile. Here's my method: 1 public SqlDataReader GetModSettings(int moduleID) 2 { 3 string CONN_STRING = ConfigurationSettings.AppSettings["conString"]; 4 string spName = "GSMGetParentID_sp"; 5 6 // Retrieve the parameters 7 SqlParameter storedParams = new SqlParameter[1]; 8 storedParams = SqlHelperParameterCache.GetSpParameterSet(CONN_STRING, spName); 9 storedParams[0].Value = moduleID; 10 11 12 //Use the parameters in a command 13 SqlDataReader ds; 14 ds = SqlHelper.ExecuteReader(CONN_STRING, CommandType.StoredProcedure, spName, storedParams); 15 return ds; 16 17 } Here's the stored procedure: PROCEDURE dbo.GSMGetParentID_sp @moduleID int AS SELECT GreenSubMenu.ParentID AS ParentID, Tabs.TabName AS TabName FROM GreenSubMenu INNER JOIN Tabs ON GreenSubMenu.ParentID = Tabs.ParentId WHERE GreenSubMenu.ModuleID = @moduleID Lines 7,8 and 9 of the C# code all cause erros which prevent me from compiling. The errors for each are: Line 7: SqlParameter storedParams = new SqlParameter[1]; Error: Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter[]' to 'System.Data.SqlClient.SqlParameter' Line 8: storedParams = SqlHelperParameterCache.GetSpParameterSet(CONN_STRING, spName); Error: Cannot implicitly convert type 'System.Data.SqlClient.SqlParameter[]' to 'System.Data.SqlClient.SqlParameter' Line 9: storedParams[0].Value = moduleID; Error: Cannot apply indexing with [] to an expression of type 'System.Data.SqlClient.SqlParameter' I'm not sure if there is a syntax error somewhere or if I'm using the SQLHelper class improperly. Anyhelp will be appreciated. Thanks, HP
    Tuesday, December 9, 2003 11:31 AM

All replies

  • User-702827227 posted
    Parameter[0] in SQL Server is RETURN_VALUE. Start with parameter 1.
    Monday, December 22, 2003 2:03 PM
  • User-1683643362 posted
    I solved this problem a while back, sorry I should have mentioned it here... The issue is with line 7. Instead of: SqlParameter storedParams = new SqlParameter[1]; it should be: SqlParameter[] storedParams = new SqlParameter[1]; C# doesn't recognize a 0 based index during array initialization. So if you try to use a 0 in place of a 1, it won't compile (I think...). However when you use the array you specify your index position using a 0 like this: storedParams[0].Value = myValue;
    Tuesday, December 23, 2003 2:33 PM