Showing posts with label procedures. Show all posts
Showing posts with label procedures. Show all posts

Tuesday, March 20, 2012

Cant see stored procedures

Hi,

Can anyone help me. I've created a stored procedure in sql server and I'm trying to run it from my asp.net page. On Database Explorer I can't see it, or any for that matter, however I can see tables in the same schema.

Also I can't see it when I build a table adapter either.

Can anybody help?

Thanks

Sam

My first suspicion is that you didn't succeed in creating the stored procedure as you think you have done. To allay that suspicion, can you see the proc in Sql Server Management Studio?

|||

Yes I can see it and execute it, it works fine through sql server

I'm thinking that it might be because I'm using the express versions of the software sql server studio and visual web developer. Do you think that might be limiting the functionality?

|||

No. That wouldn't limit what you can see in the Database explorer. I've just attached Northwind to SqlExpress, and can see the procs, views etc in DataBase Explorer in VWD.

Try clicking the Refresh button at the top of the Database Explorer panel.


|||

check you master database mybe you created it in default database which is usual master when you open New Query window.If it is true put line below on top of your proceureUSE [DATABASE NAME] GO

|||

As JPazgier mentioned you probably created the proc in the default master db. If so, script out the proc, drop the proc from master db, re-create it in the actual db. If that is not the case and the proc is already in the right database, then check your connection string to see if your application is connecting to the right database.

Sunday, February 12, 2012

can't install a CLR stored procedure

I have written 2 CLR stored procedures. I have a local copy of our database to which I was able to successfully deploy them. When I changed the connection string to our production database, it fails when I try to deploy. It gives me an error... EXTERNAL ACCESS ASSEMBLY permission denied on object 'server', database 'master'. I've logged onto the database server and set trustworthy on for that particular database. I also tried...

CREATE ASSEMBLY UpdateJobAdSearch FROM 'I:\DLL\Candidate.dll'
WITH PERMISSION_SET = EXTERNAL_ACCESS

Candidate.dll is the stored procedure dll. I also read on http://msdn2.microsoft.com/en-us/library/ms345106.aspx about doing something like....

USE master
GO

CREATE ASYMMETRIC KEY CandidateKey FROM EXECUTABLE FILE = 'I:\DLL\Candidate.dll'
CREATE LOGIN CandidateLogin FROM ASYMMETRIC KEY CandidateKey
GRANT EXTERNAL ACCESS ASSEMBLY TO CandidateLogin

but I get the errors...

Msg 15208, Level 16, State 1, Line 2
The certificate, asymmetric key, or private key file does not exist or has invalid format.
Msg 15151, Level 16, State 1, Line 3
Cannot find the asymmetric key 'CandidateKey', because it does not exist or you do not have permission.
Msg 15151, Level 16, State 1, Line 4
Cannot find the login 'CandidateLogin', because it does not exist or you do not have permission.


I'm no db admin, so I'm not sure what I need to do to get these installed. I'm not clear on what they mean about creating a login mapped to an asymetric key. Can anyone clear this up in more plain english? Thanks!

Did you sign the Candidate.dll assembly? You can do that with Visual Studio or the .NET Framework 2.0 SDK.

|||

Not originally. I found the Sign option in properties so I checked off for it to sign it and gave it a new key name. But when I tried to deploy, I still got...

Error 1 EXTERNAL ACCESS ASSEMBLY permission denied on object 'server', database 'master'. JobAd

|||

After you signed the assembly, did you successfully create the asymetric key?

|||Bonnie, that's where I'm lost. I assume I have to create the asymetric key on the server? But when I go to Security>Asymetric Keys and right click, the only option I have is refresh. There's nothing there for me to create a new one. So, I'm not sure how to do that? Thanks!|||

You can create asymmetric keys using the DDL you mentioned above (CREATE ASYMMETRIC KEY). See the HandlingLOBUsingCLR sample for step by step instructions for how to install and use an assembly that requires external access. The latest samples MSI is located at http://msdn.microsoft.com/sql/downloads/samples/default.aspx. After you install the Samples MSI, by default you'll find the HandlingLOBUsingCLR sample at drive:\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\HandlingLOBUsingCLR. In that folder will be a readme file that will walk you through it.

