C# Sql Data Reader error - c#

I have a web form with 11 text boxes and 1 radio button list (Containing two LI's).
txtCustID - Required (Generated by getting the max CustID number from database and incrementing value)
txtFirstname - Requried
txtSurname - Required
rdbGender -> Male, Female - Required
txtAge - Required
txtAddress1 - Requried
txtAddress2 - Not required
txtCity - Required
txtPhone - Required
txtMobile - Not required
txtEmail1 - Required
txtEmail2 - Required (Used to confirm Email1, not passed to DB)
There's a button, btnAdd which passes the text values to a stored procedure "AddRecord" in the database.
It is returning an error "Procedure or function 'AddRecord' expects parameter '#Surname', which was not supplied."
I can't find the issue, as #Surname is supplied?
Here's the btnClick event code:
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pSurn.ParameterName = "#Gender";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.Int;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pSurn.ParameterName = "#Address1";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pSurn.ParameterName = "#Address2";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pSurn.ParameterName = "#City";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pSurn.ParameterName = "#Phone";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pSurn.ParameterName = "#Mobile";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pSurn.ParameterName = "#Email";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}
Here's the stored procedure code:
CREATE PROCEDURE AddRecord
#CustID INT,
#First VARCHAR,
#Surname VARCHAR,
#Gender VARCHAR,
#Age INT,
#Address1 VARCHAR,
#Address2 VARCHAR,
#City VARCHAR,
#Phone VARCHAR,
#Mobile VARCHAR,
#Email VARCHAR
AS
INSERT INTO Customer
VALUES
(#CustID, #First, #Surname, #Gender, #Age, #Address1, #Address2,
#City, #Phone, #Mobile, #Email);

You have referenced same varible after initialization of pSurn to rest of all Parameters and also give parameter name to '#Surname' for Age parameter...
Try below
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pGen.ParameterName = "#Gender";
pGen.SqlDbType = SqlDbType.VarChar;
pGen.Direction = ParameterDirection.Input;
pGen.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pAge.ParameterName = "#Age";
pAge.SqlDbType = SqlDbType.Int;
pAge.Direction = ParameterDirection.Input;
pAge.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pAdd1.ParameterName = "#Address1";
pAdd1.SqlDbType = SqlDbType.VarChar;
pAdd1.Direction = ParameterDirection.Input;
pAdd1.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pAdd2.ParameterName = "#Address2";
pAdd2.SqlDbType = SqlDbType.VarChar;
pAdd2.Direction = ParameterDirection.Input;
pAdd2.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pCity.ParameterName = "#City";
pCity.SqlDbType = SqlDbType.VarChar;
pCity.Direction = ParameterDirection.Input;
pCity.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pPhone.ParameterName = "#Phone";
pPhone.SqlDbType = SqlDbType.VarChar;
pPhone.Direction = ParameterDirection.Input;
pPhone.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pMobile.ParameterName = "#Mobile";
pMobile.SqlDbType = SqlDbType.VarChar;
pMobile.Direction = ParameterDirection.Input;
pMobile.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pEmail.ParameterName = "#Email";
pEmail.SqlDbType = SqlDbType.VarChar;
pEmail.Direction = ParameterDirection.Input;
pEmail.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}

There are a mass amounts of items here that are suspect.
Never do the auto incrementing yourself. All databases that I am aware of have the ability to do this for you.
Change your variable name for each parameter. Looks as though you got to pSurn and did some copying and pasting. This code just really needs clean up work more than anything.
This is really not much of an answer but I hope this moves you in the right direction.

