For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > February 2005 > SQL Parameter question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author SQL Parameter question
hecsan07

2005-02-28, 4:00 pm

I am writing some code that passes two parameters to a store procedure. One
of the parameters is an output parameter. I am querying the DB using the
input value and returning a field from the result using the output parameter.
The issue is that when I run the ExecuteNonquery command I get the following
error:

'@shortLinkUrl' of type: String, the property Size has an invalid size: 0


Any ideas? Please help. I am a beginner programmer using C# and ASP.NET, so
please be as detailed as possible in your help. Thanks.

Hector
Wilco Bauwer

2005-02-28, 4:00 pm

Did you add it as an output parameter to the parameters collection of
the command you are executing? You can do so by setting the parameter's
Direction to ParameterDirection.Output.

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

hecsan07

2005-02-28, 4:00 pm

First of all, thank you for answering my query. To answer your question, yes
I did add it to the command object as an output parameter. I also coded the
T-SQL of the store procedure as an output parameter. I genuinely believed I
did everything I had to do to ensure everything works as it's supposed to. I
even had similar code, that works the same way, that works fine. I compared
the code to make sure I did not make any mistakes along the way and
everything seems fine. Here is some sample code that I am using:


SqlParameter[] Parameters;
Parameters = new SqlParameter[2];
Parameters[0] = new SqlParameter("@shortLinkId", SqlDbType.VarChar);
Parameters[0].Value = shortLinkId;
Parameters[1] = new SqlParameter("@shortLinkUrl", SqlDbType.VarChar);
Parameters[1].Direction = ParameterDirection.Output;
SqlHelper.ExecuteCommand(connectionString, "spGetShortLinkUrl", Parameters);
shortLinkUrl = Parameters[1].Value.ToString();


The following is some code from the ExecuteCommand method:

SqlCommand Command = new SqlCommand();
Command.Connection = new SqlConnection(connection);
Command.CommandText = commandText;
Command.CommandType = commandType;
foreach (SqlParameter parameter in commandParameters)
Command.Parameters.Add(parameter);

Command.Connection.Open();
int recordsAffected;
try
{
recordsAffected = Command.ExecuteNonQuery();
}
catch (Exception ex)
..
..
..
Thanks,
Hector

"Wilco Bauwer" wrote:

> Did you add it as an output parameter to the parameters collection of
> the command you are executing? You can do so by setting the parameter's
> Direction to ParameterDirection.Output.
>
> ----
> - Wilco Bauwer
> Blog & Custom Controls @ http://wilcoding.xs4all.nl
>
>

Wilco Bauwer

2005-02-28, 4:00 pm

Ok. Did you try to specify the length parameter (check the overloads
for the SqlParameter constructor)? If not, try to set it to the length
you specified in your stored procedure.

----
- Wilco Bauwer
Blog & Custom Controls @ http://wilcoding.xs4all.nl

hecsan07

2005-02-28, 8:59 pm

It's very weird. I have code that works the same way as the one I am having
problems with, but your suggestion did the trick. The code works fine now.
Thank you.

Hector

"Wilco Bauwer" wrote:

> Ok. Did you try to specify the length parameter (check the overloads
> for the SqlParameter constructor)? If not, try to set it to the length
> you specified in your stored procedure.
>
> ----
> - Wilco Bauwer
> Blog & Custom Controls @ http://wilcoding.xs4all.nl
>
>

Tampa.NET Koder

2005-02-28, 8:59 pm

I came across the same issue as well. Its strange how the exact code works
in one situtation and then don't work in the other

"hecsan07" <hecsan07@hotmail.com> wrote in message
news:A9E66FA9-BFC9-4FB7-AB84-B1222159ED60@microsoft.com...[color=darkred]
> It's very weird. I have code that works the same way as the one I am
> having
> problems with, but your suggestion did the trick. The code works fine now.
> Thank you.
>
> Hector
>
> "Wilco Bauwer" wrote:
>


Patrick Olurotimi Ige

2005-02-28, 9:00 pm

Thats true ..i had similar occasions too.
Patrick

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com