Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
|
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2009, 02:00 PM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Exclamation Use Stored Procedure as UpdateCommand

Hi All,
I have created a stored procedure to check concurrency before allowing updates or deletions on a table in my sql dbase.
The problem is that i am not able to pass the parameters from the Grid View to the stored procedure. Please see blelow for my stored procedure and c# code where i am attempting to pass the values form the row being edited to the stored procedure. It's not working though.

Please reply as to the solution for this problem.

Stored Procedure
Code:

ALTERPROCEDURE [dbo].[updateSupplier]
@SupplierId smallint,
@SupplierName nvarchar(100),
@Address nvarchar(100),
@ContactPerson nvarchar(50),
@Phone char(10),
@Fax char(10),
@Email nvarchar(50),
@Concurrency smalldatetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SETNOCOUNTON;
Update Supplier
Set SupplierName=@SupplierName, Address=@Address, ContactPerson=@ContactPerson,
Phone=@Phone, Fax=@Fax, Email=@Email, Concurrency=GetDate()
Where SupplierID=@SupplierID AND Concurrency=@Concurrency
IF@@ROWCOUNT= 0
BEGIN
IFEXISTS(SELECT SupplierID FROM Supplier 
WHERE SupplierID = @SupplierID )
RETURN 2 -- Concurrency conflict
ELSE
RETURN 1 -- The record has been deleted
END
ELSE
RETURN 0 -- The record could be updated
 
END
C# Code
Code:
protectedvoid Supplier_Updating(object sender, EventArgs e)
{
SqlCommand cmd = newSqlCommand("updateSupplier", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(newSqlParameter("@SupplierID", SqlDbType.SmallInt,"SupplierID"));
 
cmd.Parameters.Add(newSqlParameter("@SupplierName", SqlDbType.NVarChar, 100,"SupplierName"));
 
cmd.Parameters.Add(newSqlParameter("@SupplierAddress", SqlDbType.NVarChar,100, "Address"));
 
cmd.Parameters.Add(newSqlParameter("@ContactPerson", SqlDbType.NVarChar,100, "ContactPerson"));
 
cmd.Parameters.Add(newSqlParameter("@Phone", SqlDbType.Char,10,"Phone"));
 
cmd.Parameters.Add(newSqlParameter("@Fax", SqlDbType.Char,10,"Fax"));
 
cmd.Parameters.Add(newSqlParameter("@Email", SqlDbType.NVarChar,50,"Email"));
 
cmd.Parameters.Add(newSqlParameter("@Concurrency", SqlDbType.SmallDateTime,"Concurrency"));
 
 
try
{
 
sqlcon.Open();
cmd.ExecuteNonQuery();
 
}
catch
{
thrownewApplicationException("Data Error");
}
finally
{
sqlcon.Close();
}
 
}
 
Old March 31st, 2009, 02:17 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello. do you receive any kind of error?? why you say that you can't pass the parameters???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 31st, 2009, 03:07 PM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Exclamation

Hi,
The error is:
Procedure or Function 'updateSupplier' expects parameter '@SupplierId', which was not supplied.

Please reply as to how i could correct this.
 
Old March 31st, 2009, 11:03 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

I can see two things but I don't know where is the error.

First the parameter @SupplierId is defined in the code as @SupplierID.

second the parameter @SupplierAddress has a different name in the SP.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 1st, 2009, 10:24 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank You. I did the corrections; but i'm still getting the error re: the SupplierID not being supplied to the stored procedure. I've added another line of code:

cmd.Parameters["@SupplierID"].SourceVersion = DataRowVersion.Original;

But the error still remains.
 
Old April 1st, 2009, 11:10 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Mmmm.. I will take a shoot, but the parameter name is "@SupplierId" I don't know if that can cause and error. But if you have any doubt rename the parameter to another silly name just to try it...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old April 1st, 2009, 11:51 AM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I've been trying to get the parameters another way; please see below for snippet, now i'm getting the error Object reference not set to an instance of the object. I've highlighted the line with the error. Please check it and reply with suggestion(s).
Code:
 
private string supid;
 
protectedvoid Supplier_Updating(object sender, GridViewUpdateEventArgs e)
{
SqlCommand cmd = newSqlCommand("updateSupplier", sqlcon);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(newSqlParameter("@SupplierID", SqlDbType.SmallInt, 5, "SupplierID"));
TextBox id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("SupplierID"));
supid = id.Text;
cmd.Parameters["@SupplierID"].Value = supid;
 
Old April 1st, 2009, 12:15 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Try renaming the parameter to see what is happening!!

Also, which object is empty?? (debug and check it).
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
nickoy (April 1st, 2009)
 
Old April 1st, 2009, 02:05 PM
Registered User
 
Join Date: Mar 2009
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default Solution

It's Solved. Thank you for your help.The problem was with the parameters; in the source file i had listed several parameters within the <updateParameters> tag. These were actually creating a fresh set of parameters which were not connected to the Grid View. I would have had to map them to the grid View in the code from behind. So that's why i was getting the error '@SupplierID not supplied'.

So what i've done is delete the entire <UpdateParameters> tag in the source code and specify that my update command is a stored procedure also i specified that
onUpdating="SuppliersUpdating" on the SqlDataSource.

In the code from behind i used the following snippet:
Code:
protected void Suppliers_Updating(object sender, EventArgs e)
{
GridView1.DataBind();
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Stored procedure ashuphp MySQL 1 April 2nd, 2007 04:20 PM
Stored Procedure jezywrap SQL Server ASP 1 January 3rd, 2007 12:29 AM
Help On Stored Procedure desireemm SQL Language 2 October 31st, 2005 07:11 PM
Stored Procedure rajanikrishna SQL Server 2000 0 July 18th, 2005 05:01 AM
stored procedure lokey VB How-To 7 June 30th, 2005 12:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.