try this
protected void btnAdd_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "AddRecord";
command.Connection.Open();
SqlParameter pCustID = new SqlParameter();
pCustID.ParameterName = "#CustID";
pCustID.SqlDbType = SqlDbType.Int;
pCustID.Direction = ParameterDirection.Input;
pCustID.Value = txtCustID.Text;
command.Parameters.Add(pCustID);
SqlParameter pFirst = new SqlParameter();
pFirst.ParameterName = "#First";
pFirst.SqlDbType = SqlDbType.VarChar;
pFirst.Direction = ParameterDirection.Input;
pFirst.Value = txtFirstName.Text;
command.Parameters.Add(pFirst);
SqlParameter pSurn = new SqlParameter();
pSurn.ParameterName = "#Surname";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtSurname.Text;
command.Parameters.Add(pSurn);
SqlParameter pGen = new SqlParameter();
pSurn.ParameterName = "#Gender";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = rblGender.SelectedValue;
command.Parameters.Add(pGen);
SqlParameter pAge = new SqlParameter();
pSurn.ParameterName = "#Age";
pSurn.SqlDbType = SqlDbType.Int;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAge.Text;
command.Parameters.Add(pAge);
SqlParameter pAdd1 = new SqlParameter();
pSurn.ParameterName = "#Address1";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress1.Text;
command.Parameters.Add(pAdd1);
SqlParameter pAdd2 = new SqlParameter();
pSurn.ParameterName = "#Address2";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtAddress2.Text;
command.Parameters.Add(pAdd2);
SqlParameter pCity = new SqlParameter();
pSurn.ParameterName = "#City";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtCity.Text;
command.Parameters.Add(pCity);
SqlParameter pPhone = new SqlParameter();
pSurn.ParameterName = "#Phone";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtPhone.Text;
command.Parameters.Add(pPhone);
SqlParameter pMobile = new SqlParameter();
pSurn.ParameterName = "#Mobile";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtMobile.Text;
command.Parameters.Add(pMobile);
SqlParameter pEmail = new SqlParameter();
pSurn.ParameterName = "#Email";
pSurn.SqlDbType = SqlDbType.VarChar;
pSurn.Direction = ParameterDirection.Input;
pSurn.Value = txtEmail1.Text;
command.Parameters.Add(pEmail);
SqlDataReader reader = command.ExecuteReader();
reader.Dispose();
command.Dispose();
conn.Dispose();
}
}
in your age field you are using #Surname .change that and try again

Related

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

SQL Command Parameters

