Showing posts with label instead. Show all posts
Showing posts with label instead. Show all posts

Monday, March 26, 2012

Passing in query paramater values through WebUserControls

Hi,

I don't want to pass in parameter values in the url to get a certain record from my database (for a better search engine result) so instead i'm using webuser controls (this is amust) with Public Values that should be read by the SqlDataSource. So in the page that contains the usercontrol there is something like:

UserControl.Value = "myvalue"

Now I want the SqlDataSource in the usercontrol to read this value and pass it into a query (in the parameter of the WHERE statement, eg.: @.recordID = myvalue).

Thanks in advance!

Hi,

You should create a public attribute in your user control(eg. below). When using the user control, regard it as a input parameter. In this way ,the parameter can be passed to the SqlDataSource in the user control.

private string _para1;public string para1{get{return _para1; }set{ _para1=value; }}
Thanks.

Tuesday, March 20, 2012

Passing a variable into a stored procedure

Please take a look at line 18, how can I pass a variable into this instead of hard coding "Ann" in as the parameter.

I am wanting to do something along the following lines:
18. cmdLastName.Parameters.Add( "@.firstname", " & myvariable & " )

1. <%@. Import Namespace="system.Data.SqlClient" %>
2. <%@. Import Namespace="System.Data" %>
3. <%
4. Dim conPubs As SqlConnection
5. Dim cmdLastName as SqlCommand
6. Dim paramLastName As SqlParameter
7. Dim paramphone As SqlParameter
8. Dim strLastName As String
9. Dim phone as string

12. conPubs = New SqlConnection
13. ("server=localhost;uid=WebUserRX;pwd=ViglenWebUser;database=pubs")
14. cmdLastName = New SqlCommand("GetLastName", conPubs)
15. cmdLastName.CommandType = CommandType.StoredProcedure

17. 'FirstName Input Parameter
18. cmdLastName.Parameters.Add( "@.firstname", "Ann" )

'LastName Output Parameter
paramLastName = cmdLastName.Parameters.Add( "@.lastname", SqlDbType.Varchar )
paramphone = cmdLastName.Parameters.Add( "@.phone", SqlDbType.Varchar )

paramLastName.Size = 40
paramLastName.Direction = ParameterDirection.Output
paramphone.Size = 40
paramphone.Direction = ParameterDirection.Output

'Execute Command
conPubs.Open()
cmdLastName.ExecuteNonQuery()

