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

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);

try
{
  /* DOES NOT WORK
  SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString,
                                   "GetDbConnInfo", arParms[0]);
  */

    // THIS WORKS
    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;
  return;
}
LVL 1
tatton777Asked:
Who is Participating?
 
gbzhhuConnect With a Mentor Commented:
Have you tried this?

http://professionalaspnet.com/blogs/aspnet_blog/archive/2006/02/24/IconvertableErrorApplicationBlocks.aspx

Just change your

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

to

 SqlDataReader reader = SqlHelper.ExecuteReader(StateSqlDataSource.ConnectionString, CommandType.StoredProcedure,
                                   "GetDbConnInfo", arParms[0]);
0
 
tatton777Author Commented:
Thanks so much for your quick help, and also for helping me to understand what the problem actually was through the hyperlink you provided.
0
 
gbzhhuCommented:
Glad to be of help.  Thanks for the A grade :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.