--Bonnie

|||

Thanks Bonnie, but I found this earlier and it worked great. Thanks for your help!

http://www.sqljunkies.com/WebLog/ktegels/articles/SigningSQLCLRAssemblies.aspx

can''t import views and store procedures

I recently set up a new sql 2005 standard edition and planning to mirgrate our production sql 2000 data. but only the tables were migrated from the copy data or import / export task. I cannot find a way to recreate the store procedures and views on the new server without create one view and one store procedure at a time unless all hundreds of views and procedures were rescripted. can anyone help

Thank

Andy Wong

awong@.virginiadare.com

Right click the database > Taks > Generate Scripts, this will give you the option to put it in one output-file

Jens K. Suessmeyer

http://www.sqlserver2005.de

Friday, February 10, 2012

cant get sqldatasource to return results from a stored procedure

I thought I would impliment a new feature of my web page using stored procedures and the SqlDataSource object, for practice or whatever, since I don't normally use that stuff.

This is the stored procedure:

set ANSI_NULLSONset QUOTED_IDENTIFIERONgo-- =============================================-- Author:Lance Colton-- Create date: 7/31/06-- Description:Find the contest winner-- =============================================ALTER PROCEDURE [dbo].[AppcheckContest]-- Add the parameters for the stored procedure here@.BeginDatedatetime ='1/1/2006', @.EndDatedatetime ='12/31/2006', @.SectionIDint = 10,@.WinnerIDint = 0OUTPUTASBEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;-- Insert statements for procedure hereSELECT top 1 @.WinnerID = P.UserIDFROM cs_Posts PWHERE P.PostDateBETWEEN @.BeginDateAND @.EndDateAND P.SectionID = @.SectionIDAND P.UserID <> 2100-- I don't want to win my own contest...AND SettingsID = 1000-- This number could be different if i had more than one CS installed?AND IsApproved = 1ORDER BYNEWID()-- yes this is slow, but it works...RETURN @.WinnerIDEND

It's really simple - just needs to return the one randomly chosen integer userID. I've tested it in query designer or whatever it's called in Management Studio and it works fine there at least.

Thinking I was done the hard part, I created a new web form in visual studio, dropped a SqlDataSource on it, and used the 'configure data source' wizard from the smart tag to do all the work for me. I didn't have any trouble using the wizard to select my stored procedure, and i'm using the sa connection string to simplify my debugging. I tried using the FormParameter / FormField way of getting the output and setting the input parameters. I can't seem to get it working though. There's no errors or anything, just the output isn't coming through.

Here's the code from the aspx codebehind file:

Partial Class Contest Inherits System.Web.UI.Page Protected Sub btnSelectWinner_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectWinner.Click Dim stuff As New System.Web.UI.DataSourceSelectArguments SqlDataSource1.Select(stuff) SqlDataSource1.DataBind() lblWinnerID.Text = SqlDataSource1.SelectParameters("WinnerID").ToString End SubEnd Class

As you can see, I wasn't sure if you're supposed to call databind() or select() to actually get the stored procedure to execute, so I tried both. I was hoping the last line of code there would set the label to the value contained in the @.WinnerID parameter, but instead it sets it to "WinnerID".

Here's the code from the .aspx file. Most of this was generated by the Wizard, but I messed around with it a bit.

<%@. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Contest.aspx.vb" Inherits="Contest" title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="CPHMain" Runat="Server"> <asp:Button ID="btnSelectWinner" runat="server" Text="Find Winner" /> <asp:Calendar ID="Calendar_From" runat="server"></asp:Calendar> <asp:Calendar ID="Calendar_To" runat="server"></asp:Calendar> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:appcheck-csConnectionString-sa%>" SelectCommand="AppcheckContest" SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"> <SelectParameters> <asp:FormParameter FormField="Calendar_From" Name="BeginDate" Type="DateTime" /> <asp:FormParameter FormField="Calendar_To" Name="EndDate" Type="DateTime" /> <asp:Parameter DefaultValue="10" Name="SectionID" Type="Int32" /> <asp:formParameter FormField="lblWinnerID" defaultvalue="666" Direction="InputOutput" Name="WinnerID" Type="Int32" /> </SelectParameters> </asp:SqlDataSource>   <asp:Label ID="lblWinnerID" runat="server" Text="?"></asp:Label></asp:Content>

