M
Mike Dinnis
Hi,
I've been working through a number of turorials to try to learn more about
retrieving data from a SQL database. I think i've mastered techniques where
i create a sql string in the page and pass it to the Db and retrieveing data
from a stored procedure, but I can't get the hang of parameters.
I have a method where I can get the parameters passed to the sp but it
doesn't want to return any results. Here's a copy of my code:
<democode>
Private objCnn as New SqlConnection
("server=(local);database=test;UID=sa;PWD=")
Sub Page_Load (Sender as Object, e as Eventargs)
'Stored procedure with parameters
Dim objCmd As New SqlCommand("sploginUser",objCnn)
objCmd.CommandType = CommandType.StoredProcedure
Dim objParam as New SqlParameter ("@UserName", SqlDbType.char)
objParam.Value = "Mike"
objCmd.Parameters.Add (objParam)
objParam = New SqlParameter ("@Password", SqlDbType.char)
objParam.Value = "m"
objCmd.Parameters.Add (objParam)
Dim objReader As SqlDataReader
Try
objCmd.Connection.Open()
objReader = objCmd.ExecuteReader()
Catch ex as Exception
lblMessage.Text = "Database error: " & ex.message
End Try
dgData.DataSource = objReader
dgData.DataBind()
' objReader.Close
objCmd.Connection.Close()
End Sub
</democode>
This calls the procedure in my database as follows:
<sampleproc>
CREATE PROCEDURE spLoginUser
@UserName varchar,
@Password varchar
AS
SELECT UserID FROM tblUsers
WHERE Username = @Username
AND Password = @Password
GO
</sampleproc>
All I get in return is a blank screen. If i give the parameters values in
the proc then I can get query analyser to return values, just not from my
page. When passing parameters I can see in Profiler that the parameter is
passed to the proc but nothing comes back.
Is there anything obvious here that i'm doing wrong?
Cheers,
<M>ike
I've been working through a number of turorials to try to learn more about
retrieving data from a SQL database. I think i've mastered techniques where
i create a sql string in the page and pass it to the Db and retrieveing data
from a stored procedure, but I can't get the hang of parameters.
I have a method where I can get the parameters passed to the sp but it
doesn't want to return any results. Here's a copy of my code:
<democode>
Private objCnn as New SqlConnection
("server=(local);database=test;UID=sa;PWD=")
Sub Page_Load (Sender as Object, e as Eventargs)
'Stored procedure with parameters
Dim objCmd As New SqlCommand("sploginUser",objCnn)
objCmd.CommandType = CommandType.StoredProcedure
Dim objParam as New SqlParameter ("@UserName", SqlDbType.char)
objParam.Value = "Mike"
objCmd.Parameters.Add (objParam)
objParam = New SqlParameter ("@Password", SqlDbType.char)
objParam.Value = "m"
objCmd.Parameters.Add (objParam)
Dim objReader As SqlDataReader
Try
objCmd.Connection.Open()
objReader = objCmd.ExecuteReader()
Catch ex as Exception
lblMessage.Text = "Database error: " & ex.message
End Try
dgData.DataSource = objReader
dgData.DataBind()
' objReader.Close
objCmd.Connection.Close()
End Sub
</democode>
This calls the procedure in my database as follows:
<sampleproc>
CREATE PROCEDURE spLoginUser
@UserName varchar,
@Password varchar
AS
SELECT UserID FROM tblUsers
WHERE Username = @Username
AND Password = @Password
GO
</sampleproc>
All I get in return is a blank screen. If i give the parameters values in
the proc then I can get query analyser to return values, just not from my
page. When passing parameters I can see in Profiler that the parameter is
passed to the proc but nothing comes back.
Is there anything obvious here that i'm doing wrong?
Cheers,
<M>ike