Showing posts with label storedprocedure. Show all posts
Showing posts with label storedprocedure. Show all posts

Wednesday, March 28, 2012

passing multiple values from a listbox into a stored procedure

hi i have a listbox with selectedmode = multiple, i am currently using this code in my code behind (c#) to call the storedprocedure within the datasource but its not working: Do i have to write specific code in c# to send the mulitple values through?

protectedvoid confButton_Click(object sender,EventArgs e)

{

try

{

foreach (ListItem itemin authorsListBox4.Items)

{

if (item.Selected)

{

AddConfSqlDataSource.Insert();

}

}

saveStatusLabel.Text ="Save Successfull: The above publication has been saved";

}

catch (Exception ex)

{

saveStatusLabel.Text ="Save Failed: The above publication failed to save" + ex.Message;

}

}

The code you posted looks right, as much as you've posted. You'll have to loop, check for selected, and add. You're on the right track.
One thing that jumps out right away is that I can't see where you're passing any argument into your AddConfSqlDataSource.Insert(); function.
Did you mean something like:

AddConfSqlDataSource.Insert(item);

?


|||

Code looks fine. You are already looping thru each selected item of your multi select list box. So just check the code in this method AddConfSqlDataSource.Insert()

There should be some problem in it that is causing you the issue. Or put the code here for us to take a look.

|||

hmmm if i pass in item to the datasource:AddConfSqlDataSource.Insert(item);

i recieve error: No overload for method 'Insert' takes '1' arguments

the parameters i am passing through the datasource to the SP look like this:

<asp:SqlDataSourceID="AddConfSqlDataSource"runat="server" ConnectionString="<%$ ConnectionStrings:SoSymConnectionString%>" InsertCommand="StoredProcedureTest2" InsertCommandType="StoredProcedure"> <InsertParameters> <asp:ControlParameterControlID="PubTypeDropDownList"Name="typeID" PropertyName="SelectedValue"Type="Int16"/> <asp:ControlParameterControlID="titleTextBox4"Name="title" PropertyName="Text"Type="String"/> <asp:ControlParameterControlID="authorsListBox4"Name="authorID" Type="String"/> </InsertParameters> </asp:SqlDataSource>

and my SP as follows: maybe i have a problem within my SP - looping?

ALTER PROCEDUREdbo.StoredProcedureTest2 @.publicationIDInt=null, @.typeIDsmallint=null, @.titlenvarchar(MAX)=null, @.authorIDsmallint=null ASBEGIN TRANSACTIONSET NOCOUNT ON DECLARE@.ERRORInt SET@.ERROR=0IF EXISTS(SELECT*FROMPublicationWHEREtitle = @.title)SELECT@.publicationID = (SELECTpublicationIDFROMPublicationWHEREtitle = @.title)ELSE BEGIN INSERT INTOPublication (typeID, title) VALUES(@.typeID, @.title) SET@.publicationID = @.@.IDENTITY--Obtain the ID of the created publication SET@.ERROR = @.@.ERROREND IF NOT EXISTS(SELECT*FROMPublicationAuthorsWHEREpublicationID = @.publicationIDANDauthorID = @.authorID)BEGIN INSERT INTOPublicationAuthors (publicationID, authorID)VALUES(@.publicationID, @.authorID) SET@.ERROR = @.@.ERROR END IF(@.ERROR<>0)ROLLBACK TRANSACTIONELSECOMMIT TRANSACTION

Sorry to post loads of code! ...

Thanks

Friday, March 23, 2012

Passing DB Name/Owner Name in a prepareCall statement

When calling a stored procedure in sql server passing just the Stored
Procedure name, my stored procedure name works correctly. Below is
the String I use when performing Connection.prepareCall(String)
{CALL sp_name (?,?,?)}
However when I want to specify the db name and the user name (name of
the user who owns the sp), I get an error that the driver could not
find the stored procedure. Any clues as to how this could be fixed?
In other words, how can I set the default database name and owner name
to query against?
{CALL dbName.username.sp_name (?,?,?)}
Ryan wrote:

> When calling a stored procedure in sql server passing just the Stored
> Procedure name, my stored procedure name works correctly. Below is
> the String I use when performing Connection.prepareCall(String)
> {CALL sp_name (?,?,?)}
> However when I want to specify the db name and the user name (name of
> the user who owns the sp), I get an error that the driver could not
> find the stored procedure.
It's the DBMS, not the driver, that can or cannot find a stored procedure.
Your JDBC SQL seems fine. Show the actual exception you get. Maybe that will
help.
Any clues as to how this could be fixed?
> In other words, how can I set the default database name and owner name
> to query against?
> {CALL dbName.username.sp_name (?,?,?)}
|||Joe Weinstein <joeNOSPAM@.bea.com> wrote in message news:<412CF482.9070904@.bea.com>...[vbcol=seagreen]
> Ryan wrote:
>
> It's the DBMS, not the driver, that can or cannot find a stored procedure.
> Your JDBC SQL seems fine. Show the actual exception you get. Maybe that will
> help.
> Any clues as to how this could be fixed?
I get the following exception:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC][SQLServer]The procedure name
'dbName.username1.dbName.userName2.' contains more than the maximum
number of prefixes. The maximum is 3.
The call I pass to Connection.prepareCall is:
{CALL dbName.username2.sp_name (?,?,?)}
where dbname = the name of the database that I logged into. Username1
is the username that I logged in with. Username2 is the username of
the owner of the stored procedure and sp_name is the name of the
stored procedure.
|||Joe Weinstein <joeNOSPAM@.bea.com> wrote in message news:<412CF482.9070904@.bea.com>...[vbcol=seagreen]
> Ryan wrote:
>
> It's the DBMS, not the driver, that can or cannot find a stored procedure.
> Your JDBC SQL seems fine. Show the actual exception you get. Maybe that will
> help.
> Any clues as to how this could be fixed?
I get the following exception:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
JDBC][SQLServer]The procedure name
'dbName.username1.dbName.userName2.' contains more than the maximum
number of prefixes. The maximum is 3.
The call I pass to Connection.prepareCall is:
{CALL dbName.username2.sp_name (?,?,?)}
where dbname = the name of the database that I logged into. Username1
is the username that I logged in with. Username2 is the username of
the owner of the stored procedure and sp_name is the name of the
stored procedure.
|||Ryan wrote:

