parameter's type doesn't match cached parameters

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

Please do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.

This forum is for discussing mojoPortal development

This forum is only for questions or discussions about working with the mojoPortal source code in Visual Studio, obtaining the source code from the repository, developing custom features, etc. If your question is not along these lines this is not the right forum. Please try to post your question in the appropriate forum.

You can monitor commits to the repository from this page. We also recommend developers to subscribe to email notifications in the developer forum as occasionally important things are announced.

Before posting questions here you might want to review the developer documentation.

Do not post questions about design, CSS, or skinning here. Use the Help With Skins Forum for those questions.
This thread is closed to new posts. You must sign in to post in the forums.
4/28/2010 7:15:45 PM
Gravatar
Total Posts 20

parameter's type doesn't match cached parameters

I'm having this assert run each time I try to update one table. The parameter takes data from a editor text and passes it into an update method & sproc.

The database type is "Text" and the incoming parameter for the DBclass Update method is a string.

So what could be going wrong?

4/28/2010 8:31:38 PM
Gravatar
Total Posts 20

Re: parameter's type doesn't match cached parameters

Debug.Assert(
((type != SqlDbType.NText)&& (arParams[index].SqlDbType == type))
||
((type == SqlDbType.NText) && (arParams[index].SqlDbType == SqlDbType.NVarChar))
||
((type == SqlDbType.Image)&& (arParams[index].SqlDbType == SqlDbType.VarBinary))
, "parameter's type doesn't match cached parameters"
);

 

That is where it fails in the SQLParameterHelper.cs file. The problem is that the field was a Text field, I changed it to an NText and it still fails. I've checked triple checked and double checked the stored procedures and I still have the problem.

 

4/29/2010 1:54:49 AM
Gravatar
Total Posts 28

Re: parameter's type doesn't match cached parameters

hello

I also meet this problem

I can't solve it anyway

and finally I do like this

//Debug.Assert(arParams[index].Direction == dir, "parameter's direction doesn't match cached parameters");
//Debug.Assert(string.Equals(arParams[index].ParameterName,paramName, StringComparison.InvariantCultureIgnoreCase), "parameter's name doesn't match cached parameters");
//Debug.Assert(
// ((type != SqlDbType.NText)
// && (arParams[index].SqlDbType == type))
// ||
// ((type == SqlDbType.NText)
// && (arParams[index].SqlDbType == SqlDbType.NVarChar))
// ||
// ((type == SqlDbType.Image)
// && (arParams[index].SqlDbType == SqlDbType.VarBinary))
// , "parameter's type doesn't match cached parameters"
// );
 

4/29/2010 6:59:59 AM
Gravatar
Total Posts 18439

Re: parameter's type doesn't match cached parameters

Hi,

This is caused by code that helps you find errors in your data logic. If you declare a parameter for a stored procedure in code that does not match the parameter as it is declared in the actual stored procedure it will throw an exception there because of the debug assert (but only when compiled in debug mode not in release mode). Consider the following C# code:

public static bool Update(
            Guid id,
            string statement,
            DateTime lastModUtc,
            Guid lastModBy)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetWriteConnectionString(), "mp_SavedQuery_Update", 4);
            sph.DefineSqlParameter("@Id", SqlDbType.UniqueIdentifier, ParameterDirection.Input, id);
            sph.DefineSqlParameter("@Statement", SqlDbType.NText, ParameterDirection.Input, statement);
            sph.DefineSqlParameter("@LastModUtc", SqlDbType.DateTime, ParameterDirection.Input, lastModUtc);
            sph.DefineSqlParameter("@LastModBy", SqlDbType.UniqueIdentifier, ParameterDirection.Input, lastModBy);
            int rowsAffected = sph.ExecuteNonQuery();
            return (rowsAffected > 0);

        }

Note that I have declared the types of the parameters expected by the stored procedure. Suppose I made a mistake the @Statement parameter is actually not declared as NText in the stored procedure but at nvarchar, then it will raise an error at the Debug.Assert. If I change the stored procedure to match the c# code then I also need to touch Web.config to clear the cache because we cache sql parameters. If I change the C# code to match the procedure I have to re-compile and this would clear the cache.

