locked
field is passed as varchar and not int as it is defined RRS feed

  • Question

  • User-359936451 posted

    I have a table with 4 fields 2 are intergers, the keyID and field1.

    When I populate the gridview all of the parameters identify field1 as 'Int32'.

    I subsequently pass the value to a Stored Procedure where it is defined as

    @field1 int

    In the SP I try to use the value and INSERT it into another table where the field is also an defined as an integer.

    I continuously get an error on this field stating that the converison failed for varchar to int on field1.

    Conversion failed when converting the varchar value 'INSERT INTO
    tblPendingDBUpdates (field1) VALUES (' to data type int.

    Any help would be greatly appreciated.

     

     

    Friday, July 29, 2011 4:20 PM

Answers

  • User-359936451 posted

    This was the fix.....

    ''' WHERE [ID] = ' + CAST(@ID as nvarchar(10)) + '; '
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 2, 2011 10:07 AM

All replies

  • User1642208711 posted

    Can you post some code snippet where exactly you are passing the parameters to SP?

    Friday, July 29, 2011 4:38 PM
  • User-359936451 posted

     

    This is the most of it....

     

     

    @StringID_from_Master int
    
    AS		  
    SET NOCOUNT ON; 
    
    DECLARE @dbKEYID as smallint;
    DECLARE @dbName as varchar(100);
    
    
    DECLARE usedIN_DBScursor CURSOR FOR
    SELECT Database_Name, dbKeyID_ofStringName FROM tblDBUsage WHERE tblUniquetblStringsMaster_ID = @StringID_from_Master
    
    OPEN usedIN_DBScursor;
    
    FETCH NEXT FROM usedIN_DBScursor
    INTO @dbName, @dbKEYID;

     

     

     

     

     

     

     

     

     

     

     

     

    Friday, July 29, 2011 4:44 PM
  • User1642208711 posted

    What is the type of the column "dbKeyID_ofStringName" in the table tblDBUsage? If it is of VARCHAR type, then while you are trying to set that column value to the @dbKEYID parameter, you would get exception if there is any non integer value in that column. Becoz @dbKEYID is of type smallint.

    Check whether all the data in the dbKeyID_ofStringName column is integer. Execute the below code.

    SELECT  CONVERT(SMALLINT, dbKeyID_ofStringName)
    FROM     tblDBUsage

    If the above code throws exception, better you may have to change the @dbKEYID type to VARCHAR.

    Friday, July 29, 2011 5:02 PM
  • User-359936451 posted

    It is an integer. I tried smallint as an alternative effort.

    Friday, July 29, 2011 5:17 PM
  • User3866881 posted

    Hello:)

    In my mind, I think smallint is different from int in SQL, and in C#, it's Int16 (short). You when you inseting this value, you should try to use short instead and have a try.

    Thanks again.

    Sunday, July 31, 2011 9:18 PM
  • User1642208711 posted

    Please share relevant code.

    Conversion failed when converting the varchar value 'INSERT INTO
    tblPendingDBUpdates (field1) VALUES (' to data type int.

     I couldn't see the "tblPendingDBUpdates" in the code you have shared. You are trying to insert VARCHAR value in to the column "field1" in the table "tblPendingDBUpdates". That's why the error is.

    Sunday, July 31, 2011 11:10 PM
  • User-359936451 posted

    This was the fix.....

    ''' WHERE [ID] = ' + CAST(@ID as nvarchar(10)) + '; '
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 2, 2011 10:07 AM