I am using the old SqlClient to insert some data with a loop. I have added all the parameters correctly but I get an exception thrown that says
"Must declare the scalar variable \"#KeyProject\"."
string query = #"INSERT INTO dbo.ImportedProjects
(
KeyProject,
KeyCompany,
KeyCountry,
KeyCustomer,
KeyEmployeeProjectManager,
KeyEmployeeProjectOwner,
KeyOrganisation,
ProjectNumber,
ProjectName,
ProjectOwnerNumber,
ProjectManagerNumber,
ProjectOwnerName,
ProjectManagerName,
ProjectOwnerInitials,
ProjectManagerInitials,
CustomerNumber,
CustomerName,
CreatedDate,
ProjectStatus,
ProjectOpenClosed
) VALUES (
#KeyProject,
#KeyCompany,
#KeyCountry,
#KeyCustomer,
#KeyEmployeeProjectManager,
#KeyEmployeeProjectOwner,
#KeyOrganisation,
#ProjectNumber,
#ProjectName,
#ProjectOwnerNumber,
#ProjectManagerNumber,
#ProjectOwnerName,
#ProjectManagerName,
#ProjectOwnerInitials,
#ProjectManagerInitials,
#CustomerNumber,
#CustomerName,
#CreatedDate,
#ProjectStatus,
#ProjectOpenClosed
)";
SqlConnection sqlCon = new SqlConnection("Server=localhost;Database=DatabaseName;Trusted_Connection=True;");
SqlCommand idInsertCms = new SqlCommand("SET IDENTITY_INSERT dbo.ImportedProjects ON", sqlCon);
SqlCommand sqlCmd= new SqlCommand(query, sqlCon);
try
{
sqlCon.Open();
idInsertCms.ExecuteNonQuery();
foreach (var item in importedProjects)
{
sqlCmd.Parameters.Add("#KeyProject", SqlDbType.Int);
sqlCmd.Parameters["#KeyProject"].Value = item.KeyProject;
sqlCmd.Parameters.Add("#KeyCompany", SqlDbType.Int);
sqlCmd.Parameters["#KeyCompany"].Value = item.KeyCompany;
sqlCmd.Parameters.Add("#KeyCountry", SqlDbType.Int);
sqlCmd.Parameters["#KeyCountry"].Value = item.KeyCountry;
sqlCmd.Parameters.Add("#KeyCustomer", SqlDbType.Int);
sqlCmd.Parameters["#KeyCustomer"].Value = item.KeyCustomer;
sqlCmd.Parameters.Add("#KeyEmployeeProjectManager", SqlDbType.Int);
sqlCmd.Parameters["#KeyEmployeeProjectManager"].Value = item.KeyEmployeeProjectManager;
sqlCmd.Parameters.Add("#KeyEmployeeProjectOwner", SqlDbType.Int);
sqlCmd.Parameters["#KeyEmployeeProjectOwner"].Value = item.KeyEmployeeProjectOwner;
sqlCmd.Parameters.Add("#KeyOrganisation", SqlDbType.Int);
sqlCmd.Parameters["#KeyOrganisation"].Value = item.KeyOrganisation;
sqlCmd.Parameters.Add("#ProjectNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectNumber"].Value = item.ProjectNumber;
sqlCmd.Parameters.Add("#ProjectName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectName"].Value = item.ProjectName;
sqlCmd.Parameters.Add("#ProjectOwnerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerNumber"].Value = item.ProjectOwnerNumber;
sqlCmd.Parameters.Add("#ProjectManagerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerNumber"].Value = item.ProjectManagerNumber;
sqlCmd.Parameters.Add("#ProjectOwnerName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerName"].Value = item.ProjectOwnerName;
sqlCmd.Parameters.Add("#ProjectManagerName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerName"].Value = item.ProjectManagerName;
sqlCmd.Parameters.Add("#ProjectOwnerInitials", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerInitials"].Value = item.ProjectOwnerInitials;
sqlCmd.Parameters.Add("#ProjectManagerInitials", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerInitials"].Value = item.ProjectManagerInitials;
sqlCmd.Parameters.Add("#CustomerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#CustomerNumber"].Value = item.CustomerNumber;
sqlCmd.Parameters.Add("#CustomerName", SqlDbType.VarChar);
sqlCmd.Parameters["#CustomerName"].Value = item.CustomerName;
sqlCmd.Parameters.Add("#CreatedDate", SqlDbType.DateTime);
sqlCmd.Parameters["#CreatedDate"].Value = item.CreatedDate;
sqlCmd.Parameters.Add("#ProjectStatus", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectStatus"].Value = item.ProjectStatus;
sqlCmd.Parameters.Add("#ProjectOpenClosed", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOpenClosed"].Value = item.ProjectOpenClosed;
sqlCmd.Parameters.Clear();
sqlCmd.ExecuteNonQuery();
}
idInsertCms.CommandText = "SET IDENTITY_INSERT dbo.ImportedProjects OFF";
idInsertCms.ExecuteNonQuery();
What's wrong with my code?
How about you create a new SqlCommand for each iteration like this:
string query = #"INSERT INTO dbo.ImportedProjects
(
KeyProject,
KeyCompany,
KeyCountry,
KeyCustomer,
KeyEmployeeProjectManager,
KeyEmployeeProjectOwner,
KeyOrganisation,
ProjectNumber,
ProjectName,
ProjectOwnerNumber,
ProjectManagerNumber,
ProjectOwnerName,
ProjectManagerName,
ProjectOwnerInitials,
ProjectManagerInitials,
CustomerNumber,
CustomerName,
CreatedDate,
ProjectStatus,
ProjectOpenClosed
) VALUES (
#KeyProject,
#KeyCompany,
#KeyCountry,
#KeyCustomer,
#KeyEmployeeProjectManager,
#KeyEmployeeProjectOwner,
#KeyOrganisation,
#ProjectNumber,
#ProjectName,
#ProjectOwnerNumber,
#ProjectManagerNumber,
#ProjectOwnerName,
#ProjectManagerName,
#ProjectOwnerInitials,
#ProjectManagerInitials,
#CustomerNumber,
#CustomerName,
#CreatedDate,
#ProjectStatus,
#ProjectOpenClosed
)";
SqlConnection sqlCon = new SqlConnection("Server=localhost;Database=DatabaseName;Trusted_Connection=True;");
SqlCommand idInsertCms = new SqlCommand("SET IDENTITY_INSERT dbo.ImportedProjects ON", sqlCon);
try
{
sqlCon.Open();
idInsertCms.ExecuteNonQuery();
foreach (var item in importedProjects)
{
SqlCommand sqlCmd= new SqlCommand(query, sqlCon);
sqlCmd.Parameters.Add("#KeyProject", SqlDbType.Int);
sqlCmd.Parameters["#KeyProject"].Value = item.KeyProject;
sqlCmd.Parameters.Add("#KeyCompany", SqlDbType.Int);
sqlCmd.Parameters["#KeyCompany"].Value = item.KeyCompany;
sqlCmd.Parameters.Add("#KeyCountry", SqlDbType.Int);
sqlCmd.Parameters["#KeyCountry"].Value = item.KeyCountry;
sqlCmd.Parameters.Add("#KeyCustomer", SqlDbType.Int);
sqlCmd.Parameters["#KeyCustomer"].Value = item.KeyCustomer;
sqlCmd.Parameters.Add("#KeyEmployeeProjectManager", SqlDbType.Int);
sqlCmd.Parameters["#KeyEmployeeProjectManager"].Value = item.KeyEmployeeProjectManager;
sqlCmd.Parameters.Add("#KeyEmployeeProjectOwner", SqlDbType.Int);
sqlCmd.Parameters["#KeyEmployeeProjectOwner"].Value = item.KeyEmployeeProjectOwner;
sqlCmd.Parameters.Add("#KeyOrganisation", SqlDbType.Int);
sqlCmd.Parameters["#KeyOrganisation"].Value = item.KeyOrganisation;
sqlCmd.Parameters.Add("#ProjectNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectNumber"].Value = item.ProjectNumber;
sqlCmd.Parameters.Add("#ProjectName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectName"].Value = item.ProjectName;
sqlCmd.Parameters.Add("#ProjectOwnerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerNumber"].Value = item.ProjectOwnerNumber;
sqlCmd.Parameters.Add("#ProjectManagerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerNumber"].Value = item.ProjectManagerNumber;
sqlCmd.Parameters.Add("#ProjectOwnerName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerName"].Value = item.ProjectOwnerName;
sqlCmd.Parameters.Add("#ProjectManagerName", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerName"].Value = item.ProjectManagerName;
sqlCmd.Parameters.Add("#ProjectOwnerInitials", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOwnerInitials"].Value = item.ProjectOwnerInitials;
sqlCmd.Parameters.Add("#ProjectManagerInitials", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectManagerInitials"].Value = item.ProjectManagerInitials;
sqlCmd.Parameters.Add("#CustomerNumber", SqlDbType.VarChar);
sqlCmd.Parameters["#CustomerNumber"].Value = item.CustomerNumber;
sqlCmd.Parameters.Add("#CustomerName", SqlDbType.VarChar);
sqlCmd.Parameters["#CustomerName"].Value = item.CustomerName;
sqlCmd.Parameters.Add("#CreatedDate", SqlDbType.DateTime);
sqlCmd.Parameters["#CreatedDate"].Value = item.CreatedDate;
sqlCmd.Parameters.Add("#ProjectStatus", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectStatus"].Value = item.ProjectStatus;
sqlCmd.Parameters.Add("#ProjectOpenClosed", SqlDbType.VarChar);
sqlCmd.Parameters["#ProjectOpenClosed"].Value = item.ProjectOpenClosed;
sqlCmd.ExecuteNonQuery();
}
idInsertCms.CommandText = "SET IDENTITY_INSERT dbo.ImportedProjects OFF";
idInsertCms.ExecuteNonQuery();
SqlCommand sqlCmd= new SqlCommand(query, sqlCon);
sqlCmd.Parameters.Add("#KeyProject", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCompany", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCountry", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCustomer", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyEmployeeProjectManager", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyEmployeeProjectOwner", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyOrganisation", SqlDbType.Int);
sqlCmd.Parameters.Add("#ProjectNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerInitials", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerInitials", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CustomerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CustomerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CreatedDate", SqlDbType.DateTime);
sqlCmd.Parameters.Add("#ProjectStatus", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOpenClosed", SqlDbType.VarChar);
foreach (var item in importedProjects)
{
sqlCmd.Parameters["#KeyProject"].Value = item.KeyProject;
sqlCmd.Parameters["#KeyCompany"].Value = item.KeyCompany;
sqlCmd.Parameters["#KeyCountry"].Value = item.KeyCountry;
sqlCmd.Parameters["#KeyCustomer"].Value = item.KeyCustomer;
sqlCmd.Parameters["#KeyEmployeeProjectManager"].Value = item.KeyEmployeeProjectManager;
sqlCmd.Parameters["#KeyEmployeeProjectOwner"].Value = item.KeyEmployeeProjectOwner;
sqlCmd.Parameters["#KeyOrganisation"].Value = item.KeyOrganisation;
sqlCmd.Parameters["#ProjectNumber"].Value = item.ProjectNumber;
sqlCmd.Parameters["#ProjectName"].Value = item.ProjectName;
sqlCmd.Parameters["#ProjectOwnerNumber"].Value = item.ProjectOwnerNumber;
sqlCmd.Parameters["#ProjectManagerNumber"].Value = item.ProjectManagerNumber;
sqlCmd.Parameters["#ProjectOwnerName"].Value = item.ProjectOwnerName;
sqlCmd.Parameters["#ProjectManagerName"].Value = item.ProjectManagerName;
sqlCmd.Parameters["#ProjectOwnerInitials"].Value = item.ProjectOwnerInitials;
sqlCmd.Parameters["#ProjectManagerInitials"].Value = item.ProjectManagerInitials;
sqlCmd.Parameters["#CustomerNumber"].Value = item.CustomerNumber;
sqlCmd.Parameters["#CustomerName"].Value = item.CustomerName;
sqlCmd.Parameters["#CreatedDate"].Value = item.CreatedDate;
sqlCmd.Parameters["#ProjectStatus"].Value = item.ProjectStatus;
sqlCmd.Parameters["#ProjectOpenClosed"].Value = item.ProjectOpenClosed;
sqlCmd.ExecuteNonQuery();
}
or you could recycle sqlCmd and setup the parameters outside the loop and then just set them within the loop:
SqlCommand sqlCmd= new SqlCommand(query, sqlCon);
sqlCmd.Parameters.Add("#KeyProject", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCompany", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCountry", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyCustomer", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyEmployeeProjectManager", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyEmployeeProjectOwner", SqlDbType.Int);
sqlCmd.Parameters.Add("#KeyOrganisation", SqlDbType.Int);
sqlCmd.Parameters.Add("#ProjectNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOwnerInitials", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectManagerInitials", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CustomerNumber", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CustomerName", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#CreatedDate", SqlDbType.DateTime);
sqlCmd.Parameters.Add("#ProjectStatus", SqlDbType.VarChar);
sqlCmd.Parameters.Add("#ProjectOpenClosed", SqlDbType.VarChar);
foreach (var item in importedProjects)
{
sqlCmd.Parameters["#KeyProject"].Value = item.KeyProject;
sqlCmd.Parameters["#KeyCompany"].Value = item.KeyCompany;
sqlCmd.Parameters["#KeyCountry"].Value = item.KeyCountry;
sqlCmd.Parameters["#KeyCustomer"].Value = item.KeyCustomer;
sqlCmd.Parameters["#KeyEmployeeProjectManager"].Value = item.KeyEmployeeProjectManager;
sqlCmd.Parameters["#KeyEmployeeProjectOwner"].Value = item.KeyEmployeeProjectOwner;
sqlCmd.Parameters["#KeyOrganisation"].Value = item.KeyOrganisation;
sqlCmd.Parameters["#ProjectNumber"].Value = item.ProjectNumber;
sqlCmd.Parameters["#ProjectName"].Value = item.ProjectName;
sqlCmd.Parameters["#ProjectOwnerNumber"].Value = item.ProjectOwnerNumber;
sqlCmd.Parameters["#ProjectManagerNumber"].Value = item.ProjectManagerNumber;
sqlCmd.Parameters["#ProjectOwnerName"].Value = item.ProjectOwnerName;
sqlCmd.Parameters["#ProjectManagerName"].Value = item.ProjectManagerName;
sqlCmd.Parameters["#ProjectOwnerInitials"].Value = item.ProjectOwnerInitials;
sqlCmd.Parameters["#ProjectManagerInitials"].Value = item.ProjectManagerInitials;
sqlCmd.Parameters["#CustomerNumber"].Value = item.CustomerNumber;
sqlCmd.Parameters["#CustomerName"].Value = item.CustomerName;
sqlCmd.Parameters["#CreatedDate"].Value = item.CreatedDate;
sqlCmd.Parameters["#ProjectStatus"].Value = item.ProjectStatus;
sqlCmd.Parameters["#ProjectOpenClosed"].Value = item.ProjectOpenClosed;
sqlCmd.ExecuteNonQuery();
}
Instead of inserting in a loop, you can use a different approach, try creating a table and add rows to this table with your values, once you finishing adding your rows to this table, you can send this table as parameter to your stored procedure, in SQL Server create a user-defined table with the same columns as your table created in the loop and use this type as type in the parameter, after this you can do an insert into your table using a select
var newDataRow = medicareBuyInBuyInDataTable.NewRow();
foreach (var value in values) {
newDataRow["FieldName"] = value;
....
....
}
yourDataTable.Rows.Add(newDataRow);
// send your table as parameter
var parameter = new List<KeyValuePair<string, object>>{
new KeyValuePair<string, object>("#yourTableParam", medicareBuyInDataTable)
};
return DbAccess.ExecuteScalar(ConString, StoredProcedureName, parameter);
In SQL Server create a type with the same values in the table you are sending:
-- ================================
-- Create User-defined Table Type
-- ================================
USE <database_name, sysname, AdventureWorks>
GO
-- Create the data type
CREATE TYPE YourTableTypeName AS TABLE
(
<columns_in_primary_key, , c1> <column1_datatype, , int> <column1_nullability,, NOT NULL>,
<column2_name, sysname, c2> <column2_datatype, , char(10)> <column2_nullability,, NULL>,
<column3_name, sysname, c3> <column3_datatype, , datetime> <column3_nullability,, NULL>,
PRIMARY KEY (<columns_in_primary_key, , c1>)
)
GO
Finally, in the stored procedure, use the type you created "YourTableTypeName"
CREATE PROCEDURE [dbo].[YourProcedureName]
#yourTableParam [dbo].[YourTableTypeName] READONLY
AS
BEGIN
INSERT INTO [dbo].[YourTable]
SELECT *
FROM yourTableParam
END
In my application has been conflicts with the primary key constraint and a foreign key. I use stored procedures in a programming language c # and SqlDataReader. How to add data to the foreign keys in the database, that there was not primary key constraint. As the example of a way to organize this?
That my code for firstTable:
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#io", SqlDbType.Int);
cmd.Parameters["#io"].Direction = ParameterDirection.Input;
cmd.Parameters["#io"].Value = 1;
cmd.Parameters.Add("#idstud", SqlDbType.BigInt);
cmd.Parameters["#idstud"].Direction = ParameterDirection.Input;
cmd.Parameters["#idstud"].Value = id_studtextBox.Text;
cmd.Parameters.Add("#numb_stud", SqlDbType.Int);
cmd.Parameters["#numb_stud"].Direction = ParameterDirection.Input;
cmd.Parameters["#numb_stud"].Value = numberstudtextbox.Text;
cmd.Parameters.Add("#naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["#naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["#naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("#place", SqlDbType.Int);
cmd.Parameters["#place"].Direction = ParameterDirection.Input;
cmd.Parameters["#place"].Value = placetextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_id_stud].ToString(),
(string)rdr[_nomer_stud].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_mesto].ToString());
}
That code for secondTable
cmd = new SqlCommand(STORED_PREFIX + STORED_NAME2, connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#daw", SqlDbType.Int);
cmd.Parameters["#daw"].Direction = ParameterDirection.Input;
cmd.Parameters["#daw"].Value = 3;
cmd.Parameters.Add("#teachid", SqlDbType.BigInt);
cmd.Parameters["#teachid"].Direction = ParameterDirection.Input;
cmd.Parameters["#teachid"].Value = id_cabtextBox.Text;
cmd.Parameters.Add("#numb_teacher", SqlDbType.Int);
cmd.Parameters["#numb_teacher"].Direction = ParameterDirection.Input;
cmd.Parameters["#numb_teacher"].Value = numbercabtextbox.Text;
cmd.Parameters.Add("#naim_corpus", SqlDbType.NVarChar);
cmd.Parameters["#naim_corpus"].Direction = ParameterDirection.Input;
cmd.Parameters["#naim_corpus"].Value = naimcorpuscombobox.Text;
cmd.Parameters.Add("#studid", SqlDbType.Int);
cmd.Parameters["#studid"].Direction = ParameterDirection.Input;
cmd.Parameters["#studid"].Value = studidtextbox.Text;
rdr = cmd.ExecuteReader();
while (rdr.Read())
{
dataGridView2.Rows.Add((string)rdr[_teacher_id].ToString(),
(string)rdr[_nomer_teacher].ToString(),
(string)rdr[_naim_corpus].ToString(),
(string)rdr[_studid].ToString());
}
I am trying to get data returned from my stored procedure. This is what I have tried and absolutely nothing is coming back.
Stored Proc:
Declare #Submit_ID as int
Set #Submit_ID = (Select (Max(Sub_ID) + 1) from Sub_Details);
INSERT INTO.....
Return #Submit_ID
C# (2.0)
SqlConnection conn = null;
int subid;
try
{
conn = new SqlConnection(conn_string);
SqlCommand cmd = new SqlCommand("INSERT_INTO_MYDB", conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter p1 = new SqlParameter("#FirstName", SqlDbType.NVarChar, 255);
p1.Direction = ParameterDirection.Input;
p1.Value = txtFirstName.Text;
cmd.Parameters.Add(p1);
SqlParameter p2 = new SqlParameter("#LastName", SqlDbType.NVarChar, 255);
p2.Direction = ParameterDirection.Input;
p2.Value = txtLastName.Text;
cmd.Parameters.Add(p2);
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub);
}
You're not adding the return parameter to the command:
SqlParameter pSub = new SqlParameter("#Sub_ID", SqlDbType.Int);
pSub.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(pSub); // <------------- add to command
conn.Open();
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(cmd.Parameters["pSub"].Value);
First add parameter to cmd and use .Value to its value
cmd.Parameters.Add(pSub); // add parameters to command
cmd.ExecuteNonQuery();
subid = Convert.ToInt32(pSub.Value); // Use .Value
I have a register form and I want to check that the user did not register before.
My code is in below. I think two problems exist: (1) the call method and (2) passing the parameter to the stored procedure. My symptom is that this causes an exception that says the input parameter not initialized.
create procedure fakeuser #username nvarchar(250),#codemeli nchar(10),#email nvarchar(50), #user nvarchar(250) output,#code nchar(10)output,#mail nvarchar(50)output
as
if exists(select username,email,codemeli from karbar where username=#username)
set #user=#username
else if exists(select username,email,codemeli from karbar where codemeli=#codemeli)
set #code=#codemeli
else if exists(select username,email,codemeli from karbar where email=#email)
set #mail= #email
Here is the c# code:
public static string confirm(string username, string email, string codemeli)
{
string constring = "data source=.;database=site;integrated security=true;";
SqlConnection connection = new SqlConnection(constring);
// Command - specify as StoredProcedure
SqlCommand command = new SqlCommand("fakeuser", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter param = new SqlParameter("#username", SqlDbType.NVarChar);
param.Direction = ParameterDirection.Input;
param.Value = username;
command.Parameters.Add(param);
SqlParameter param2 = new SqlParameter("#email", SqlDbType.NVarChar);
param2.Direction = ParameterDirection.Input;
param2.Value = username;
command.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("#codemeli", SqlDbType.NChar);
param3.Direction = ParameterDirection.Input;
param3.Value = username;
command.Parameters.Add(param3);
// Return value as parameter
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar);
returnuser.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnuser);
SqlParameter returncode = new SqlParameter("#code", SqlDbType.NChar);
returncode.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returncode);
SqlParameter returnmail = new SqlParameter("#mail", SqlDbType.NVarChar);
returnmail.Direction = ParameterDirection.ReturnValue;
command.Parameters.Add(returnmail);
// Execute the stored procedure
connection.Open();
command.ExecuteNonQuery();
connection.Close();
Thank you.
Set ParameterDirection=Output for #user,#code, and #mail and also specify the width/size/length of parameter.
SqlParameter returnuser = new SqlParameter("#user", SqlDbType.NVarChar,100);
returnuser.Direction = ParameterDirection.Output;
command.Parameters.Add(returnuser);