passing parameter values to SQL Server from CSharp

  • Thread starter Thread starter Charles Crawford
  • Start date Start date
C

Charles Crawford

I'm having problems getting correct parameter values to pass to SQL Server
from C# code.

In my C# code, I assign a parameter '@appID' and give it a value based on
the option chosen by a user.

No matter what I select when debugging, the code always passes a value of
'1' to the stored procedure.

The stored procedure is as follows:

CREATE PROCEDURE [dbo].[spLockApplication]
@appID int
AS
UPDATE tblApplications SET isLocked = 1 where appID = @appID
GO

The C# code that uses this sp is as follows:

string connString =
System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString();

SqlConnection myConn = new SqlConnection(connString);

SqlParameter myAppID = new SqlParameter("@appID", SqlDbType.Bit);

myAppID.Value = applicationID;

SqlCommand myCmd = new SqlCommand("spLockApplication", myConn);

myCmd.CommandType = CommandType.StoredProcedure;

myCmd.Parameters.Add(myAppID);

myConn.Open();

myCmd.ExecuteNonQuery();

myConn.Close();

Debugging the C# code shows that prior to execution, applicationID has the
correct value. If the value is 2, for instance, the data row with appID =
@appID should be the target row for the update operation. The only affected
record, however, is the row with appID = 1.

Any ideas?

Thanks,

Charlie
 
Probably because the type SqlDbType.Bit transform it to the value 1 for any
value different of 0.

Finally, this newsgroup is about ADP and not about ADO.NET.
 
You're exactly right... I was getting ready to post that I had found the
error...

thanks for the quick insight. I guess I was staring at my code too long.

Charlie

Sylvain Lafontaine said:
Probably because the type SqlDbType.Bit transform it to the value 1 for
any value different of 0.

Finally, this newsgroup is about ADP and not about ADO.NET.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF


Charles Crawford said:
I'm having problems getting correct parameter values to pass to SQL
Server from C# code.

In my C# code, I assign a parameter '@appID' and give it a value based on
the option chosen by a user.

No matter what I select when debugging, the code always passes a value of
'1' to the stored procedure.

The stored procedure is as follows:

CREATE PROCEDURE [dbo].[spLockApplication]
@appID int
AS
UPDATE tblApplications SET isLocked = 1 where appID = @appID
GO

The C# code that uses this sp is as follows:

string connString =
System.Configuration.ConfigurationManager.AppSettings["connectionString"].ToString();

SqlConnection myConn = new SqlConnection(connString);

SqlParameter myAppID = new SqlParameter("@appID", SqlDbType.Bit);

myAppID.Value = applicationID;

SqlCommand myCmd = new SqlCommand("spLockApplication", myConn);

myCmd.CommandType = CommandType.StoredProcedure;

myCmd.Parameters.Add(myAppID);

myConn.Open();

myCmd.ExecuteNonQuery();

myConn.Close();

Debugging the C# code shows that prior to execution, applicationID has
the correct value. If the value is 2, for instance, the data row with
appID = @appID should be the target row for the update operation. The
only affected record, however, is the row with appID = 1.

Any ideas?

Thanks,

Charlie
 
Back
Top