Note also that this error caused by the Debug.Assert will not happen if you compile for release mode (which you should always do for production builds). A different error may or may not happen, the database is somewhat forgiving, if a param is declared as NText but is actually NVarchar(max) it may still work with no error when compiled as release mode because these data types are close enough and sql server is tolerant. But if for example I declare a uniqueidentifier when it is really an int, that will typically throw an error at runtime.

So the Debug.Assert is there to help you find errors in your code at development/debug time so you can prevent errors at runtime.

Hope it helps,

Joe

4/29/2010 10:03:07 AM
Gravatar
Total Posts 20

Re: parameter's type doesn't match cached parameters

Thanks Joe

In my case, SQL Server wasn't committing itself. I had a friend login and take a look. The assert statement should have cleared and not caused an alert box to pop up. It's running now.

Some of your explanations which are so very great, should be included in your documentation. I understood how the Assert statement was suppose to work but could not understand why it wasn't working correctly.

4/29/2010 10:20:19 AM
Gravatar
Total Posts 18439

Re: parameter's type doesn't match cached parameters

Hi Desirea,

You should not encounter issues with Debug.Assert on production sites if you compiled for release and have <compilation debug="false" in Web.config on production.

It is really a developer tool. Some more info about it, like when to use it and when not to can be found on this stackoverflow post about debug.assert.

I am constantly trying to improve our documentation, but many things that people have questions about are ASP.NET topics not really specific to mojoPortal other than the fact that mojoPortal is an ASP.NET application. So mainly I'm trying to document mojoPortal specific things, there is already lots of documentation for ASP.NET on the web and I try to link to that as much as possible for questions that are not specific to mojoPortal, but often these questions come up in context of developing with mojoPortal even though the concepts apply to all ASP.NET.

Best,

Joe

5/10/2011 8:56:31 AM
Gravatar
Total Posts 7

Re: parameter's type doesn't match cached parameters

So can you override this Debug as I have found it is throwing errors when I don't need them.

Thanks

Richard

5/10/2011 9:10:18 AM
Gravatar
Total Posts 18439

Re: parameter's type doesn't match cached parameters

No, the SqlParameterHelper has these Debug.Asserts to help developers avoid mistakes in their code, you cannot bypass them in debug mode unless you don't use SqlParameterHelper.

You can write your own ADO.NET code if you don't like our helper methods, else you can pass in all the parameters expected by the procedure even if they are null to avoid the error. I recommend use the helpers and pass in the parameters assigned as DBNull.Value if they are null.

Best,

Joe

10/4/2011 2:34:27 AM
Gravatar
Total Posts 39

Re: parameter's type doesn't match cached parameters

To fix this error I had to change the order I was adding the parameters

 

From:

public static IDataReader GetSelectedByItem(
            int ItemID, int ModuleID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "dfc_MyData_SelectedByItem", 2);
            sph.DefineSqlParameter("@ItemID", SqlDbType.Int, ParameterDirection.Input, ItemID);
            sph.DefineSqlParameter("@ModuleID", SqlDbType.Int, ParameterDirection.Input, ModuleID);
            return sph.ExecuteReader();

        }

To:

public static IDataReader GetSelectedByItem(
            int ItemID, int ModuleID)
        {
            SqlParameterHelper sph = new SqlParameterHelper(GetReadConnectionString(), "dfc_MyData_SelectedByItem", 2);
            sph.DefineSqlParameter("@ModuleID", SqlDbType.Int, ParameterDirection.Input, ModuleID);   
            sph.DefineSqlParameter("@ItemID", SqlDbType.Int, ParameterDirection.Input, ItemID);
            return sph.ExecuteReader();

        }

 

10/4/2011 7:25:39 AM
Gravatar
Total Posts 18439

Re: parameter's type doesn't match cached parameters

the parameters have to be in the same order that they are declared in the stored procedure, if they are not then it is an error in code and the debug assert will alert you to it.

You must sign in to post in the forums. This thread is closed to new posts.