Error Converting data type varchar to numeric. .NET application

Hi,

I am getting the above error when calling a stored procedure.

The issue is, how do I find out which one is causing the problem?

Or is it happening in the Stored procedure?


Error
SmashAndGrabAsked:
Who is Participating?
 
Vitor MontalvãoConnect With a Mentor MSSQL Senior EngineerCommented:
It's line 34 of your stored procedure:
INSERT INTO FF_tTrailers (TrailerID, Vendor_Code, Description)
VALUES (@NewTrailer, @Vendor, @Description)

Open in new window

What's the data type for FF_tTrailers..TrailerID?
0
 
Máté FarkasDatabase Developer and AdministratorCommented:
Yes, this message comes from stored procedure (from SQL Server side).
You can test it with calling the UpdateChanges procedure manually in Management Studio by giving parameters as .net code would done.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Review the parameters and see if they match with the giving .NET data types. If they match then must be one of the fields that are not totally numeric (depending on the regional settings a comma or a point can be used for decimal so check also the decimal formats).
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
SmashAndGrabAuthor Commented:
Code:


  cmdUpdate.Parameters.Add("@Reg", SqlDbType.VarChar, 8).Value = tbReg.Text.ToUpper();
            cmdUpdate.Parameters.Add("@Driver", SqlDbType.VarChar, 10).Value = ddlDriver.SelectedValue;
            cmdUpdate.Parameters.Add("@DriverName", SqlDbType.VarChar, 50).Value = tbDriverName.Text;
            cmdUpdate.Parameters.Add("@Remarks", SqlDbType.VarChar, 25).Value = tbRemarks.Text.ToUpper();
            cmdUpdate.Parameters.Add("@Trailer", SqlDbType.VarChar, 8).Value = tbTrailer.Text;
            cmdUpdate.Parameters.Add("@Bay", SqlDbType.Int).Value = Convert.ToInt32(ddlBay.SelectedValue);
            cmdUpdate.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(hidID.Value);
            cmdUpdate.Parameters.Add("@RegOut", SqlDbType.VarChar, 8).Value = tbRegOut.Text;
            cmdUpdate.Parameters.Add("@Inbound", SqlDbType.Bit).Value = (0);

            if (tbTrailer.Text.Length > 0)
            {
                cmdUpdate.Parameters.Add("@Change", SqlDbType.Bit).Value = 1;
                cmdUpdate.Parameters.Add("@NewTrailer", SqlDbType.VarChar, 8).Value = tbTrailer.Text;
                cmdUpdate.Parameters.Add("@Vendor", SqlDbType.VarChar, 8).Value = ddlDriver.SelectedValue;
                cmdUpdate.Parameters.Add("@Description", SqlDbType.VarChar, 50).Value = ddlDriver.SelectedItem.Text;
            }

Open in new window

0
 
SmashAndGrabAuthor Commented:
Stored Proc:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UpdateChanges](@ID			int,
									@Reg		varchar(8),
									@Driver		varchar(10),
									@DriverName varchar(50),
									@Remarks	varchar(25),
									@Trailer	varchar(8),
									@Bay		int,
									@RegOut		varchar(8),
									@Inbound	bit,
									@Change		bit = NULL,
									@NewTrailer	varchar(15) = NULL,
									@Vendor		varchar(8) = NULL,
									@Description varchar(50) = NULL)

AS
	
UPDATE FF_tTransportBookingIn
SET RegistrationIn = @Reg, Contracter = @Driver, Remarks = @Remarks, Trailer = @Trailer, Bay = @Bay, RegistrationOut = @RegOut, InboundGoods = @Inbound, DriverName = @DriverName
WHERE (ID = @ID)

IF @Change = 1

	BEGIN
	
		DECLARE @Count	int
	
		SET @Count = (SELECT COUNT(*) AS Expr1 FROM FF_tTrailers WHERE (TrailerID = @NewTrailer))
	
		IF @Count = 0
		
			BEGIN
	
				INSERT INTO FF_tTrailers (TrailerID, Vendor_Code, Description)
				VALUES (@NewTrailer, @Vendor, @Description)
				
			END
			
		ELSE IF @Count > 0 
		
		BEGIN
		
			UPDATE FF_tTrailers
			SET TrailerID = @NewTrailer
			WHERE (TrailerID = @Trailer)
		
		END
		
		UPDATE FF_tTransportBookingIn
		SET Trailer = @NewTrailer
		WHERE (ID = @ID)
	
	END

SET NOCOUNT ON

Open in new window

0
 
SmashAndGrabAuthor Commented:
Here's my watching varibles (these are the INTs)

codeImage.PNG
0
 
SmashAndGrabAuthor Commented:
Table Definition:

tableDef.PNG
0
 
SmashAndGrabAuthor Commented:
The error is happeing when the ID is:  '75568' so here is the current data for that record:

