Failed to convert parameter value from a SqlParameter to a String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • balajius
    New Member
    • Apr 2010
    • 2

    #1

    Failed to convert parameter value from a SqlParameter to a String

    I am using VS2008 with asp.net 3.5 and enterprise library 4.1. I am getting the following error when I try to connect to database.
    "Failed to convert parameter value from a SqlParameter to a String"

    Here is my code

    Code:
    Database db = DatabaseFactory.CreateDatabase("SQLSRVR");
    
    string myStoredProcedure = "usp_GetPersonContact";
                
    SqlParameter[] SQLParameters = new SqlParameter[1];
    SQLParameters[0] = new SqlParameter("@ln", SqlDbType.NVarChar, 50);            
    SQLParameters[0].Value = "Smith"; 
    SQLParameters[0].Direction = ParameterDirection.Input;
    
    DataSet resultDataSet = null;
    resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters);
    I already tried conversion but seems nothing is working. Please let me know what i am doing wrong. Thanks.
  • PandiarajanChellappan
    New Member
    • Apr 2010
    • 7

    #2
    Chage the type SqlDbType.NVarC har to SqlDbType.VarCh ar..
    then it will work.....
    (ie) change ur code like this,,,,,

    SQLParameters[0] = new SqlParameter("@ ln", SqlDbType.VarCh ar, 50,"ln");
    ..

    Comment

    • balajius
      New Member
      • Apr 2010
      • 2

      #3
      Originally posted by PandiarajanChel lappan
      Hi,
      Chage the type SqlDbType.NVarC har to SqlDbType.VarCh ar..
      then it will work.....
      (ie) change ur code like this,,,,,

      SQLParameters[0] = new SqlParameter("@ ln", SqlDbType.VarCh ar, 50,"ln");

      Thanks,
      Pandian...
      Thanks for the help Pandian.

      Still is not working. Here is the latest code
      C#
      Code:
      Database db = DatabaseFactory.CreateDatabase("SQLSRVR");
      
      string myStoredProcedure = "usp_GetPersonContact";
      
      SqlParameter[] SQLParameters = new SqlParameter[1];
      
      SQLParameters[0] = new SqlParameter("@lastname", SqlDbType.VarChar, 50, "LastName");            
      
      SQLParameters[0].Value = "Smith"; 
      
      SQLParameters[0].Direction = ParameterDirection.Input;
      
      DataSet resultDataSet = null;
      
      resultDataSet = db.ExecuteDataSet(myStoredProcedure, SQLParameters);
      Storeprocedure
      Code:
      ALTER Procedure [dbo].[usp_GetPersonContact]@lastname varchar(50)   
      as 
      select * from Person.Contact where LastName = @lastname

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I don't use C# on a regular basis so I held off answer before now.
        Please review the following article that outline how to use a database. The first article has examples on how to provide parameters to a SqlCommand.

        Database tutorial Part 1
        Database tutorial Part 2

        -Frinny

        Comment

        Working...