I have an asp.net MVC web app that adds data to a sql database. It has been working fine with the following code:
query = "insert into ImageNameTable(ImageName,NotifyDate,NotifyDate2) values(#ImageName,#NotifyDate,#NotifyDate2)";
SqlParameter StringParameter = new SqlParameter();
StringParameter.SqlDbType = SqlDbType.VarChar;
StringParameter.ParameterName = "#ImageName";
StringParameter.Value = imageName;
DateTime now = DateTime.Now;
now = now.AddMilliseconds(-now.Millisecond);
DateTime notifyDate = now.AddDays(1);
DateTime notify = DateTime.Now;
DateTime aNotify = DateTime.Now;
notify = notify.AddMilliseconds(-notify.Millisecond);
notify = notify.AddMinutes(1);
aNotify = notify.AddMinutes(1);
SqlParameter notifyDateParameter = new SqlParameter();
notifyDateParameter.SqlDbType = SqlDbType.DateTime;
notifyDateParameter.ParameterName = "#NotifyDate";
notifyDateParameter.Value = notify;
SqlParameter notifyDate2Parameter = new SqlParameter();
notifyDate2Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate2Parameter.ParameterName = "#NotifyDate2";
notifyDate2Parameter.Value = aNotify;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(StringParameter);
cmd.Parameters.Add(notifyDateParameter);
cmd.Parameters.Add(notifyDate2Parameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
This was used for testing purposes. So now I have changed it to what I actually want it to do, and nothing is appearing in the sql database. Essentially, all I have done is added parameters to the sql command. There is no error, just that it doesn't appear in the database. Here is the change:
query = "insert into ImageNameTable(ImageName,NotifyDate,NotifyDate2,NotifyDate3,NotifyDate4,NotifyDate5,NotifyDate6,NotifyDate7,NotifyDate8,NotifyDate9,NotifyDate10,NotifyDate11,NotifyDate12,NotifyDate13,NotifyDate14) values(#ImageName,#NotifyDate,#NotifyDate2,#NotifyDate3,#NotifyDate4,#NotifyDate5,#NotifyDate6,#NotifyDate7,#NotifyDate8,#NotifyDate9,#NotifyDate10,#NotifyDate11,#NotifyDate12,#NotifyDate13,#NotifyDate14)";
SqlParameter StringParameter = new SqlParameter();
StringParameter.SqlDbType = SqlDbType.VarChar;
StringParameter.ParameterName = "#ImageName";
StringParameter.Value = imageName;
DateTime now = DateTime.Now;
now = now.AddMilliseconds(-now.Millisecond);
DateTime notifyDate = now.AddDays(1);
DateTime notify = DateTime.Now;
DateTime aNotify = DateTime.Now;
notify = notify.AddMilliseconds(-notify.Millisecond);
notify = notify.AddHours(1);
aNotify = notify.AddHours(5);
DateTime bNotify = aNotify.AddDays(1);
DateTime cNotify = bNotify.AddDays(1);
DateTime dNotify = cNotify.AddDays(2);
DateTime eNotify = dNotify.AddDays(2);
DateTime fNotify = eNotify.AddDays(3);
DateTime gNotify = fNotify.AddDays(4);
DateTime hNotify = gNotify.AddDays(7);
DateTime iNotify = hNotify.AddDays(14);
DateTime jNotify = bNotify.AddMonths(2);
DateTime kNotify = bNotify.AddMonths(3);
DateTime lNotify = bNotify.AddMonths(4);
DateTime mNotify = bNotify.AddMonths(6);
SqlParameter notifyDateParameter = new SqlParameter();
notifyDateParameter.SqlDbType = SqlDbType.DateTime;
notifyDateParameter.ParameterName = "#NotifyDate";
notifyDateParameter.Value = notify;
SqlParameter notifyDate2Parameter = new SqlParameter();
notifyDate2Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate2Parameter.ParameterName = "#NotifyDate2";
notifyDate2Parameter.Value = aNotify;
SqlParameter notifyDate3Parameter = new SqlParameter();
notifyDate3Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate3Parameter.ParameterName = "#NotifyDate3";
notifyDate3Parameter.Value = bNotify;
SqlParameter notifyDate4Parameter = new SqlParameter();
notifyDate4Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate4Parameter.ParameterName = "#NotifyDate4";
notifyDate4Parameter.Value = cNotify;
SqlParameter notifyDate5Parameter = new SqlParameter();
notifyDate5Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate5Parameter.ParameterName = "#NotifyDate5";
notifyDate5Parameter.Value = dNotify;
SqlParameter notifyDate6Parameter = new SqlParameter();
notifyDate6Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate6Parameter.ParameterName = "#NotifyDate6";
notifyDate6Parameter.Value = eNotify;
SqlParameter notifyDate7Parameter = new SqlParameter();
notifyDate7Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate7Parameter.ParameterName = "#NotifyDate7";
notifyDate7Parameter.Value = fNotify;
SqlParameter notifyDate8Parameter = new SqlParameter();
notifyDate8Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate8Parameter.ParameterName = "#NotifyDate8";
notifyDate8Parameter.Value = gNotify;
SqlParameter notifyDate9Parameter = new SqlParameter();
notifyDate9Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate9Parameter.ParameterName = "#NotifyDate9";
notifyDate9Parameter.Value = hNotify;
SqlParameter notifyDate10Parameter = new SqlParameter();
notifyDate10Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate10Parameter.ParameterName = "#NotifyDate10";
notifyDate10Parameter.Value = iNotify;
SqlParameter notifyDate11Parameter = new SqlParameter();
notifyDate11Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate11Parameter.ParameterName = "#NotifyDate11";
notifyDate11Parameter.Value = jNotify;
SqlParameter notifyDate12Parameter = new SqlParameter();
notifyDate12Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate12Parameter.ParameterName = "#NotifyDate12";
notifyDate12Parameter.Value = kNotify;
SqlParameter notifyDate13Parameter = new SqlParameter();
notifyDate13Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate3Parameter.ParameterName = "#NotifyDate13";
notifyDate13Parameter.Value = lNotify;
SqlParameter notifyDate14Parameter = new SqlParameter();
notifyDate14Parameter.SqlDbType = SqlDbType.DateTime;
notifyDate14Parameter.ParameterName = "#NotifyDate14";
notifyDate14Parameter.Value = mNotify;
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(StringParameter);
cmd.Parameters.Add(notifyDateParameter);
cmd.Parameters.Add(notifyDate2Parameter);
cmd.Parameters.Add(notifyDate3Parameter);
cmd.Parameters.Add(notifyDate4Parameter);
cmd.Parameters.Add(notifyDate5Parameter);
cmd.Parameters.Add(notifyDate6Parameter);
cmd.Parameters.Add(notifyDate7Parameter);
cmd.Parameters.Add(notifyDate8Parameter);
cmd.Parameters.Add(notifyDate9Parameter);
cmd.Parameters.Add(notifyDate10Parameter);
cmd.Parameters.Add(notifyDate11Parameter);
cmd.Parameters.Add(notifyDate12Parameter);
cmd.Parameters.Add(notifyDate13Parameter);
cmd.Parameters.Add(notifyDate14Parameter);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
I'm wondering if I have used too many parameters, but I've been told that the max amount of parameters a command can hold is way more than 14. Does anyone know what is going on here?
notifyDate3Parameter.ParameterName = "#NotifyDate13"; - note 3 and 13 to the left and to the right of the assignment sign, respectively.

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

The SqlParameter is already contained by another SqlParameterCollection - command.Parameters.Clear() does not work

I am getting desperate here. I cant figure out why does this exception occur. I searched and read about this problem and it seems everyone managed to solve it with command.Parameters.Clear(), but this does not work for me. Here is my code:
public class ProductsDataAccess : Connection
{
public static SqlParameter[] createParams(Products prod)
{
SqlParameter idParam = new SqlParameter("#id", prod.prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter nameParam = new SqlParameter("#name", prod.prodName);
nameParam.SqlDbType = System.Data.SqlDbType.VarChar;
nameParam.Size = 45;
nameParam.IsNullable = false;
nameParam.Direction = ParameterDirection.Input;
SqlParameter demandParam = new SqlParameter("#demand", prod.demand);
demandParam.SqlDbType = System.Data.SqlDbType.Int;
demandParam.IsNullable = false;
demandParam.Direction = ParameterDirection.Input;
SqlParameter demandLTParam = new SqlParameter("#demandLT", prod.demandLT);
demandLTParam.SqlDbType = System.Data.SqlDbType.Int;
demandLTParam.IsNullable = false;
demandLTParam.Direction = ParameterDirection.Input;
SqlParameter leadTimeParam = new SqlParameter("#leadTime", prod.leadTime);
leadTimeParam.SqlDbType = System.Data.SqlDbType.Int;
leadTimeParam.IsNullable = false;
leadTimeParam.Direction = ParameterDirection.Input;
SqlParameter fixedCoastParam = new SqlParameter("#k", prod.k);
fixedCoastParam.SqlDbType = System.Data.SqlDbType.Float;
fixedCoastParam.IsNullable = false;
fixedCoastParam.Direction = ParameterDirection.Input;
SqlParameter cParam = new SqlParameter("#c", prod.c);
cParam.SqlDbType = System.Data.SqlDbType.Float;
cParam.IsNullable = false;
cParam.Direction = ParameterDirection.Input;
SqlParameter chPrecentageParam = new SqlParameter("#chPrecent", prod.precentOfC);
chPrecentageParam.SqlDbType = System.Data.SqlDbType.Int;
chPrecentageParam.IsNullable = false;
chPrecentageParam.Direction = ParameterDirection.Input;
SqlParameter chParam = new SqlParameter("#ch", prod.ch);
chParam.SqlDbType = System.Data.SqlDbType.Float;
chParam.IsNullable = false;
chParam.Direction = ParameterDirection.Input;
SqlParameter optimalQParam = new SqlParameter("#q", prod.q);
optimalQParam.SqlDbType = System.Data.SqlDbType.Int;
optimalQParam.IsNullable = false;
optimalQParam.Direction = ParameterDirection.Input;
SqlParameter annualChParam = new SqlParameter("#annualCh", prod.annualCh);
annualChParam.SqlDbType = System.Data.SqlDbType.Float;
annualChParam.IsNullable = false;
annualChParam.Direction = ParameterDirection.Input;
SqlParameter annualKParam = new SqlParameter("#annualK", prod.annualK);
annualKParam.SqlDbType = System.Data.SqlDbType.Float;
annualKParam.IsNullable = false;
annualKParam.Direction = ParameterDirection.Input;
SqlParameter optimalTACParam = new SqlParameter("#tac", prod.tac);
optimalTACParam.SqlDbType = System.Data.SqlDbType.Float;
optimalTACParam.IsNullable = false;
optimalTACParam.Direction = ParameterDirection.Input;
SqlParameter rParam = new SqlParameter("#r", prod.r);
rParam.SqlDbType = System.Data.SqlDbType.Int;
rParam.IsNullable = false;
rParam.Direction = ParameterDirection.Input;
SqlParameter inventoryLvlParam = new SqlParameter("#invLvl", prod.inventoryLvl);
inventoryLvlParam.SqlDbType = System.Data.SqlDbType.Int;
inventoryLvlParam.IsNullable = false;
inventoryLvlParam.Direction = ParameterDirection.Input;
SqlParameter statusParam = new SqlParameter("#status", prod.status);
statusParam.SqlDbType = System.Data.SqlDbType.VarChar;
statusParam.Size = 10;
statusParam.IsNullable = false;
statusParam.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, nameParam, demandParam, demandLTParam,
leadTimeParam, fixedCoastParam, cParam,
chPrecentageParam, chParam, optimalQParam,
annualChParam, annualKParam, optimalTACParam,
rParam, inventoryLvlParam, statusParam };
return paramList;
}
public static void insertProduct(Products product)
{
String comStr = "insert into Products(prodID, name, demand, demandLT, leadTime, fixedCoast, " +
"c, chAsPrecentage, ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus) " +
"values(#id, #name, #demand, #demandLT, #leadTime, #k, #c, #chPrecent, " +
"#ch, #q, #annualCh, #annualK, #tac, #r, #invLvl, #status)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(product));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear()
command.Dispose();
}
public static DataTable getAllProducts()
{
String comStr = "select prodID, name, demand, demandLT, leadTime, fixedCoast, c, chAsPrecentage, " +
"ch, optimalQ, annualCh, annualK, optimalTAC, r, inventoryLvl, prodStatus from Products";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
DataTable table = new DataTable("allProduct");
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand=command;
openConnection();
adapter.Fill(table);
command.Dispose();
closeConnection();
return table;
}
}
Here is the other class in which I am using sqlParameters:
public class StatisticsDataAccess : Connection
{
public static SqlParameter[] createParams(Statistics stat, string prodID)
{
SqlParameter idParam = new SqlParameter("#id", prodID);
idParam.SqlDbType = System.Data.SqlDbType.VarChar;
idParam.Size = 45;
idParam.IsNullable = false;
idParam.Direction = ParameterDirection.Input;
SqlParameter m1Param = new SqlParameter("#m1", stat.month1);
m1Param.SqlDbType = System.Data.SqlDbType.Int;
m1Param.IsNullable = false;
m1Param.Direction = ParameterDirection.Input;
SqlParameter m2Param = new SqlParameter("#m2", stat.month2);
m2Param.SqlDbType = System.Data.SqlDbType.Int;
m2Param.IsNullable = false;
m2Param.Direction = ParameterDirection.Input;
SqlParameter m3Param = new SqlParameter("#m3", stat.month3);
m3Param.SqlDbType = System.Data.SqlDbType.Int;
m3Param.IsNullable = false;
m3Param.Direction = ParameterDirection.Input;
SqlParameter m4Param = new SqlParameter("#m4", stat.month4);
m4Param.SqlDbType = System.Data.SqlDbType.Int;
m4Param.IsNullable = false;
m4Param.Direction = ParameterDirection.Input;
SqlParameter m5Param = new SqlParameter("#m5", stat.month5);
m5Param.SqlDbType = System.Data.SqlDbType.Int;
m5Param.IsNullable = false;
m5Param.Direction = ParameterDirection.Input;
SqlParameter m6Param = new SqlParameter("#m6", stat.month6);
m6Param.SqlDbType = System.Data.SqlDbType.Int;
m6Param.IsNullable = false;
m6Param.Direction = ParameterDirection.Input;
SqlParameter m7Param = new SqlParameter("#m7", stat.month7);
m7Param.SqlDbType = System.Data.SqlDbType.Int;
m7Param.IsNullable = false;
m7Param.Direction = ParameterDirection.Input;
SqlParameter m8Param = new SqlParameter("#m8", stat.month8);
m8Param.SqlDbType = System.Data.SqlDbType.Int;
m8Param.IsNullable = false;
m8Param.Direction = ParameterDirection.Input;
SqlParameter m9Param = new SqlParameter("#m9", stat.month9);
m9Param.SqlDbType = System.Data.SqlDbType.Int;
m9Param.IsNullable = false;
m9Param.Direction = ParameterDirection.Input;
SqlParameter m10Param = new SqlParameter("#m10", stat.month10);
m10Param.SqlDbType = System.Data.SqlDbType.Int;
m10Param.IsNullable = false;
m10Param.Direction = ParameterDirection.Input;
SqlParameter m11Param = new SqlParameter("#m11", stat.month11);
m11Param.SqlDbType = System.Data.SqlDbType.Int;
m11Param.IsNullable = false;
m11Param.Direction = ParameterDirection.Input;
SqlParameter m12Param = new SqlParameter("#m12", stat.month12);
m12Param.SqlDbType = System.Data.SqlDbType.Int;
m12Param.IsNullable = false;
m12Param.Direction = ParameterDirection.Input;
SqlParameter[] paramList = {idParam, m1Param, m2Param, m3Param, m4Param,
m5Param, m6Param, m7Param, m8Param, m8Param,
m10Param, m11Param, m12Param };
return paramList;
}
public static void insertStatistic(Statistics stat, string prodID)
{
String comStr = "insert into LTDStatistic(prodID, month1, month2, month3, month4, month5, " +
"month6, month7, month8, month9, month10, month11, month12)" +
"values(#id, #m1, #m2, #m3, #m4, #m5, #m6, #m7, #m8, #m9, #m10, #m11, #m12)";
createConncetion();
SqlCommand command = new SqlCommand(comStr, con);
command.Parameters.AddRange(createParams(stat, prodID));
openConnection();
command.ExecuteNonQuery();
closeConnection();
command.Parameters.Clear();
command.Dispose();
}
}
WCF Service uses methods from those classes in order to modify a data base
public void create(ProductsData product)
{
Statistics statDLT = new Statistics(product.demandLTStat.month1, product.demandLTStat.month2,
product.demandLTStat.month3, product.demandLTStat.month4, product.demandLTStat.month5,
product.demandLTStat.month6, product.demandLTStat.month7, product.demandLTStat.month8,
product.demandLTStat.month9, product.demandLTStat.month10, product.demandLTStat.month11,
product.demandLTStat.month12);
Products newProd = new Products(product.prodID, product.prodName,
product.leadTime, product.k, product.c, product.precentOfC,
product.demand, statDLT);
newProd.callculate();
newProd.determineR();
ProductsDataAccess.insertProduct(newProd);
//String plsWork = newProd.prodID;
StatisticsDataAccess.insertStatistic(newProd.demandLTStat, newProd.prodID);
}
Maybe Im missing some important aspect of how things work ... I dont know. Hope someone can help me, and sorry for the long code segments.
m8Param occurs twice in the array returned by StatisticsDataAccess.createParams().
SqlParameter[] paramList = {
idParam, m1Param, m2Param, m3Param, m4Param,
m5Param, m6Param, m7Param, **m8Param**, **m8Param**,
m10Param, m11Param, m12Param };

Categories