How to change into stored Procedure

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2005
Posts: 54
Reputation: benyam_dessu is an unknown quantity at this point 
Solved Threads: 0
benyam_dessu benyam_dessu is offline Offline
Junior Poster in Training

How to change into stored Procedure

 
0
  #1
Dec 21st, 2007
Dear friends:
I've copy the asp.net code from Unleashed book. I want to use this part of the code to take stored procedure rather than select statment. The stored procedure I write for is this:

*********************************************************
CREATE PROCEDURE authenticatedUser
(
@userName Varchar ( 20 ) ,
@userStatus Varchar (20),
@userPassword Varchar(20),
@jobTitle Varchar(20) Output,
@projectId Varchar(20) Output
)

AS

select projectId, jobTitle
From amcduser
where userName = @userName and userPassword = @userPassword and userStatus = "Active"
GO
*********************************************************

When the Login button is cliked the next event handler (from Asp.Net Unleashed book) will be executed. But I want to use my stored procedure instead of select statment.

How can I do that dear friends.

************************************************************
Sub button_Click(ByVal s As Object, ByVal e As EventArgs)
Dim dstEmployees As DataSet
Dim conNorthwind As SqlConnection ' Object
Dim dadEmployees As SqlDataAdapter ' no object
Dim arrvalues(1) As Object
Dim dvwEmployees As DataView
Dim intEmployeesIndex As Integer

'Get chached Dataview
dvwEmployees = Cache("amdcuser")
If dvwEmployees Is Nothing Then
dstEmployees = New DataSet()
conNorthwind = New SqlConnection("server=(local);database= childdatabase ;Trusted_Connection=yes")
dadEmployees = New SqlDataAdapter("select * From amcduser", conNorthwind)

dadEmployees.Fill(dstEmployees, "amdcuser")
dvwEmployees = dstEmployees.Tables("amdcuser").DefaultView()
dvwEmployees.Sort = "userName , userPassword"
Cache("amdcuser") = dvwEmployees
End If
'Find The Employee
arrvalues(0) = txtusername.Text
arrvalues(1) = txtpassword.Text
intEmployeesIndex = dvwEmployees.Find(arrvalues)
If intEmployeesIndex <> -1 Then
lbltest.Text = txtusername.Text & " , " & txtpassword.Text
Session("jobtitle") = dvwEmployees(intEmployeesIndex).Row("jobTitle")
Session("projectid") = dvwEmployees(intEmployeesIndex).Row("projectId")

lbltest.Text = Session("jobtitle") & "<>"
lbltest.Text &= Session("projectid")
Else
test.Text = "Employee Not Found"
End If
End Sub
*********************************************************

Regards,

Ben
Reply With Quote Quick reply to this message  
Join Date: Dec 2003
Posts: 2,414
Reputation: alc6379 has a spectacular aura about alc6379 has a spectacular aura about alc6379 has a spectacular aura about 
Solved Threads: 123
Team Colleague
alc6379's Avatar
alc6379 alc6379 is offline Offline
Cookie... That's it

Re: How to change into stored Procedure

 
0
  #2
Dec 26th, 2007
You have to make a SqlCommand object, and change the CommandType to "CommandType.StoredProcedure". Then when you initialize the SqlDataAdapter, you pass that SqlCommand to the SqlDataAdapter.

I just found this pretty good code snippet, from this site:

  1. sqlConnection = new SqlConnection( "Integrated Security=yes;Initial Catalog=Northwind;Data Source=(local)" );
  2.  
  3. //pass the stored proc name and SqlConnection
  4. sqlCommand = new SqlCommand( "Employee Sales by Country", sqlConnection );
  5. //important to set this as StoredProcedure is *not* the default
  6. sqlCommand.CommandType = CommandType.StoredProcedure;
  7.  
  8. //instantiate SqlAdapter and DataSet
  9. sqlDataAdapter = new SqlDataAdapter( sqlCommand );

The example is in C#, but it illustrates how you'd use the SqlCommand instead of just feeding a string to the SqlDataAdapter.
Last edited by alc6379; Dec 26th, 2007 at 9:49 am. Reason: make it look nicer
Alex Cavnar, aka alc6379
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC