Insert using C#

G

Guest

When I try to insert the second record I get the message "The Variable name
"@Parm1" has already been declared. What can I do to prevent this. The code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
M

Miha Markic [MVP C#]

Hi,

I guess you are trying to add all those parameters again and that's wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..
 
G

Guest

It's calling Add twice using the property right off the method. Try creating
a Parameter object and then assigning the value:
SqlParameter param = sqlInsertCommand1.Parameters.Add("@Parm1",
SqlDbType.Int);
param.Value = txtServiceCode.Text;
param = sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar);
param.Value = txtServiceDesc.Text;
.... etc.

Cheers,
Steve Goodyear
 
G

Guest

Steve, I hope that I understand, here is what I did:
sqlInsertCommand1.CommandText = "INSERT INTO [service-task]
([service-order-task], [service-date], [animal-id] ,[owner-id],
[animal-size-ind], [service-nmr], [service-code], [service-description]
,cost) VALUES (@Param1, @Param2, @Param3, @Param4, @Param5, @Param6, @Param7,
@Param8 ,@cost)";

SqlParameter Param = sqlInsertCommand1.Parameters.Add("@Param1",
SqlDbType.Int);
Param.Value = txtServiceOrderTask.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param2", SqlDbType.DateTime);
Param.Value = txtServiceDate.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param3", SqlDbType.Int);
Param.Value = txtAnimalId.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param4", SqlDbType.Int);
Param.Value = txtOwnerId.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param5", SqlDbType.VarChar);
Param.Value = cbAnimalSize.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param6", SqlDbType.Int);
Param.Value = txtServiceNumber.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param7", SqlDbType.Int);
Param.Value = txtServiceCode.Text;
Param = sqlInsertCommand1.Parameters.Add("@Param8", SqlDbType.VarChar);
Param.Value = txtServiceDesc.Text;
Param = sqlInsertCommand1.Parameters.Add("@cost", SqlDbType.Money);
Param.Value = Convert.ToDecimal(txtServiceCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

I got the error message "the variable @Param1 has already been declaired.
 
G

Guest

Miha, I made the change that you suggested and I could get it to compile.
The message was "identifier required". I tried several ways with '(' , '(['
and it did not work.

Miha Markic said:
Hi,

I guess you are trying to add all those parameters again and that's wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
When I try to insert the second record I get the message "The Variable
name
"@Parm1" has already been declared. What can I do to prevent this. The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.["@Parm1"],SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.["@Parm2"],SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.["@Parm3"],SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
A

Andrew Robinson

sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

Miha Markic said:
Hi,

I guess you are trying to add all those parameters again and that's wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
When I try to insert the second record I get the message "The Variable
name
"@Parm1" has already been declared. What can I do to prevent this. The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
M

Miha Markic [MVP C#]

Yes, I was just testing if anybody will spot the error ;-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Andrew Robinson said:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

Miha Markic said:
Hi,

I guess you are trying to add all those parameters again and that's
wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
When I try to insert the second record I get the message "The Variable
name
"@Parm1" has already been declared. What can I do to prevent this. The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
G

Guest

This compiles but does not run?? The message that I receive:
"System.IndexOutOfRangeException:An SqlParameter with ParameterName "@Param1"
is not contained by this SqlParameterCollection". I also cannot convert
'@param1 to SqlDbType.Int'.??

sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description] ,[large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values(@Param1,@Param2,@Param3, @Param4,@Param5)";

sqlInsertCommand1.Parameters["@Param1"].Value = txtServiceCode.Text;
sqlInsertCommand1.Parameters["@Param2"].Value = txtServiceDesc.Text;
sqlInsertCommand1.Parameters["@Param3"].Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param4"].Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param5"].Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);


Andrew Robinson said:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

Miha Markic said:
Hi,

I guess you are trying to add all those parameters again and that's wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
When I try to insert the second record I get the message "The Variable
name
"@Parm1" has already been declared. What can I do to prevent this. The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
G

Guest

This compiles but does not run?? The message that I receive:
"System.IndexOutOfRangeException:An SqlParameter with ParameterName "@Param1"
is not contained by this SqlParameterCollection". I also cannot convert
'@param1 to SqlDbType.Int'.??

sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description] ,[large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values(@Param1,@Param2,@Param3, @Param4,@Param5)";

sqlInsertCommand1.Parameters["@Param1"].Value = txtServiceCode.Text;
sqlInsertCommand1.Parameters["@Param2"].Value = txtServiceDesc.Text;
sqlInsertCommand1.Parameters["@Param3"].Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param4"].Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param5"].Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);


Miha Markic said:
Yes, I was just testing if anybody will spot the error ;-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Andrew Robinson said:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

Miha Markic said:
Hi,

I guess you are trying to add all those parameters again and that's
wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

When I try to insert the second record I get the message "The Variable
name
"@Parm1" has already been declared. What can I do to prevent this. The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])" +
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");
 
G

Guest

Hi nobohana,

Try narrowing your code to ensure the problem is not coming from another
area. Here's a simplier, yet more complete example for parameter usage which
may help:

private void button1_Click(object sender, System.EventArgs e){
SqlConnection db = new SqlConnection("Data Source=(local);Initial
Catalog=Northwind;Integrated Security=SSPI;");
SqlCommand insertCommand = new SqlCommand();
insertCommand.CommandText = "INSERT INTO [Region] ([RegionID],
[RegionDescription]) VALUES (@Param1, @Param2)";
insertCommand.Connection = db;
SqlParameter param = insertCommand.Parameters.Add("@Param1", SqlDbType.Int);
param.Value = int.Parse(textBox1.Text);
param = insertCommand.Parameters.Add("@Param2", SqlDbType.NVarChar, 50);
param.Value = textBox2.Text;
db.Open();
insertCommand.ExecuteNonQuery();
db.Close();
}