query.PNG
It all looks ok to me!  So I am a little stuck here!
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
If ID is INT why are you using LIKE and keeping the number between single quotes?
It should be ID = 75568.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
btw, the maximum value for a short integer is 65536. 75568 > 65536 so you should definitely not using LIKE and single quotes. This will make implicit conversion from string to numeric.
0
 
SmashAndGrabAuthor Commented:
Try not to focus on that part as I only did that select query to show you the data of the ID that is causing the issue.

Point taken though :)
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok but I can't see any error. The last screenshot shows that the record has been returned with no errors.
IMO the error is about the data in the form and not with the code. Did you check if all data are valid?
Can you print all parameters before running the ExecuteNonQuery command?
0
 
SmashAndGrabAuthor Commented:
Thank you Vitor - I think you are right.

I think the answer is in the debugging of the stored procedure.

How do I debug the SP?   When I click DEBUG - it just says "The command completed successfully".  I don't have a chance to add all the inputs?
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Debug the .net code and watch the variables values. Hopefully you'll see the bad data.
0
 
SmashAndGrabAuthor Commented:
ok.  1 step closer I think!

I found the data in the bad record (1 of them anyway).

I took this data and ran the Stored Procedure with this data..

SQL
However - I am still not exactly sure where the error lies!  ha!  It references line 34 but that doesn't even exist!
0
 
Pawan KumarConnect With a Mentor Database ExpertCommented:
Yes Vitor is correct. Either TrailerID or Vendor_Code is of type numeric.

And you are passing Nvarchar value to that thats why the conversion error while executing the sp.
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Other thing that I just saw now. You have @Vendor = '' and empty strings can't be converted to numeric. Better use NULL or '0':
INSERT INTO FF_tTrailers (TrailerID, Vendor_Code, Description)
VALUES (@NewTrailer, CASE 
        WHEN @Vendor='' THEN NULL
        ELSE @Vendor END, @Description)

Open in new window

0
 
ZberteocCommented:
Do you need those double quotes inside the parameters value? That could be an issue alone!

You have to debug the stored procedure code to pinpoint the exact statement. You need to get the CREATE code for the sp and to comment the CREATE with parameters part until AS. Then use a declare statement with all the parameters and initialize them with the values from the procedure code. Run the script that way and then double click on the error message in the Massage tab where the results should show. That will jump you to the exact statement in the code that generated the error and you will see that you are trying to insert into a numeric column a non numeric value. If you have double quotes around the value that is your problem but even if you remove them the value could be non numeric.

Here, execute this script after you replace the columns with your real values:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* -- comment this line to uncomment the whole section
ALTER PROCEDURE [dbo].[UpdateChanges](
	@ID			int,
	@Reg		varchar(8),
	@Driver		varchar(10),
	@DriverName varchar(50),
	@Remarks	varchar(25),
	@Trailer	varchar(8),
	@Bay		int,
	@RegOut		varchar(8),
	@Inbound	bit,
	@Change		bit = NULL,
	@NewTrailer	varchar(15) = NULL,
	@Vendor		varchar(8) = NULL,
	@Description varchar(50) = NULL)

AS
--*/

--/* -- uncomment this line to comment the whole section
declare
	@ID			int=75557,
	@Reg		varchar(8)='value here',
	@Driver		varchar(10)='value here',
	@DriverName varchar(50)='value here',
	@Remarks	varchar(25)='value here',
	@Trailer	varchar(8)='value here',
	@Bay		int=28,
	@RegOut		varchar(8)='value here',
	@Inbound	bit=0,
	@Change		bit = 1,
	@NewTrailer	varchar(15)='value here',
	@Vendor		varchar(8)='value here',
	@Description varchar(50)='value here'
--*/
	
UPDATE FF_tTransportBookingIn
SET RegistrationIn = @Reg, Contracter = @Driver, Remarks = @Remarks, Trailer = @Trailer, Bay = @Bay, RegistrationOut = @RegOut, InboundGoods = @Inbound, DriverName = @DriverName
WHERE (ID = @ID)

IF @Change = 1

	BEGIN
	
		DECLARE @Count	int
	
		SET @Count = (SELECT COUNT(*) AS Expr1 FROM FF_tTrailers WHERE (TrailerID = @NewTrailer))
	
		IF @Count = 0
		
			BEGIN
	
				INSERT INTO FF_tTrailers (TrailerID, Vendor_Code, Description)
				VALUES (@NewTrailer, @Vendor, @Description)
				
			END
			
		ELSE IF @Count > 0 
		
		BEGIN
		
			UPDATE FF_tTrailers
			SET TrailerID = @NewTrailer
			WHERE (TrailerID = @Trailer)
		
		END
		
		UPDATE FF_tTransportBookingIn
		SET Trailer = @NewTrailer
		WHERE (ID = @ID)
	
	END

SET NOCOUNT ON

Open in new window

0
 
SmashAndGrabAuthor Commented:
Perfect!  thank you guys!
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.