473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem with SQL Parameters

Hi everyone,

I'm having a very frustrating problem executing a stored procedure. I'll put
the code at the bottom.

When I build the SP and add all the parameters everything goes as expected.
However when I run it, the exception tells me that the parameter doesnt
exist for that SP.

Obviously it's founmd the stored procedure, but I am absolutely certain that
it does contain that parameter. It actually does the same with with all four
parmeters that are passed though.

It just swears blind that the parameter isnt in the SP. Its driving me nuts

I hope somone can help

Simon

The code is as follows:

public static bool insertSiteTestRange(int siteID, int testID, string
minValue, string maxValue){
SqlCommand cmd;

cmd = new SqlCommand("insertTestRange");

SqlParameter siteIDParam = new SqlParameter("centreID",
Convert.ToInt16(siteID));
cmd.Parameters.Add(siteIDParam);

SqlParameter trialIDParam = new SqlParameter("testID", testID);
cmd.Parameters.Add(trialIDParam);

SqlParameter maxValParam = new SqlParameter("upperBound", minValue);
cmd.Parameters.Add(maxValParam);

SqlParameter minValParam = new SqlParameter("lowerBound", maxValue);
cmd.Parameters.Add(minValParam);

if(!DataAccessProvider.executeNonQueryTransaction( cmd)){
return false;
}

// If we get here then we were successful
return true;
}

public static bool executeNonQueryTransaction(SqlCommand cmd){
int rowsAffected = 0;
SqlConnection con = new SqlConnection(connectionString);
SqlTransaction trans;

// We can't put this in a try block because if con.open fails, trans wont
be assigned to and we'll
// get an unassigned variable. Wont compile
con.Open();
trans = con.BeginTransaction();

try{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;

rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected == 0){
// updateSiteDetails: If no rows are affected, that likely means that
someone has deleted the centre
// whilst the user was viewing the updateSite page
if(cmd.CommandText.Equals("updateSiteDetails")){
throw new Exception("Exception thrown in
DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd) whilst update
centre details. The centre specified was not found. " +
"If no rows are affected, it could mean that someone has deleted the
centre whilst the user was viewing the updateSite page");
}
else{
return false;
}
}

else{
trans.Commit();
return true;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd)",
e));
return false;
}

finally{
con.Close();
}

}
Jul 21 '05 #1
3 1765
Stored procedure parameters all have the "@" in the front. This symbol most
also be included when referencing the parameter by name in your code.

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Hi everyone,

I'm having a very frustrating problem executing a stored procedure. I'll put the code at the bottom.

When I build the SP and add all the parameters everything goes as expected. However when I run it, the exception tells me that the parameter doesnt
exist for that SP.

Obviously it's founmd the stored procedure, but I am absolutely certain that it does contain that parameter. It actually does the same with with all four parmeters that are passed though.

It just swears blind that the parameter isnt in the SP. Its driving me nuts
I hope somone can help

Simon

The code is as follows:

public static bool insertSiteTestRange(int siteID, int testID, string
minValue, string maxValue){
SqlCommand cmd;

cmd = new SqlCommand("insertTestRange");

SqlParameter siteIDParam = new SqlParameter("centreID",
Convert.ToInt16(siteID));
cmd.Parameters.Add(siteIDParam);

SqlParameter trialIDParam = new SqlParameter("testID", testID);
cmd.Parameters.Add(trialIDParam);

SqlParameter maxValParam = new SqlParameter("upperBound", minValue);
cmd.Parameters.Add(maxValParam);

SqlParameter minValParam = new SqlParameter("lowerBound", maxValue);
cmd.Parameters.Add(minValParam);

if(!DataAccessProvider.executeNonQueryTransaction( cmd)){
return false;
}

// If we get here then we were successful
return true;
}

public static bool executeNonQueryTransaction(SqlCommand cmd){
int rowsAffected = 0;
SqlConnection con = new SqlConnection(connectionString);
SqlTransaction trans;

// We can't put this in a try block because if con.open fails, trans wont be assigned to and we'll
// get an unassigned variable. Wont compile
con.Open();
trans = con.BeginTransaction();

try{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;

rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected == 0){
// updateSiteDetails: If no rows are affected, that likely means that
someone has deleted the centre
// whilst the user was viewing the updateSite page
if(cmd.CommandText.Equals("updateSiteDetails")){
throw new Exception("Exception thrown in
DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd) whilst update centre details. The centre specified was not found. " +
"If no rows are affected, it could mean that someone has deleted the
centre whilst the user was viewing the updateSite page");
}
else{
return false;
}
}

else{
trans.Commit();
return true;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd)",
e));
return false;
}

finally{
con.Close();
}

}

