473,406 Members | 2,707 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,406 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();
}

}
Nov 16 '05 #1
3 1217
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();
}

}

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

}

Nov 16 '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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.