C#: ExecuteNonQuery() returns -1 when execute the stored procedure
Posted
by
user1122359
on Stack Overflow
See other posts from Stack Overflow
or by user1122359
Published on 2012-10-17T10:40:05Z
Indexed on
2012/10/17
11:00 UTC
Read the original article
Hit count: 159
I'm trying to execute stored procedure in Visual Studio. Its given below.
CREATE PROCEDURE [dbo].[addStudent]
@stuName varchar(50),
@address varchar(100),
@tel varchar(15),
@etel varchar(15),
@nic varchar (10),
@dob date
AS
BEGIN
SET NOCOUNT ON;
DECLARE @currentID INT
DECLARE @existPerson INT
SET @existPerson = (SELECT p_ID FROM Student WHERE s_NIC = @nic);
IF @existPerson = null
BEGIN
INSERT INTO Person (p_Name, p_RegDate, p_Address, p_Tel, p_EmergeNo, p_Valid, p_Userlevel)
VALUES (@stuName, GETDATE(), @address, @tel, @etel, 0, 'Student' );
SET @currentID = (SELECT MAX( p_ID) FROM Person);
INSERT INTO Student (p_ID, s_Barcode, s_DOB, s_NIC) VALUES (@currentID , NULL, @dob, @nic);
return 0;
END
ELSE
return -1;
END
Im doing so by using this code below.
SqlConnection con = new SqlConnection();
Connect conn = new Connect();
con = conn.getConnected();
con.Open();
cmd = new SqlCommand("addStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@stuName", SqlDbType.VarChar).Value = nameTxt.Text.ToString();
cmd.Parameters.Add("@address", SqlDbType.VarChar).Value = addressTxt.Text.ToString();
cmd.Parameters.Add("@tel", SqlDbType.VarChar).Value = telTxt.Text.ToString();
cmd.Parameters.Add("@etel", SqlDbType.VarChar).Value = emerTxt.Text.ToString();
cmd.Parameters.Add("@nic", SqlDbType.VarChar).Value = nicTxt.Text.ToString();
cmd.Parameters.Add("@dob", SqlDbType.DateTime).Value = dobTime.Value.ToString("MM-dd-yyyy");
int n = cmd.ExecuteNonQuery();
MessageBox.Show(n.ToString());
But it returns me -1. I tried this stored procedure by entering the same values I captured from debugging. It was successful. What can be the possible error? Thanks a lot!
© Stack Overflow or respective owner