I used the following statement for a parameter.
comm.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = txtname.Text;
Name is a SQL Server nvarchar column but I get this error:
Error converting data type nvarchar to numeric.
My sql:
SqlCommand comm = conn.CreateCommand();
comm.CommandText = "execute addName @name";
comm.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = txtname.Text;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
Can help my to fix this problem?
My store procedure:
USE [info]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[addName]
@id numeric(18,0) = 0,
@name nchar(50)
as
if (select name from TName where name = @name) is null
begin
select @id = MAX(id)+1 from TName
insert into TName
values (@id, @name)
print @id
end
else
begin
print 'Eroare'
end
With the edit, with the signature:
then the problem becomes clear:
is pass-by-position – so you are passing the value of
@nameinto the@idparameter. If you were calling this from TSQL, to pass-by-name you need to use:The first (left) states the parameter name, the second (right) states the value to use for this parameter; for example, to pass a literal into
addName‘s@nameparameter:However, from ADO.NET, a better approach is:
which automatically treats it as an SP-exec using pass-by-name.