okay well i gave up on using a sqldatasource since nobody replied.

here's my final working code using an sqlCommand object instead:

PartialClass ContestInherits System.Web.UI.PageProtected Sub btnSelectWinner_Click(ByVal senderAs Object,ByVal eAs System.EventArgs)Handles btnSelectWinner.ClickDim objCommandAs New SqlClient.SqlCommandDim objConnAs New SqlClient.SqlConnectionDim strQueryResultAs New Object objConn.ConnectionString = ConfigurationManager.ConnectionStrings("appcheck-csConnectionString-sa").ConnectionString objCommand.Connection = objConn objCommand.CommandType = CommandType.StoredProcedure objCommand.CommandText ="AppcheckContest"Dim intWinnerIDParamAs New SqlClient.SqlParameter("@.WinnerID", SqlDbType.Int)Dim DateFromParamAs New SqlClient.SqlParameter("@.BeginDate", SqlDbType.DateTime)Dim DateToParamAs New SqlClient.SqlParameter("@.EndDate", SqlDbType.DateTime) intWinnerIDParam.Direction = ParameterDirection.Output DateFromParam.Direction = ParameterDirection.Input DateToParam.Direction = ParameterDirection.Input objCommand.Parameters.Add(intWinnerIDParam) DateFromParam.Value = Calendar_From.SelectedDate DateToParam.Value = Calendar_To.SelectedDate objCommand.Parameters.Add(DateFromParam) objCommand.Parameters.Add(DateToParam) objConn.Open() objCommand.ExecuteScalar() objConn.Close()Try lblWinnerID.Text ="Winner ID: <a target='_blank' href='http://forum.appcheck.net/web/user/Profile.aspx?UserID=" & intWinnerIDParam.Value & "'>" & intWinnerIDParam.Value & "</a>" Catch ex As Exception lblWinnerID.Text = "error"End Try End SubEnd Class
<%@. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Contest.aspx.vb" Inherits="Contest" title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="CPHMain" Runat="Server"> Contest Start Date:<asp:Calendar ID="Calendar_From" runat="server"></asp:Calendar> Contest End Date:<asp:Calendar ID="Calendar_To" runat="server"></asp:Calendar> <asp:Button ID="btnSelectWinner" runat="server" Text="Find Winner" /> <asp:Label ID="lblWinnerID" runat="server" Text="?"></asp:Label></asp:Content>
set ANSI_NULLSONset QUOTED_IDENTIFIERONgo-- =============================================-- Author:Lance Colton-- Create date: 7/31/06-- Description:Find the contest winner-- =============================================ALTER PROCEDURE [dbo].[AppcheckContest] @.BeginDatedatetime, @.EndDatedatetime, @.SectionIDint = 10,@.WinnerIDint = 0OUTPUTASBEGINSET NOCOUNT ON;SELECT top 1 @.WinnerID = P.UserIDFROM cs_Posts PWHERE P.PostDateBETWEEN @.BeginDateAND @.EndDateAND P.SectionID = @.SectionIDAND P.UserID <> 2100-- I don't want to win my own contest...AND SettingsID = 1000-- This number could be different if i had more than one CS installed?AND IsApproved = 1ORDER BYNEWID()-- yes this is slow, but it works...END
|||Hi,

I've had a similar issue for the last few days.
I only found 1 page that helped me solve it:
http://dotnetjunkies.com/QuickStartv20/util/srcview.aspx?path=~/aspnet/samples/data/RetValAndOutputParamsInsert.src

I hope this helps you.

Cheers
Luke

|||

The above posters link is correct. The output/return value parameters are only available during the "ED" events (Inserted/Updated/Deleted). So if you want them, you have to capture them during that event.

And just a FYI - NewID isn't random, not even close. Google a better random method, which usually involves taking the last 32 bits of newid and feeding it into the rand function.