Question regarding passing parameters to SQL Server

  • Thread starter Thread starter AlBruAn
  • Start date Start date
A

AlBruAn

I need to persist some data to SQL Server, but I can't for the life of me
figure out why I'm getting an error with each attempt.

The code I'm using is as follows:

SqlTransaction trans;

using (SqlConnection conn = new
SqlConnection(myConn)) //_connectionString))
{
conn.Open();
trans = conn.BeginTransaction();

SqlCommand comm = new SqlCommand("InsertContainerData", conn, trans);
#region unused
//comm.Parameters.Add("@ContainerEID", SqlDbType.UniqueIdentifier);
//comm.Parameters.Add("@ContainerName", SqlDbType.NVarChar);
//comm.Parameters.Add("@ContainerID", SqlDbType.NVarChar);
//comm.Parameters.Add("@ContainerStatus", SqlDbType.TinyInt);

//comm.Parameters.Add("@ContainerEID", SqlDbType.UniqueIdentifier,
16).Value = new Guid(this.EID);
//comm.Parameters.Add("@ContainerName", SqlDbType.NVarChar, 40).Value =
this.Name;
//comm.Parameters.Add("@ContainerID", SqlDbType.NVarChar, 40).Value =
this.ID;
//comm.Parameters.Add("@ContainerStatus", SqlDbType.TinyInt, 1).Value =
this.Status.ToString();
#endregion
comm.Parameters.AddWithValue("@ContainerEID", new Guid(this.EID));
comm.Parameters.AddWithValue("@ContainerName", this.Name);
comm.Parameters.AddWithValue("@ContainerID", this.ID);
comm.Parameters.AddWithValue("@ContainerStatus", this.Status.ToString());

comm.ExecuteNonQuery();

and the problem occurs when I execute the last line. The error returned
from SQL Server is as follows:

$exception {"Incorrect syntax near 'InsertContainerData'."} System.Exception
{System.Data.SqlClient.SqlException}

Using SQL Profiler, this is what it sees coming from my application:

exec sp_executesql N'InsertContainerData',N'@ContainerEID
uniqueidentifier,@ContainerName
nvarchar(6),@ContainerID nvarchar(4),@ContainerStatus
nvarchar(1)',@ContainerEID='42B1462A-ED4B-40B9-B627-75B364719731',@ContainerName=N'ct8888',@ContainerID=N'8888',@ContainerStatus=N'3'

What is going wrong with my application? Many thanks in advance.

Allen
 
AlBruAn,

Do you want to abuse us with all this outcommented rows.
Are you not able to show us your problem in a clean way?

Cor
 
Pardon my French, but screw you, you pompous ass. I copied the code from the
app, the code that was commented out was in a collapsed region that expanded
after pasting ... something not obvious to the user when the cursor is
several lines down below where the collapsed region was; if you'll check,
there's no button to preview how it will appear once posted.

As for the problem I had had, I've remedied it by placing the command
parameters outside the new SqlCommand constructor.
 
Yes, that was precisely the problem! I did end up breaking out the
CommandType, CommandText and Connection from what I was passing into the
constructor, and passed them in individually; it took care of the problem.
Thanks! :)
 
Back
Top