Click here to Skip to main content
15,900,460 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
In below code i used multiple queries in SP. when i passed type 2 from C# code its expecting @noofpersons as a error. which is not necessary in that select query
SQL
CREATE PROCEDURE [dbo].[RoomDetails_Insertquery]
(
@type int,
@roomno bigint,
@noofpersons bigint,
@roomtype nvarchar(150),
@amt bigint
)
AS
BEGIN
	IF @type=1
BEGIN
	INSERT INTO roomdetails VALUES (@roomno,@noofpersons,@roomtype,@amt)
END

ELSE IF @type=2
BEGIN
	SELECT * FROM roomdetails WHERE roomno=@roomno
END
ELSE IF @type=3
BEGIN
UPDATE roomdetails SET noofpersons=@noofpersons,roomtype=@roomtype,@amt=amt WHERE roomno=@roomno
END   
END
Posted
Updated 22-Aug-12 18:57pm
v2
Comments
ssd_coolguy 23-Aug-12 0:54am    
can you show us how you called your sp?
Umapathi K 23-Aug-12 0:58am    
SqlCommand cmd = new SqlCommand("RoomDetails_Insertquery", gn.cn());
cmd.Parameters.AddWithValue("@type", 2);
cmd.Parameters.AddWithValue("@roomno", textBox1.Text);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.SelectCommand = cmd;
ssd_coolguy 23-Aug-12 1:04am    
see the solution 2...

while calling stored proc pass Null
SQL
exec  [dbo].[RoomDetails_Insertquery] @type=2,@roomno=200,@noofpersons=null,@roomtype='something',
@amt=2000


or in the procedure,
while declare @noopersons set it as default
SQL
CREATE PROCEDURE [dbo].[RoomDetails_Insertquery]
(
@type int,
@roomno bigint,
@noofpersons bigint =null,
@roomtype nvarchar(150),
@amt bigint 
)


and you can pass only the other 4 parameters
 
Share this answer
 
try below code:-

As per your sp,you must pass the all parameters to sp..
VB
SqlCommand cmd = new SqlCommand("RoomDetails_Insertquery", gn.cn());
                  cmd.Parameters.AddWithValue("@type", 2);
                  cmd.Parameters.AddWithValue("@roomno", textBox1.Text);
                  cmd.Parameters.AddWithValue("@noofpersons", Null);
                  cmd.Parameters.AddWithValue("@roomtype",  Null);
                  cmd.Parameters.AddWithValue("@amt", Null); 
                  cmd.CommandType = CommandType.StoredProcedure;
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  DataTable dt = new DataTable();
                  da.SelectCommand = cmd;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900