I have a table 'Agent' its ID is already inserted before and displayed in a textbox for using it in my insertion , now I try just to add the rest of records but in couldn't do that an error is displayed ...(I'm working in asp with c#) and SQL SERVER :
Violation of PRIMARY KEY constraint 'PK__Agent. "Can not insert duplicate key in object 'dbo.Agent. "The duplicate key value is (1).
The statement has been terminated.
this my code behind :
protected void Button_validerinfo_Click(object sender, EventArgs e)
{
try
{
c.cmd = c.cn.CreateCommand();
c.cmd.CommandText = "AjouterAgent";
c.cmd.CommandType = CommandType.StoredProcedure;
if (c.cn.State == ConnectionState.Closed)
{
c.cn.Open();
}
c.cmd.Parameters.Add("#ppr", SqlDbType.Int);
c.cmd.Parameters.Add("#lieu", SqlDbType.VarChar);
c.cmd.Parameters.Add("#adresspro", SqlDbType.VarChar);
c.cmd.Parameters.Add("#adressperso", SqlDbType.VarChar);
c.cmd.Parameters.Add("#telbureau", SqlDbType.VarChar);
c.cmd.Parameters.Add("#telgsm", SqlDbType.VarChar);
c.cmd.Parameters.Add("#email", SqlDbType.VarChar);
c.cmd.Parameters.Add("#np", SqlDbType.VarChar);
c.cmd.Parameters.Add("#proff", SqlDbType.VarChar);
c.cmd.Parameters.Add("#empl", SqlDbType.VarChar);
c.cmd.Parameters.Add("#retraite", SqlDbType.VarChar);
c.cmd.Parameters.Add("#TypeOperation", SqlDbType.Int);
c.cmd.Parameters["#ppr"].Value = TextBox_PPR.Text;
c.cmd.Parameters["#lieu"].Value = TextBox_ln.Text;
c.cmd.Parameters["#adresspro"].Value = TextBox_adrspro.Text;
c.cmd.Parameters["#adressperso"].Value = TextBox_adrssperso.Text;
c.cmd.Parameters["#telbureau"].Value = TextBox_bureau.Text;
c.cmd.Parameters["#telgsm"].Value = TextBox_gsm.Text;
c.cmd.Parameters["#email"].Value = TextBox_email.Text;
c.cmd.Parameters["#np"].Value = TextBox_npconj.Text;
c.cmd.Parameters["#proff"].Value = TextBox_prof.Text;
c.cmd.Parameters["#empl"].Value = TextBox_empl.Text;
c.cmd.Parameters["#retraite"].Value = DropDownList_retraite.SelectedValue;
c.cmd.Parameters["#TypeOperation"].Value = 0;
c.cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
if (c.cn.State == ConnectionState.Open)
{
c.cn.Close();
}
}
}
and my stroredprocedure :
USE [CVtech]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[AjouterAgent]
#ppr int,
#lieu varchar(100),
#adresspro varchar(100),
#adressperso varchar(100),
#telbureau varchar(100),
#telgsm varchar(100),
#email varchar(100),
#np varchar(100),
#proff varchar(100),
#empl varchar(100),
#retraite varchar(3),
#TypeOperation nvarchar(1)
as
if(#TypeOperation = '0')
begin tran
if exists ( select ppr from Agent where PPR = #ppr)
begin
insert into Agent (LieuNaissance,AdressePro, AdressePerso,TelBureau,TelPerso,Email)
values (#lieu,#adresspro, #adressperso,#telbureau,#telgsm,#email)
end
insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (#ppr ,#np ,#proff,#empl,#retraite)
commit
Does your table have Identity Specification switched to off? To check go on your table, right click, design, select the primary key, in the options displayed you should see IDENTITY SPECIFICATION. This must be set to yes in your case. For more information about this, you can view this link from MSDN : http://msdn.microsoft.com/en-us/library/x5s13zy2.aspx
Obviously, if you want to do an update, you must execute an update statement, and not insert the same record again.
Yes, first my question is, are you send the column value (which one has primary key constrain) from user? if no mean check that column has the identity or not(if no mean change it) if yes then you have to pass the unique value for that textbox
USE [CVtech]
GO
/****** Object: StoredProcedure [dbo].[AjouterAgent] Script Date: 02/04/2014 12:00:08 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[AjouterAgent]
#ppr int,
#lieu varchar(100),
#adresspro varchar(100),
#adressperso varchar(100),
#telbureau varchar(100),
#telgsm varchar(100),
#email varchar(100),
#np varchar(100),
#proff varchar(100),
#empl varchar(100),
#retraite varchar(3),
#TypeOperation nvarchar(1)
as
if(#TypeOperation = '0')
begin tran
if exists ( select ppr from Agent where PPR = #ppr)
begin
update Agent set LieuNaissance=#lieu,AdressePro=#adresspro, AdressePerso=#adressperso , TelBureau=#telbureau,TelPerso=#telgsm,Email=#email
where PPR = #ppr
end
if exists ( select ppr from Agent where PPR = #ppr)
begin
update Conjoint set PPR=#ppr,NomPrenom =#np, Profession=#proff, Employeur=#empl, Retraite=#retraite where ppr= #ppr
end
else
insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (#ppr ,#np ,#proff,#empl,#retraite)
commit
So the code would be this:
ALTER proc [dbo].[AjouterAgent]
#ppr int,
#lieu varchar(100),
#adresspro varchar(100),
#adressperso varchar(100),
#telbureau varchar(100),
#telgsm varchar(100),
#email varchar(100),
#np varchar(100),
#proff varchar(100),
#empl varchar(100),
#retraite varchar(3),
#TypeOperation nvarchar(1)
as
if(#TypeOperation = '0')
begin tran
if exists ( select ppr from Agent where PPR = #ppr)
begin
UPDATE Agent
SET LieuNaissance = #lieu
,AdressePro = #adresspro
,AdressePerso = #adressperso
,TelBureau = #telbureau
,TelPerso = #telgsm
,Email = #email
WHERE ppr = #ppr
end
insert into Conjoint (PPR,NomPrenom , Profession, Employeur, Retraite) values (#ppr ,#np ,#proff,#empl,#retraite)
commit
It doesn't seem to make sense that you are trying to do an insert where some value in Agent exists.
if exists ( select ppr from Agent where PPR = #ppr)
begin
insert into Agent (LieuNaissance,AdressePro, AdressePerso,TelBureau,TelPerso,Email)
values (#lieu,#adresspro, #adressperso,#telbureau,#telgsm,#email)
end
I would expect that if the item exists that you want to do an UPDATE not an INSERT.
Related
I need a little help with this. The stored procedure below doesn't seem to ever match exiting unique identifier
ALTER PROCEDURE [dbo].[spInsertUpdateThisStuff]
#Id uniqueidentifier OUTPUT,
#Content nvarchar(255)
AS
BEGIN
SET NOCOUNT ON;
DECLARE #tAudit table (id uniqueidentifier)
IF EXISTS(SELECT * FROM [dbo].[myData] WHERE [ID] = #Id)
-- UPDATE
BEGIN
UPDATE [dbo].[myData]
SET [ID] = #ID,
[Content] = #Content
OUTPUT inserted.[ID] INTO #tAudit
WHERE [ID] = #Id
SELECT id FROM #tAudit
END
ELSE
BEGIN
-- INSERT
SET #ID = NEWID()
INSERT INTO [dbo].CBData ([ID], [Content])
OUTPUT inserted.[ID] INTO #tAudit
VALUES(#Id, #Content)
SELECT id FROM #tAudit
END;
SET #ID = (SELECT id FROM #tAudit);
END
the C#
cmd.Parameters.Add("#ID", SqlDbType.UniqueIdentifier).Value = (currentRecord.ID == null) ? Guid.Empty : currentRecord.ID;
cmd.Parameters["#ID"].Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
currentRecord.ID = Guid.Parse(cmd.Parameters["#ID"].Value.ToString());
It seems the first IF statement does not ever become true, but if test (SELECT * FROM [dbo].[myData] WHERE [ID] = #Id) with a the matching UID it comes back with data.
This statement is problematic,,
DECLARE #tAudit table (id uniqueidentifier)
IF EXISTS(SELECT * FROM [dbo].[myData] WHERE [ID] = #Id)
-- UPDATE
BEGIN
#id is an Output Parameter and is null by default and you are trying to check that..
basically NEWID() won't be duplicated,so update part is redundant
How are you declaring the parameter in your code? Is it ParameterDirection.Output?
Try changing it to ParameterDirection.InputOutput. Your SQL looks okay. And you can pass an input value to a parameter declared as OUTPUT in your stored procedure. So maybe it's just that ParameterDirection.
You can also change this
IF EXISTS(SELECT * FROM [dbo].[myData] WHERE [ID] = #Id)
to
IF #Id IS NOT NULL AND EXISTS(SELECT * FROM [dbo].[myData] WHERE [ID] = #Id)
If your ID column isn't nullable then it's going to work the same either way. But this is a little more explicit, showing that it's recognized that #Id might be null because it's an OUTPUT parameter.
This is my fix.
This is my new stored procedure
ALTER PROCEDURE [dbo].[spInsertUpdatemyData]
#ID uniqueidentifier,
#IDOut uniqueidentifier OUTPUT,
#CONTENT nvarchar(255)
AS
BEGIN
--SET NOCOUNT ON;
DECLARE #tAudit table (outputID uniqueidentifier)
IF EXISTS(SELECT * FROM [dbo].[myData] WHERE [ID] = #ID)
-- UPDATE
BEGIN
UPDATE [dbo].[CBData]
SET [ID] = #ID,
[Content] = #Content
OUTPUT inserted.[ID] INTO #tAudit
WHERE [ID] = #ID
SELECT outputID FROM #tAudit;
END
ELSE
BEGIN
-- INSERT
INSERT INTO [dbo].myData
([ID],[Content])
OUTPUT inserted.[ID] INTO #tAudit
VALUES(NEWID(),#Content);
SELECT outputID FROM #tAudit
END;
set #IDOut = (SELECT outputID FROM #tAudit);
END
and the relative C#
//Add Parameter for output to sql command then Change Direction of parameter
cmd.Parameters.Add("#IDOut", SqlDbType.UniqueIdentifier).Value = Guid.Empty ;
cmd.Parameters["#IDOut"].Direction = ParameterDirection.InputOutput;
cmd.ExecuteNonQuery();
currentRecord.ID = Guid.Parse(cmd.Parameters["#IDOut"].Value.ToString());
cmd.Transaction.Commit();
I have this procedure
create proc Insert_New_Emp
#EMP_Name varchar(50),
#EMP_Email varchar(50),
#EMP_Role varchar (10),
#Username varchar(50),
#password varchar(50),
#EMP_Phone varchar(15),
#EMP_Department varchar(50),
#Question varchar(200)
as
begin
insert into EMP_Info (EMP_Name, EMP_Email, EMP_Role, Username, password,
EMP_Phone, EMP_Department, Question)
values (#EMP_Name, #EMP_Email, #EMP_Role, #Username, #password,
#EMP_Phone, #EMP_Department, #Question)
if (exists(select EMP_Role from EMP_Info where EMP_Role ='Admin' ))
return 'Admin'
else
return 'Employee'
end
I need to check the procedure code in Windows Form C#
When the return Admin do something and else do something
A RETURN statement can return an integer code, with zero indicating success and non-zero for warning/error. It is not intended to return data. To return a scalar value back to the application, use either an OUTPUT parameter or SELECT statement. Examples below.
CREATE PROC Insert_New_Emp
#EMP_Name varchar(50)
, #EMP_Email varchar(50)
, #EMP_Role varchar(10)
, #Username varchar(50)
, #password varchar(50)
, #EMP_Phone varchar(15)
, #EMP_Department varchar(50)
, #Question varchar(200)
, #EmpType varchar(8) OUTPUT
AS
BEGIN
INSERT INTO EMP_Info
( EMP_Name
, EMP_Email
, EMP_Role
, Username
, password
, EMP_Phone
, EMP_Department
, Question
)
VALUES ( #EMP_Name
, #EMP_Email
, #EMP_Role
, #Username
, #password
, #EMP_Phone
, #EMP_Department
, #Question
);
IF EXISTS ( SELECT EMP_Role
FROM EMP_Info
WHERE EMP_Role = 'Admin' )
SET #EmpType = 'Admin'
ELSE
SET #EmpType = 'Employee';
END;
GO
CREATE PROC Insert_New_Emp
#EMP_Name varchar(50)
, #EMP_Email varchar(50)
, #EMP_Role varchar(10)
, #Username varchar(50)
, #password varchar(50)
, #EMP_Phone varchar(15)
, #EMP_Department varchar(50)
, #Question varchar(200)
AS
BEGIN
INSERT INTO EMP_Info
( EMP_Name
, EMP_Email
, EMP_Role
, Username
, password
, EMP_Phone
, EMP_Department
, Question
)
VALUES ( #EMP_Name
, #EMP_Email
, #EMP_Role
, #Username
, #password
, #EMP_Phone
, #EMP_Department
, #Question
);
IF EXISTS ( SELECT EMP_Role
FROM EMP_Info
WHERE EMP_Role = 'Admin' )
SELECT 'Admin' AS EmpType
ELSE
SELECT 'Employee' AS EmpType;
END;
GO
I will suggest you to modify your SP to return an integer value, because a Stored Proc can only return Integer values. Modiy it like this:-
Declare #ReturnVal INT
if (exists(select EMP_Role from EMP_Info where EMP_Role ='Admin' ))
SET #ReturnVal = 1 -- return 1 for Admin
else
SET #ReturnVal = 2 --return 2 for Employee
Return #ReturnVal
end
Then you can call this SP from your ADO.NET code as:-
SqlParameter returnParam = cmd.Parameters.Add("ReturnVal", SqlDbType.Int);
returnParam.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
//Read the returned value.
int UserType= (int) returnParam.Value;
Also, It would be better if you define an Enum in your code like this and use this in your code rather than hard-coding in SP:-
public enum EmployeeType
{
Admin = 1,
Employee = 2
}
If you want to execute storedproc from c# code then you need to do as below :
private void button1_Click(object sender, EventArgs e) {
using (SqlConnection con = new SqlConnection(dc.Con)) {
using (SqlCommand cmd = new SqlCommand("sp_Add_contact", con)) {
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = txtFirstName.Text;
cmd.Parameters.Add("#LastName", SqlDbType.VarChar).Value = txtLastName.Text;
con.Open();
cmd.ExecuteNonQuery();
}
}
}
Just replace the procedure name from 'sp_Add_contact' and add your own parameters with the data type as defined in your stored proc
Original answer : here
I have a windows form where the user can input multiple values in multiple textboxes for faster search results. But when running, it takes only 1st parameter i.e., the fullname and ignores the other. Don't know the reason why is it so. Am getting the full table in the result which I don't want. I have created a stored procedure.
CREATE PROCEDURE USP_SELECT_CUSTOMERS (
#fullName VARCHAR(100) = '',
#Address VARCHAR(100) = '',
#ContactNumber VARCHAR(15) = ''
)
AS BEGIN
SELECT * FROM Customers
WHERE ((ContactName LIKE #fullName+'%') OR (#fullName = ''))
OR ((Address LIKE #Address+'%') OR (#Address = ''))
OR ((Phone LIKE #ContactNumber+'%') OR (#ContactNumber = ''))
END
Here's how am i calling the stored procedure in my program :=>
private void btnSubmit_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlCommand cmd = new SqlCommand("USP_SELECT_CUSTOMERS", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#fullName", txtFullName.Text);
cmd.Parameters.AddWithValue("#Address", txtAddress.Text);
cmd.Parameters.AddWithValue("#ContactNumber", txtContactNumber.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
dataGridView1.Visible = true;
dataGridView1.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
con.Close();
}
}
What I want is even if the 1 textbox if filled, the stored procedure should give the desired output. Kindly help me out in this.
Thanks.
You should AND instead of OR. Try this:
CREATE PROCEDURE USP_SELECT_CUSTOMERS (
#fullName VARCHAR(100),
#Address VARCHAR(100),
#ContactNumber VARCHAR(15)
)
AS BEGIN
SELECT * FROM Customers
WHERE ((ContactName LIKE #fullName+'%') OR (#fullName = ''))
AND ((Address LIKE #Address+'%') OR (#Address = ''))
AND ((Phone LIKE #ContactNumber+'%') OR (#ContactNumber = ''))
END
It will give you the desired result.
See SQL Fiddle
EDIT:
As per your requirement, use this query:
CREATE PROCEDURE USP_SELECT_CUSTOMERS
(
#fullName VARCHAR(100),
#Address VARCHAR(100),
#ContactNumber VARCHAR(15)
)
AS
BEGIN
IF LEN(#fullName)=0
SET #fullName='$$$'
IF LEN(#Address)=0
SET #Address='$$$'
IF LEN(#ContactNumber)=0
SET #ContactNumber='$$$'
SELECT * FROM Customers
WHERE ((ContactName LIKE #fullName+'%'))
OR ((Address LIKE #Address+'%'))
OR ((Phone LIKE #ContactNumber+'%'))
END
Added a simple trick. See SQL Fiddle.
Replace $$$ with anything which will not be included in ContactName,Address and Phone fields.
I agree with what #Baljeet said ... it should fetch the desired results.
Make Sure you have done the following
1) Intitilise the the parameter value as '' (blank) if varchar
eg #fullName VARCHAR(100) = ''
and in where condition like given below, people tend to make mistake in the second condtion of OR.
Make sure the condtion is Where ( 'Field Name' like '#parametername' OR #parametername ='')
WHERE ((ContactName LIKE #fullName+'%') OR (#fullName = ''))
As well as carrying out the changes in Raging Bull's answer, try adding ISNULL to your second check for each parameter. So
#fullname = ''
becomes
ISNULL(#fullname, '') = ''
and do the same for the other two parameters.
From memory Sql Server will only use the default value you specify for the parameters, if you don't pass the parameter at all, if you do pass the parameter but the value is null then your query will only work with the change above.
My suggestion would be to define the stored procedure with default value of parameters as NULL. Database is good at handling NULL values and it would keep the stored procedure definition simple.
Then pass the parameters from C# conditionally:
if(!String.IsNullOrEmpty(txtFullName.Text))
cmd.Parameters.AddWithValue("#fullName", txtFullName.Text);
//...add other parameters the same way
Stored procedure should be defined as below:
CREATE PROCEDURE USP_SELECT_CUSTOMERS (
#fullName VARCHAR(100) = NULL,
#Address VARCHAR(100) = NULL,
#ContactNumber VARCHAR(15) = NULL
)
AS BEGIN
SELECT * FROM Customers
WHERE ContactName LIKE #fullName+'%'
OR Address LIKE #Address +'%'
OR Phone LIKE #ContactNumber+'%'
--in case you want to select all rows if all the parameters are NULL then add
--this condition too
--OR 1 = CASE WHEN COALESCE(#fullName, #Address, #ContactNumber,'') = '' THEN 1 ELSE 0 END
END
IMO,
this should do
CREATE PROCEDURE USP_SELECT_CUSTOMERS (
#fullName VARCHAR(100) = '',
#Address VARCHAR(100) = '',
#ContactNumber VARCHAR(15) = ''
)
AS BEGIN
SELECT * FROM Customers
WHERE ((ContactName LIKE #fullName+'%') OR (#fullName = ''))
AND ((Address LIKE #Address+'%') OR (#Address = ''))
AND ((Phone LIKE #ContactNumber+'%') OR (#ContactNumber = ''))
END
using northwind i get following , command with EXEC USP_SELECT_CUSTOMERS 'maria','',''
exec USP_SELECT_CUSTOMERS 'maria','Ã…kergatan 24',''
--result row is displayed now check the next pic
** exec USP_SELECT_CUSTOMERS 'maria','Ã…kergatan 24 ','' **
--because of extra space end of address parameter you could see no result
I saw few solutions here but none worked. I tried SCOPE_IDENTITY() didn't wotk. the id is autoincrement.
This is my stored procedure
CREATE PROCEDURE [dbo].[uploadVid]
#video varbinary(MAx),
#vidTitle varchar(50),
#vidCategory varchar(50),
#vidDate date,
#vidDescription varchar(Max),
#vidName varchar(50),
#vidSize bigint
AS
INSERT INTO Video(video, vidTitle, vidCategory, vidDate, vidDescription, vidName, vidSize)
VALUES (#video, #vidTitle, #vidCategory, #vidDate, #vidDescription, #vidName, #vidSize)
& in the back end I tried
Object i = register.ExecuteScalar();
&
int newId = (Int32)register.ExecuteScalar();
I put a break point and it gave me a value of null or 0. any help appreciated
Thanks
try this
CREATE PROCEDURE [dbo].[uploadVid]
#video varbinary(MAx),
#vidTitle varchar(50),
#vidCategory varchar(50),
#vidDate date,
#vidDescription varchar(Max),
#vidName varchar(50),
#vidSize bigint
AS
begin
declare #id as int --assuming your identity column is int
INSERT INTO Video(video, vidTitle, vidCategory, vidDate, vidDescription, vidName, vidSize)
VALUES (#video, #vidTitle, #vidCategory, #vidDate, #vidDescription, #vidName, #vidSize)
set #id = scope_identity()
select #id --return the value for executescaler to catch it
end
Probably you don't execute the commands in the correct sequence: (supposing you are using an SQL Server DB)
SqlCommand cmd = new SqlCommand("uploadVid", connnection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(.....)
......
// After adding the parameters you execute the command to insert the new row....
int rowsInserted = cmd.ExecuteNonQuery();
if(rowsInserted > 0)
{
SqlCommand cmd1 = new SqlCommand("SELECT SCOPE_IDENTITY()", connection);
int newID = (int)cmd1.ExecuteScalar();
}
ExecuteScalar returns only the value of the first row in the first column of the query executed. In case of an insert this value is meaningless. You need an ExecuteNonQuery that returns the rows inserted by the command. After that run a new command with the SELECT SCOPE_IDENTITY() command text with the ExecuteScalar. This, of course, if you cannot modify the SP, the answer from th1rdey3 is better if you could change the proc because it avoids a run-trip to the database.
I am trying to write procedure for insert value into table but the value not inserted in table
for reference I am give code which i had try for it...can anyone please help me
for insert i am try this code
public int Visitor_Insert(visitor_Master visitor_obj, common_Class comm_obj)
{
DB_Connection();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "INSERT_VISITOR";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("#VISITOR_NAME", visitor_obj.VisitorName);
cmd.Parameters.AddWithValue("#VISITOR_CONTACT", visitor_obj.VisitorContact);
cmd.Parameters.AddWithValue("#VISITOR_ADDRESS", visitor_obj.VisitorAddress);
cmd.Parameters.AddWithValue("#VISITOR_AGE", visitor_obj.VisitorAge);
cmd.Parameters.AddWithValue("#VISITOR_VEHICLENO", visitor_obj.VisitorVehicleNO);
//cmd.Parameters.AddWithValue("#VISITOR_AGE ", visitor_obj.VisitorAge);
int i = cmd.ExecuteNonQuery();
return i;
}
For Procedure I am written this code
USE [VPMS]
GO
/****** Object: StoredProcedure [dbo].[INSERT_VISITOR] Script Date: 07/24/2013 16:01:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[INSERT_VISITOR]
#VISITOR_NAME NVARCHAR(30),
#VISITOR_CONTACT NVARCHAR(10),
#VISITOR_ADDRESS NVARCHAR(40),
#VISITOR_AGE NVARCHAR(2),
#VISITOR_VEHICLENO NVARCHAR(20),
#VISITOR_IMAGE IMAGE,
#VISITOR_CREATEDBY NVARCHAR(20),
#VISITOR_CREDTEDON NVARCHAR(20),
#VISITOR_MODIFIEDBY NVARCHAR(20),
#VISITOR_MODIFIEDON NVARCHAR(20)
AS
DECLARE #VISITOR_ID INT,
BEGIN
SET NOCOUNT ON;
SELECT #VISITOR_ID= MAX(Visitor_Id) FROM Visitor_Master
IF (#VISITOR_ID > 0 )
BEGIN
SET #VISITOR_ID=#VISITOR_ID+1
END
ELSE
BEGIN
SET #VISITOR_ID=10
END
INSERT INTO Visitor_Master( Visitor_Id, VisitorName, Visitor_Contact, Visitor_Address, Visitor_age,
Visitor_VehicleNo, Visitor_Image, Created_By, Created_On, Modify_By, Modify_On)
VALUES(#VISITOR_ID,#VISITOR_NAME,#VISITOR_CONTACT,#VISITOR_ADDRESS,#VISITOR_AGE,#VISITOR_VEHICLENO,#VISITOR_IMAGE,#VISITOR_CREATEDBY,
#VISITOR_CREDTEDON,#VISITOR_MODIFIEDBY,#VISITOR_MODIFIEDON)
END
As you are not passing all the parameters defined in the procedure. It will throw exception.
Here I have assigned null to other parameters which you have not specified assign value as per your reqirement
ALTER PROCEDURE [dbo].[INSERT_VISITOR]
#VISITOR_NAME NVARCHAR(30),
#VISITOR_CONTACT NVARCHAR(10),
#VISITOR_ADDRESS NVARCHAR(40),
#VISITOR_AGE NVARCHAR(2),
#VISITOR_VEHICLENO NVARCHAR(20),
#VISITOR_IMAGE IMAGE = null,
#VISITOR_CREATEDBY NVARCHAR(20) = null,
#VISITOR_CREDTEDON NVARCHAR(20) = null,
#VISITOR_MODIFIEDBY NVARCHAR(20) = null,
#VISITOR_MODIFIEDON NVARCHAR(20) = null
-- rest of code
You have #VISITOR_IMAGE,#VISITOR_CREATEDBY,#VISITOR_CREDTEDON,#VISITOR_MODIFIEDBY,#VISITOR_MODIFIEDON parameters in your procedure but you have not passed value to the the above parameter.Either pass value to all the parameters or allow null
BEGIN
set #VISITOR_IMAGE = null,
set #VISITOR_CREATEDBY = null,
set #VISITOR_CREDTEDON = null,
set #VISITOR_MODIFIEDBY = null,
set #VISITOR_MODIFIEDON = null
........