> Joe Weinstein <joeNOSPAM@.bea.com> wrote in message news:<412CF482.9070904@.bea.com>...
>
> I get the following exception:
> java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for
> JDBC][SQLServer]The procedure name
> 'dbName.username1.dbName.userName2.' contains more than the maximum
> number of prefixes. The maximum is 3.
> The call I pass to Connection.prepareCall is:
> {CALL dbName.username2.sp_name (?,?,?)}
> where dbname = the name of the database that I logged into. Username1
> is the username that I logged in with. Username2 is the username of
> the owner of the stored procedure and sp_name is the name of the
> stored procedure.
very odd. Does this code work for you?
It does for me... (logging in as a non-sa user...)
Statement s = c.createStatement();
s.execute("use tempdb");
s.execute("exec master.dbo.sp_who");
PreparedStatement p = c.prepareStatement("{ call master.dbo.sp_who() }");
p.execute();
CallableStatement cl = c.prepareCall("{ call master.dbo.sp_who() }");
cl.execute();
|||Thanks Joe,
I found the culprit in an extended class. It was prepending a default
databaseName.ownerName. causing this "weird" behavior. A wasted
thread,
{CALL dbname.ownername.sp_name (?,?,?)}
will work.
Joe Weinstein wrote:[vbcol=seagreen]
> Ryan wrote:
news:<412CF482.9070904@.bea.com>...[vbcol=seagreen]
Stored[vbcol=seagreen]
is[vbcol=seagreen]
of[vbcol=seagreen]
not[vbcol=seagreen]
procedure.[vbcol=seagreen]
that will[vbcol=seagreen]
name[vbcol=seagreen]
Username1[vbcol=seagreen]
of
> very odd. Does this code work for you?
> It does for me... (logging in as a non-sa user...)
> Statement s = c.createStatement();
> s.execute("use tempdb");
> s.execute("exec master.dbo.sp_who");
> PreparedStatement p = c.prepareStatement("{ call
master.dbo.sp_who() }");
> p.execute();
> CallableStatement cl = c.prepareCall("{ call master.dbo.sp_who()
}");
> cl.execute();

Wednesday, March 21, 2012

passing column name as parameter to a stored procedure

Hi!
I want to pass a column name, sometimes a table name, to a stored
procedure, but it didn't work. I tried to define the data type as
char, vachar, nchar, text, but the result were same. Any one know how
to get it work?
Thanks a lot!!
Saiyou[posted and mailed, please reply in news]

Saiyou Anh (wangc@.alexian.net) writes:
> I want to pass a column name, sometimes a table name, to a stored
> procedure, but it didn't work. I tried to define the data type as
> char, vachar, nchar, text, but the result were same. Any one know how
> to get it work?

So why do you need to do this?

While this is possible to do this, you might essentially be throwing
out the baby with the bathtub and loose most of the advantages of
stored procedures. Often this is a token of bad design.

Anyway, I have a longer article on my web site that shows you how to do
it - and why you probably shouldn't.

http://www.sommarskog.se/dynamic_sql.html

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp

Monday, March 12, 2012

Passing a database name as a parameter

Does SQL Server allow you to pass in a Database name and use it in a stored
procedure? I need to do a join on two tables in two different databases.
Here is some code example that I'm trying to accomplish
//call in C# to add a parameter to the command object
cmd.AddInParameter("@.dbname", DbType.String, "dbName.dbo");
--Stored Procedure--
DECLARE @.dbname varchar(50) (this will be "dbname.dbo")
SELECT * From
table1 INNER JOIN @.dbname ON table1.column1ID = @.dbname.column1ID
Sooooooooo....this is quite a scaled down version of the stored procedure
I'm working on, but the premise is that I have to join tables in two
different databases and I want to be able to do this without having to hard
code "dbname.dbo." for every plase in the SP that is accessing tables from
this second database because eventually the SP will be broken when the
database is moved from our development environment. All references to that
one database would have to be different for each environment we have.
Thanks in advance,
John Scott."John Scott" <johnscott@.despammed.com> wrote in message
news:327E4E45-53BE-44C9-815C-865E2E4E384C@.microsoft.com...
> Does SQL Server allow you to pass in a Database name and use it in a
> stored
> procedure? I need to do a join on two tables in two different databases.
>
> Here is some code example that I'm trying to accomplish
> //call in C# to add a parameter to the command object
> cmd.AddInParameter("@.dbname", DbType.String, "dbName.dbo");
>
> --Stored Procedure--
> DECLARE @.dbname varchar(50) (this will be "dbname.dbo")
> SELECT * From
> table1 INNER JOIN @.dbname ON table1.column1ID = @.dbname.column1ID
>
>
> Sooooooooo....this is quite a scaled down version of the stored procedure
> I'm working on, but the premise is that I have to join tables in two
> different databases and I want to be able to do this without having to
> hard
> code "dbname.dbo." for every plase in the SP that is accessing tables from
> this second database because eventually the SP will be broken when the
> database is moved from our development environment. All references to
> that
> one database would have to be different for each environment we have.
> --
> Thanks in advance,
> John Scott.
I would either create a view to reference the table in the other database or
I would create a synonym (in 2005 only). That way the database name is only
coded in one place per object and it's easy to parameterize that db name at
install time. Much easier than trying to do it dynamically in a proc anyway.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||Thanks for the very quick reply David!!
Unfortuanately we are using SQL 2000 and as far as creating a view to
represent a certain table from the second database the database reference in
each view would have to be changed to point at the proper database when
switching environments. I basically need a configureable way to change the
reference to this second database..
if i can't pass in a parameter value....I could live with something like
this:
----
--
DECLARE @.dbname varchar(50)
set @.dbname = "dbname.dbo"
SELECT * From
table1 INNER JOIN @.dbname ON table1.column1ID = @.dbname.column1ID
----
--
Can I do that? Or is there a way to represent a variable as a database
object somehow'
Thanks again,
John Scott.
"David Portas" wrote:

> "John Scott" <johnscott@.despammed.com> wrote in message
> news:327E4E45-53BE-44C9-815C-865E2E4E384C@.microsoft.com...
> I would either create a view to reference the table in the other database
or
> I would create a synonym (in 2005 only). That way the database name is onl
y
> coded in one place per object and it's easy to parameterize that db name a
t
> install time. Much easier than trying to do it dynamically in a proc anywa
y.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>
>|||John Scott wrote:
> each view would have to be changed to point at the proper database when
> switching environments.
Exactly. Do that in the installation script when you create the views.
If you mean you must switch environments at runtime then I'd say that
was best achieved through connection strings in client code, but that
depends what you are doing of course.

> if i can't pass in a parameter value....I could live with something like
> this:
> ----
--
> DECLARE @.dbname varchar(50)
> set @.dbname = "dbname.dbo"
> SELECT * From
> table1 INNER JOIN @.dbname ON table1.column1ID = @.dbname.column1ID
> ----
--
>
> Can I do that? Or is there a way to represent a variable as a database
> object somehow'
>
Dynamic SQL. But as general solution that's a nightmare way to kill a
database. Make sure you read:
http://www.sommarskog.se/dynamic_sql.html
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--