Jul 21 '05 #2
I am such a dick

Thank you. Yoiu are a Gem amongst sh*t as we say where I come from

Take care

Simon
Jul 21 '05 #3
Hi Simon,

I see that you're already solved the problem thanks to Peter.
As an another approach to solve the problem you might:
use server explorer, drag & drop the sp on the form and check the code
windows forms designer has generated.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Simon Harvey" <si**********@the-web-works.co.uk> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Hi everyone,

I'm having a very frustrating problem executing a stored procedure. I'll put the code at the bottom.

When I build the SP and add all the parameters everything goes as expected. However when I run it, the exception tells me that the parameter doesnt
exist for that SP.

Obviously it's founmd the stored procedure, but I am absolutely certain that it does contain that parameter. It actually does the same with with all four parmeters that are passed though.

It just swears blind that the parameter isnt in the SP. Its driving me nuts
I hope somone can help

Simon

The code is as follows:

public static bool insertSiteTestRange(int siteID, int testID, string
minValue, string maxValue){
SqlCommand cmd;

cmd = new SqlCommand("insertTestRange");

SqlParameter siteIDParam = new SqlParameter("centreID",
Convert.ToInt16(siteID));
cmd.Parameters.Add(siteIDParam);

SqlParameter trialIDParam = new SqlParameter("testID", testID);
cmd.Parameters.Add(trialIDParam);

SqlParameter maxValParam = new SqlParameter("upperBound", minValue);
cmd.Parameters.Add(maxValParam);

SqlParameter minValParam = new SqlParameter("lowerBound", maxValue);
cmd.Parameters.Add(minValParam);

if(!DataAccessProvider.executeNonQueryTransaction( cmd)){
return false;
}

// If we get here then we were successful
return true;
}

public static bool executeNonQueryTransaction(SqlCommand cmd){
int rowsAffected = 0;
SqlConnection con = new SqlConnection(connectionString);
SqlTransaction trans;

// We can't put this in a try block because if con.open fails, trans wont be assigned to and we'll
// get an unassigned variable. Wont compile
con.Open();
trans = con.BeginTransaction();

try{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Transaction = trans;

rowsAffected = cmd.ExecuteNonQuery();

if(rowsAffected == 0){
// updateSiteDetails: If no rows are affected, that likely means that
someone has deleted the centre
// whilst the user was viewing the updateSite page
if(cmd.CommandText.Equals("updateSiteDetails")){
throw new Exception("Exception thrown in
DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd) whilst update centre details. The centre specified was not found. " +
"If no rows are affected, it could mean that someone has deleted the
centre whilst the user was viewing the updateSite page");
}
else{
return false;
}
}

else{
trans.Commit();
return true;
}
}
catch(Exception e){
trans.Rollback();
ExceptionManager.Publish(new Exception("Exception detected whilst
executing DataAccessProvider.executeNonQueryTransaction(SqlC ommand cmd)",
e));
return false;
}

finally{
con.Close();
}

}

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Christopher Brandsdal | last post by:
Hi! I have a problem running my code on 2000 server and iis5.0. The code runs perfectly on my localhost (xp iis5.1) but when i run it on 2000 server iis5.0 I get this error: ...
3
by: Bilbo | last post by:
I have a a headscratcher here: I have a form that when submitted should do 2 things when a user enters data and then clicks the Add button. Here goes: 1. Call a stored procedure called...
1
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say...
1
by: leslie_tighe | last post by:
Hello, I have webservice created with Axis 1.2.1 and that I am trying to consuming in .NET (VB) using the Microsoft provided tools. While I am able to consume methods on the service that return...
4
by: leslie_tighe | last post by:
Hello, I have a webservice running on a J2EE server created with Axis 1.2.. I have a client that I am building in .net that needs to consume this webserivce and am having a bit of trouble. I have...
0
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't...
0
by: CJM | last post by:
Repeated for the benefit of m.p.i.asp.general, which I forgot to include in the original posting... "CJM" <cjmnews04@REMOVEMEyahoo.co.ukwrote in message news:4lo3f8F2shqtU1@individual.net...
12
by: Light | last post by:
Hi all, I posted this question in the sqlserver.newusers group but I am not getting any response there so I am going to try it on the fine folks here:). I inherited some legacy ASP codes in my...
14
by: rashmidutt | last post by:
hello sir i am making project on vb.net language..and project is on hospital management..its major project..and too many fields are present in its data base..i was connecting data base in forms but...
3
by: kpeeroo | last post by:
Private Function AddCompanyOvertime() As Integer Dim companyID As Integer = GetCompanyID() Console.WriteLine(companyID) Dim paramCompanyID As New SqlParameter("@CompanyID",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.