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

Posted on 2006-05-29
Medium Priority
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
  • 2
LVL 12

Accepted Solution

gbzhhu earned 2000 total points
ID: 16785421
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]);

Author Comment

ID: 16785459
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

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 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