I NEED HELP FAST SqlHelper.ExecuteQuery doesn't like SqlParameters

Posted on 2006-05-29
Last Modified: 2008-01-09
When I try to pass a SqlParameter into the SqlHelper.ExecuteQuery method I get this error.


Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Object must implement IConvertible.

Source Error:

Line 684:                  else
Line 685:                  {
Line 686:                        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Line 687:                  }
Line 688:                  

Source File: C:\Program Files\Microsoft Application Blocks for .NET\Data Access\Code\CS\Microsoft.ApplicationBlocks.Data\SQLHelper.cs    Line: 686

Stack Trace:

[InvalidCastException: Object must implement IConvertible.]
   System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +2561437
   System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +842

[InvalidCastException: Failed to convert parameter value from a SqlParameter to a Int32.]
   System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType) +904
   System.Data.SqlClient.SqlParameter.GetCoercedValue() +58
   System.Data.SqlClient.SqlParameter.Validate(Int32 index) +194
   System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) +153
   System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) +107
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) +1121
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) +334
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +45
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) +162
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior) +86
   Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, String commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership) in C:\Program Files\Microsoft Application Blocks for .NET\Data Access\Code\CS\Microsoft.ApplicationBlocks.Data\SQLHelper.cs:686
   Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(String connectionString, CommandType commandType, String commandText, SqlParameter[] commandParameters) in C:\Program Files\Microsoft Application Blocks for .NET\Data Access\Code\CS\Microsoft.ApplicationBlocks.Data\SQLHelper.cs:741
   Microsoft.ApplicationBlocks.Data.SqlHelper.ExecuteReader(String connectionString, String spName, Object[] parameterValues) in C:\Program Files\Microsoft Application Blocks for .NET\Data Access\Code\CS\Microsoft.ApplicationBlocks.Data\SQLHelper.cs:772
   login.LoginButton_Click(Object sender, EventArgs e) in d:\My Documents\Visual Studio 2005\WebSites\MSF_v1.5\login.aspx.cs:79
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72


However, if I just pass a simple variable in as my parameter it works fine. What is going on? All the examples that I'm seeing say to use the SqlParameters, that they are better for security and just part of good programming when it comes to using DAAB.

Here is the code...

SqlParameter[] arParms = new SqlParameter[1];
arParms[0] = new SqlParameter("@Dist_Id", SqlDbType.Int);
arParms[0].Value = Convert.ToInt32(DistrictDropDownList.SelectedValue);

int iDist_Id = Convert.ToInt32(DistrictDropDownList.SelectedValue);

  SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString,
                                   "GetDbConnInfo", arParms[0]);

    SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString,
                                   "GetDbConnInfo", iDist_Id);

  while (reader.Read())
    string zDbServer = reader.GetString(0);
    string zDbDataBase = reader.GetString(1);
    string zDbUserId = reader.GetString(2);
    string zDbPassword = reader.GetString(3);

    sqlDistCnBuilder.ConnectionString =
        "Data Source=" + zDbServer + ";Initial Catalog=" + zDbDataBase +
        ";User ID= " + zDbUserId + ";Password=" + zDbPassword;
    sv.DistConn = sqlDistCnBuilder;  
catch (SqlException sqlEx)
  ErrorMessageLabel.Visible = true;
  ErrorMessageLabel.Text = "Method LoginButton_Click():1 threw data access error  : " + sqlEx.Message;
Question by:tatton777
    LVL 12

    Accepted Solution

    Have you tried this?

    Just change your

     SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString,
                                       "GetDbConnInfo", arParms[0]);


     SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString, CommandType.StoredProcedure,
                                       "GetDbConnInfo", arParms[0]);
    LVL 1

    Author Comment

    Thanks so much for your quick help, and also for helping me to understand what the problem actually was through the hyperlink you provided.
    LVL 12

    Expert Comment

    Glad to be of help.  Thanks for the A grade :-)

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now