'Retrieve value of Output Parameter
If Not IsDBNull( cmdLastName.Parameters( "@.lastname" ).value ) then
strLastName = cmdLastName.Parameters( "@.lastname" ).value
phone = cmdLastName.Parameters( "@.phone" ).value
else
strLastName = "Unknown"
End if
conPubs.Close
%>
The last name is <%=strLastName%><br>
Phone = <%=phone%
ThanXThere are several ways. I think the easiest to code would be like this (you'd need to use the correct datatype and length):

18. cmdLastName.Parameters.Add("@.firstname", SqlDbType.VarChar,99).Value=myvariable

Terri|||Thanks that works but do you know how I can return more than one record, do I need a loop around line 32? I have a dropdownlist which when autoposted sends a variable to a stored procedure, then only one row is returned. What I would like to do is when the dropdown is autoposted all the records (all rows) are returned and put into a datagrid.

When I use SQL within an aspx page I use a datareader and simply bind the result of the SQL to a web control e.g

myDropDownList2.DataSource = myDataReader
myDropDownList2.DataBind()

But via the Stored Procedure method I cannot see how this is achieved. Any code samples would be much appreciated.

1. <%
2. Dim conPubs As SqlConnection
3. Dim cmdLastName as SqlCommand
4. Dim paramLastName As SqlParameter
5. Dim strLastName As String
6. Dim mystr as string

8. mystr = myDropDownList2.SelectedItem.Value
9. Response.write("mystr= " & mystr & "<br>")

11. conPubs = New SqlConnection
12. ("server=marketstore;uid=steve;pwd=;database=Contents")
13. cmdLastName = New SqlCommand("GetLastNames", conPubs)
14. cmdLastName.CommandType = CommandType.StoredProcedure

17. 'FirstName Input Parameter
18. cmdLastName.Parameters.Add("@.firstname", SqlDbType.VarChar,99).Value = mystr

20. 'LastName Output Parameter
21. paramLastName = cmdLastName.Parameters.Add( "@.lastname", SqlDbType.Varchar )

24. paramLastName.Size = 40
25. paramLastName.Direction = ParameterDirection.Output

27. 'Execute Command
28. conPubs.Open()
29. cmdLastName.ExecuteNonQuery()

31. 'Retrieve value of Output Parameter
32. If Not IsDBNull( cmdLastName.Parameters( "@.lastname" ).value ) then
33. strLastName = cmdLastName.Parameters( "@.lastname" ).value
34. else
35. strLastName = "Unknown"
36. End if
37. conPubs.Close
%>|||Sure, you can take the resultset of a stored procedure, put them into a DataReader, and then bind the DataReader to the drop-down list. Replacing your lines 20-37:

20. Dim myReader As SqlClient.SqlDataReader
21. 'Execute Command
22. conPubs.Open()
23. myDataReader = cmdLastName.ExecuteReader()
24. myDropDownList2.DataSource = myDataReader
25. myDropDownList2.DataBind()
26. conPubs.Close

The SELECT statement in your stored procedure would be something like:

SELECT lastname FROM myTable WHERE firstname = @.firstname

where currently you probably have something like this:
SELECT @.lastname = lastname FROM myTable WHERE firstname = @.firstname

You would not use an Output parameter when you are returning more than one row. Instead you would return a set of records.

Terri|||Thanks I have that working now, but now I have a new issue, I am wanting to use a DataAdapter instead of a Datareader because i wish to add paging to my site, do you know the equivalent for 'Parameters' in DataAdapter terms as I wish to pass this variable into my stored proc and I am getting this error message, thanks in advance.

Compiler Error Message: BC30456: 'Parameters' is not a member of 'System.Data.SqlClient.SqlDataAdapter'.

Source Error:

Line 23: Dim paramvar As New SqlParameter
Line 24:
Line 25:
Line 26: paramvar = objDA.Parameters.Add("@.myvar", SqlDbType.VarChar,99).Value = "Steve"

Source File: C:\Inetpub\wwwroot\aspnet\datalistpaging2.aspx Line: 26|||You don't add parameters to a data adapter. You add them to the SQLCommand just as before.

Please review theTutorials section of this site, especially Server Side Data Access, and "Parameterized Selects" within that topic. You should be able to work out what you need for yourself from that.

Here's a stab at your code, but I did not test it.

1. <%
2. Dim conPubs As SqlConnection
3. Dim cmdLastName as SqlCommand
4. Dim paramLastName As SqlParameter
5. Dim strLastName As String
6. Dim mystr as string

8. mystr = myDropDownList2.SelectedItem.Value
9. Response.write("mystr= " & mystr & "<br>")

11. conPubs = New SqlConnection
12. ("server=marketstore;uid=steve;pwd=;database=Contents")
13. cmdLastName = New SqlCommand("GetLastNames", conPubs)
14. cmdLastName.CommandType = CommandType.StoredProcedure

17. 'FirstName Input Parameter
18. cmdLastName.Parameters.Add("@.firstname", SqlDbType.VarChar,99).Value = mystr

20. Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter(cmdLastName)
21. Dim myDataSet As DataSet = New DataSet()
22. myDataAdapter.Fill(myDataSet,"LastNames")
23. myDropDownList2.DataSource = myDataSet.Tables("LastName").DefaultView
24. myDropDownList2.DataBind()
25. conPubs.Close

Terri

Friday, March 9, 2012

Passing @ to database instead of interpreting it as a parameter

I am using a secondary datasource in my reports, but it requires the @. symbol
to be passed to the database instead of being used as a parameter (functions
begin with @.). Is there any way to get this to work with reporting services?Open your datasource query, double click onthe ellipsis (...) go to the
parameters tab and make sure the @.functions are not mapped to a parameter...
I suspect you will have problems with this... It may keep adding the
parameter.
You can also try using dynamic sql...or maybe changing the name of those
functions... @. is a special character in SQL...
--
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
I support the Professional Association for SQL Server ( PASS) and it''s
community of SQL Professionals.
"Ben" wrote:
> I am using a secondary datasource in my reports, but it requires the @. symbol
> to be passed to the database instead of being used as a parameter (functions
> begin with @.). Is there any way to get this to work with reporting services?