Solved

Error Converting data type varchar to numeric. .NET application

Posted on 2016-09-26
19
54 Views
Last Modified: 2016-09-26
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
0
Comment
Question by:SmashAndGrab
19 Comments
 
LVL 11

Expert Comment

by:Máté Farkas
Comment Utility
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
 

Author Comment

by:SmashAndGrab
Comment Utility
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
 

Author Comment

by:SmashAndGrab
Comment Utility
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
 

Author Comment

by:SmashAndGrab
Comment Utility
Here's my watching varibles (these are the INTs)

codeImage.PNG
0
 

Author Comment

by:SmashAndGrab
Comment Utility
Table Definition:

tableDef.PNG
0
 

Author Comment

by:SmashAndGrab
Comment Utility
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
If ID is INT why are you using LIKE and keeping the number between single quotes?
It should be ID = 75568.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:SmashAndGrab
Comment Utility
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
 

Author Comment

by:SmashAndGrab
Comment Utility
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
Debug the .net code and watch the variables values. Hopefully you'll see the bad data.
0
 

Author Comment

by:SmashAndGrab
Comment Utility
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
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 250 total points
Comment Utility
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
 
LVL 16

Assisted Solution

by:Pawan Kumar Khowal
Pawan Kumar Khowal earned 250 total points
Comment Utility
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
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
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
 
LVL 26

Expert Comment

by:Zberteoc
Comment Utility
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
 

Author Closing Comment

by:SmashAndGrab
Comment Utility
Perfect!  thank you guys!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now