Must declare the scalar variable - why won't my parameter work? - c#

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

Related

how to get a value from texbox c# to insert to database

i tried to saved a value in DB, but when i make the insert, the DB don`t save any data from texbox when they texbox is load from a javascript, this is my insert:
ConectarBD();
Tabb = new SqlCommand("Insert into peliculas Values(#nombre_pelicula,#dias_arriendo,#valor_dia,#total_arriendo,#descuento,#total_pagar) ", Conn);
Dadap.InsertCommand = Tabb;
Dadap.InsertCommand.Parameters.Add("#nombre_pelicula", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("#dias_arriendo", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("#valor_dia", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("#total_arriendo", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("#descuento", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters.Add("#total_pagar", SqlDbType.VarChar);
Dadap.InsertCommand.Parameters["#nombre_pelicula"].Value = txtNombrePelicula.Text;
Dadap.InsertCommand.Parameters["#dias_arriendo"].Value = txtNumDias.Text;
Dadap.InsertCommand.Parameters["#valor_dia"].Value = txtValorDia.Text;
Dadap.InsertCommand.Parameters["#total_arriendo"].Value = lblTotalArriendo.Text;
Dadap.InsertCommand.Parameters["#descuento"].Value =txtDescuento.Text;
Dadap.InsertCommand.Parameters["#total_pagar"].Value = txtTotalPagar.Text;
Dadap.InsertCommand.ExecuteNonQuery();
lblMensaje.Text = "Datos Grabados";
Conn.Close();
Limpiar();
txtRut.Focus();
Your insert looks wrong, try the following code and change column1,2,3... to columns names in your database.
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "INSERT Into peliculas (Column1, Column1,Column2,Column3,Column4,Column5,Column6) Values(#nombre_pelicula,#dias_arriendo,#valor_dia,#total_arriendo,#descuento,#total_pagar)";
cmd.Connection = con;
cmd.Parameters.AddWithValue("#nombre_pelicula", txtNombrePelicula.Text);
cmd.Parameters.AddWithValue("#dias_arriendo", txtNumDias.Text);
cmd.Parameters.AddWithValue("#valor_dia", txtValorDia.Text);
cmd.Parameters.AddWithValue("#total_arriendo", lblTotalArriendo.Text);
cmd.Parameters.AddWithValue("#descuento", txtDescuento.Text);
cmd.Parameters.AddWithValue("#total_pagar", txtTotalPagar.Text);
}
cmd.ExecuteNonQuery();

Work with foreign key

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());
}

Return Value from a Stored Procedure

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

sqlParameterCollection error

I have a definiton of procedure
public int Add_Nastavenie(out int typNastav, int nastavID, string hod)
{
ResetParameters();
cmd.CommandText = "add_Nastav";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParameter;
var sqlParameterOut = new SqlParameter("#TypNastav", SqlDbType.Int);
sqlParameterOut.Direction = ParameterDirection.Output;
sqlParameter = new SqlParameter("#NastavenieID", SqlDbType.Int);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = nastavID;
cmd.Parameters.Add(sqlParameter);
sqlParameter = new SqlParameter("#Hodnota", SqlDbType.NVarChar, 100);
sqlParameter.Direction = ParameterDirection.Input;
sqlParameter.Value = hod;
cmd.Parameters.Add(sqlParameter);
var sqlParameterRet = new SqlParameter("retValue", SqlDbType.Int);
sqlParameterRet.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
typNastav = (int)sqlParameterOut.Value;
return (int)cmd.Parameters["retvalue"].Value;
}
I call this procedure like this :
dataConnector.Add_Nastavenie(out typNastav,nastavID,hod);
It's show error :
sql parameter with ParameterName 'retvalue' is not contained by this
sqlParameterCollection
How can i fix it ?
You are getting this because you haven't added the return parameter to command. you missed this
cmd.Parameters.Add(sqlParameterRet );
before
cmd.ExecuteNonQuery();

Allow Null values in parameterised SQL query?

I have the following SQL query that inserts the record into a database based on the parameters. It works well but does not allow null values, returning an error if there is a Null.
The table is set up to allow Nulls.
SqlConnection dbconnection = new SqlConnection(#"Data Source=LEWIS;Initial Catalog=CustomPC;Integrated Security=True");
SqlCommand InsertPC = new SqlCommand("INSERT INTO [Custom_PC] ([CPU_ID], [Motherboard_ID],[Graphics_Card_ID],[PSU_ID],[RAM_ID],[Case_ID],[Primary_Storage_Drive_ID],[Secondary_Storage_Drive_ID],[Primary_Optical_Drive_ID],[Secondary_Optical_Drive_ID]) VALUES (#CPU_ID, #Motherboard_ID, #Graphics_Card_ID, #PSU_ID, #RAM_ID, #Case_ID, #Primary_Storage_Drive_ID, #Secondary_Storage_Drive_ID, #Primary_Optical_Drive_ID, #Secondary_Optical_Drive_ID) SET #ReturnedID = Scope_Identity();", dbconnection);
SqlParameter CPU_IDParam = InsertPC.Parameters.Add("#CPU_ID", SqlDbType.Int);
CPU_IDParam.Value = Session["DATA"];
SqlParameter Motherboard_IDParam = InsertPC.Parameters.Add("#Motherboard_ID", SqlDbType.Int);
Motherboard_IDParam.Value = Session["MotherboardID"];
SqlParameter Graphics_Card_IDParam = InsertPC.Parameters.Add("#Graphics_Card_ID", SqlDbType.Int);
Graphics_Card_IDParam.Value = Session["GraphicsID"];
SqlParameter PSU_IDParam = InsertPC.Parameters.Add("#PSU_ID", SqlDbType.Int);
PSU_IDParam.Value = Session["PSUID"];
SqlParameter RAM_IDParam = InsertPC.Parameters.Add("#RAM_ID", SqlDbType.Int);
RAM_IDParam.Value = Session["RAMID"];
SqlParameter Case_IDParam = InsertPC.Parameters.Add("#Case_ID", SqlDbType.Int);
Case_IDParam.Value = Session["CaseID"];
SqlParameter Primary_Storage_Drive_IDParam = InsertPC.Parameters.Add("#Primary_Storage_Drive_ID", SqlDbType.Int);
Primary_Storage_Drive_IDParam.Value = Session["HDD1ID"];
SqlParameter Secondary_Storage_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Storage_Drive_ID", SqlDbType.Int);
Secondary_Storage_Drive_IDParam.Value = Session["HDD2ID"];
SqlParameter Primary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Primary_Optical_Drive_ID", SqlDbType.Int);
Primary_Optical_Drive_IDParam.Value = Session["OpticalDrive1ID"];
SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = Session["OpticalDrive2ID"];
SqlParameter ReturnedIDParam = InsertPC.Parameters.Add("#ReturnedID", SqlDbType.Int);
ReturnedIDParam.Direction = ParameterDirection.Output;
dbconnection.Open();
InsertPC.ExecuteNonQuery();
Session["PCNum"] = (ReturnedIDParam.Value);
Response.Redirect("~/CustomPC_Details.aspx");
You need to check whether the object is null and set your parameter to DBNull.Value.
SqlParameter Secondary_Optical_Drive_IDParam = InsertPC.Parameters.Add("#Secondary_Optical_Drive_ID", SqlDbType.Int);
Secondary_Optical_Drive_IDParam.Value = (object)Session["OpticalDrive2ID"] ?? (object)DBNull.Value;
Note that you need to cast to object because with the ?? operator both possible values must have the same type.

Categories