calling a stored procedure in ADO.NET 2.0

D

David Sagenaut

How can I calling a stored procedure (say "sp_example") in ADO.NET 2.0? I
used to do this in ADO.NET 1.1, but in 2.0 (beta2 vs2005), after the
following
...
sqlConnection con = new sqlConnection(strCon)
sqlCommand cmd = sqlCommand("sp_example", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.???

under the Parameters property, I only find the AddRange() and AddWithValue()
method, but not the Add() method which is in ADO.NET 1.1. Does anyone have
an idea how to call a stored procedure in the new 2.0 framwork? Thanks in
advance.

David
 
C

Cor Ligthert [MVP]

David,

The Addwithvalue replaces in my opinion in most cases there where you where
using the Add. (In VBNet there is given a warning to change that in that
way)

I hope this helps,

Cor
 
M

Miha Markic [MVP C#]

Hi David,

Not sure about beta 2 but there is an overloaded Add method for
SqlParameterCollection in RC.
Try typing Add anyway and see if compiler yields errors.
 
M

Miha Markic [MVP C#]

Hi Sahil,

I suspected something like this. Do you know why it is marked with that
attribute?
 
S

Sahil Malik [MVP]

... Thats a question for the softies :)

I'm guessing that will go away in RTM .. .. but u nevah know !! :p

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------

Miha Markic said:
Hi Sahil,

I suspected something like this. Do you know why it is marked with that
attribute?

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Sahil Malik said:
David,

You're on the right track.

Next you need to add parameters, which AddWithValue is an easy way to do.

Add still works, it just doesn't appear in the intellisense -
http://msdn2.microsoft.com/en-us/library/hyaybfa1(en-US,VS.80).aspx (that
is because it is set to EditorBrowsable.Never)

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
 
D

David Sceppa [MSFT]

David,

This was a known problem with some Beta 2 builds of Visual Studio .NET
2005. The problem has been addressed in the released version. In the
meantime, you can still use Parameters.Add, even though that method does
not appear in the Intellisense drop-downs.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2005 Microsoft Corporation. All rights reserved.
 
K

Khaled Hussein

Hello David Sceppa [MSFT],
Thanks for this information... I was curious why isn't there an Add method
in Parameters collection, and you helped me with this. but I was having a
question of curioucity.
Does your post means that Intellisense drop-downs are filled dynamically?
are they generated at runtime?
I mean if the method already exist and I can use it which means it is a public
method, why wouldn't it be viewable in the Intellisense drop-downs?

Thanks
--
Khaled Hussein
Graduate Teaching Assistant
College of Computing and Information Technology
Arab Academy for Science and Technology and Maritime Transport
Web Site: http://www.aast.edu
(The power of imagination makes us infinite.)
 
S

Sahil Malik [MVP]

Khaled,

It wasn't viewable because of a bug, this should be fixed in RTM.

- Sahil Malik [MVP]
ADO.NET 2.0 book -
http://codebetter.com/blogs/sahil.malik/archive/2005/05/13/63199.aspx
----------------------------------------------------------------------------



Khaled Hussein said:
Hello David Sceppa [MSFT],
Thanks for this information... I was curious why isn't there an Add method
in Parameters collection, and you helped me with this. but I was having a
question of curioucity.
Does your post means that Intellisense drop-downs are filled dynamically?
are they generated at runtime?
I mean if the method already exist and I can use it which means it is a
public method, why wouldn't it be viewable in the Intellisense drop-downs?

Thanks
--
Khaled Hussein
Graduate Teaching Assistant
College of Computing and Information Technology
Arab Academy for Science and Technology and Maritime Transport
Web Site: http://www.aast.edu
(The power of imagination makes us infinite.)
David,

This was a known problem with some Beta 2 builds of Visual Studio
.NET 2005. The problem has been addressed in the released version.
In the meantime, you can still use Parameters.Add, even though that
method does not appear in the Intellisense drop-downs.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2005 Microsoft Corporation. All rights reserved.
 
D

David Sceppa [MSFT]

Khaled,

If I remember correctly, the Intellisense drop-downs are generated
dynamically using reflection. There was one overload for the Add method
that was marked with an attribute so it would not show up in Intellisense.
The Intellisense logic saw this attribute and hid all Add methods rather
than this particular overload.

So, even though the Add method does not show up in the Intellisense
drop-downs, you can still type it manually. Your code will compile and run.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2005 Microsoft Corporation. All rights reserved.
 
C

Cor Ligthert [MVP]

David,

What is wrong to use addwithvalue in these cases?

In VBNet it is warned to use in the cases where this statement is for to use
that instead of the ADD.

I read often so much for future use (especially in this newsgroup), that I
do not agree, however here VBNet warns.

Cor
 
D

David Sceppa [MSFT]

There's nothing wrong with using AddWithValue. I was merely trying to
explain what happened to Add and how people could still use it in the beta.

I hope this information proves helpful.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2005 Microsoft Corporation. All rights reserved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top