Cheers,
Steve Goodyear
 
M

Miha Markic [MVP C#]

Look,

You have to create a SqlCommand instance and then *add* all those
parameters.
When/If you want to reuse the same instance, you shouldn't add parameters
again but you have to use them as we told you.
Now, you don't have any code that adds parameters...
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
This compiles but does not run?? The message that I receive:
"System.IndexOutOfRangeException:An SqlParameter with ParameterName
"@Param1"
is not contained by this SqlParameterCollection". I also cannot convert
'@param1 to SqlDbType.Int'.??

sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description] ,[large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values(@Param1,@Param2,@Param3, @Param4,@Param5)";

sqlInsertCommand1.Parameters["@Param1"].Value = txtServiceCode.Text;
sqlInsertCommand1.Parameters["@Param2"].Value = txtServiceDesc.Text;
sqlInsertCommand1.Parameters["@Param3"].Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param4"].Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param5"].Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);


Miha Markic said:
Yes, I was just testing if anybody will spot the error ;-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

Andrew Robinson said:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
Hi,

I guess you are trying to add all those parameters again and that's
wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

When I try to insert the second record I get the message "The
Variable
name
"@Parm1" has already been declared. What can I do to prevent this.
The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO
services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])"
+
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text
+
" Successful.","");
 
G

Guest

Miha, I must be dumb! I coded the insert as per your samples. I don't
understand what you mean by adding the parameters again? This is what I have:

sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description], [large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values(@Param1,@Param2,@Param3, @Param4,@Param5)";

SqlParameter param =
sqlInsertCommand1.Parameters.Add("@Param1",SqlDbType.Int);
param.Value = txtServiceCode.Text;

param = sqlInsertCommand1.Parameters.Add("@Param2",SqlDbType.VarChar);
param.Value = txtServiceDesc.Text;

param = sqlInsertCommand1.Parameters.Add("@Param3",SqlDbType.Money);
param.Value = Convert.ToDecimal(cbLargeAnimalCost.Text);

param = sqlInsertCommand1.Parameters.Add("@Param4",SqlDbType.Money);
param.Value = Convert.ToDecimal(cbMediumAnimalCost.Text);

param = sqlInsertCommand1.Parameters.Add("@Param5",SqlDbType.Money);
param.Value = Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text +
" Successful.","");


Miha Markic said:
Look,

You have to create a SqlCommand instance and then *add* all those
parameters.
When/If you want to reuse the same instance, you shouldn't add parameters
again but you have to use them as we told you.
Now, you don't have any code that adds parameters...
--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

nbohana said:
This compiles but does not run?? The message that I receive:
"System.IndexOutOfRangeException:An SqlParameter with ParameterName
"@Param1"
is not contained by this SqlParameterCollection". I also cannot convert
'@param1 to SqlDbType.Int'.??

sqlInsertCommand1.CommandText = "INSERT INTO services([service-code],
[service-description] ,[large-animal-cost], [medium-animal-cost],
[small-animal-cost]) values(@Param1,@Param2,@Param3, @Param4,@Param5)";

sqlInsertCommand1.Parameters["@Param1"].Value = txtServiceCode.Text;
sqlInsertCommand1.Parameters["@Param2"].Value = txtServiceDesc.Text;
sqlInsertCommand1.Parameters["@Param3"].Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param4"].Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters["@Param5"].Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);


Miha Markic said:
Yes, I was just testing if anybody will spot the error ;-)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

should read

sqlInsertCommand1.Parameters["@Parm1"].Value = ..

remove the period after Parameters

-Andrew

"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
Hi,

I guess you are trying to add all those parameters again and that's
wrong.
You have to reuse them if you are reusing SqlCommand instance.
So, instead of adding you should do:
sqlInsertCommand1.Parameters.["@Parm1"].Value = ..

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info

When I try to insert the second record I get the message "The
Variable
name
"@Parm1" has already been declared. What can I do to prevent this.
The
code:

try
{
sqlInsertCommand1.CommandText = "INSERT INTO
services([service-code],
[service-description]" +
" ,[large-animal-cost], [medium-animal-cost], [small-animal-cost])"
+
" values(@Parm1,@Parm2,@Parm3, @Parm4,@Parm5)";

sqlInsertCommand1.Parameters.Add("@Parm1",SqlDbType.Int).Value =
txtServiceCode.Text;
sqlInsertCommand1.Parameters.Add("@Parm2",SqlDbType.VarChar).Value =
txtServiceDesc.Text;
sqlInsertCommand1.Parameters.Add("@Parm3",SqlDbType.Money).Value =
Convert.ToDecimal(cbLargeAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm4",SqlDbType.Money).Value =
Convert.ToDecimal(cbMediumAnimalCost.Text);
sqlInsertCommand1.Parameters.Add("@Parm5",SqlDbType.Money).Value =
Convert.ToDecimal(cbSmallAnimalCost.Text);

sqlInsertCommand1.Connection = sqlConnection1;
if (sqlConnection1.State != ConnectionState.Open)
sqlConnection1.Open();
sqlInsertCommand1.ExecuteNonQuery();

this.sqlConnection1.Close();

MessageBox.Show("Insert Complete. [services] " + txtServiceNbr.Text
+
" Successful.","");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top