Set SelectParameter for SqlDataSource SelectCommand with unknown number of variables - asp.net

<asp:SqlDataSource ID="workHourListSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:PlaningSystemConnectionString %>"
SelectCommand="SELECT *
FROM Date AS t1 FULL OUTER JOIN (SELECT * FROM WorkHoursEntry WHERE (WorkerID LIKE #WorkerID)) AS t2 ON t1.PKDate = t2.WorkDay
LEFT OUTER JOIN Worker AS t3 ON t2.WorkerID = t3.WorkerID
LEFT OUTER JOIN (SELECT * FROM Project WHERE ProjectID IN(#Project)) AS t4 ON t2.ProjectID = t4.ProjectID"
FilterExpression="YearMonth IN({0})">
<FilterParameters>
<asp:ControlParameter ControlID="yearMonthFilterLabel" Name="YearMonth"
PropertyName="Text" Type="String" />
</FilterParameters>
<SelectParameters>
<asp:ControlParameter ControlID="WorkerIDLabel" Name="WorkerID"
PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="projectFilterLabel" Name="Project"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Greetings, I have problem with #Project ControlParameter, problem is I don't know how to pass several values and FilterParameters method won't work as I want. Is there any way to pass several variables ? (without using c#).
projectFilterLabel.Text = 'aaa, bbb, ccc';
Basically what I want to do is to select all values from Date table and join them with some values from Project table and output to DataList.
Any help appreciated, I just started learning asp.net
Thanks.

In your asp form create a javascript function to handle the Opening of the window fro your report. In this function set a parameters (var pPassParm) value to a textbox.text (hidden on your parent form) and pass this value with opening of the window ( /reportfolder/report.asx?pPassparm )
On your execution code, create a piece of code that concatenates all your parameters into one string ( textbox.text = 'StartDate = 2012/01/01, EndDate = 2012/02/01, Surname = Jones' etc) before opening the new form for the report.
Refer to post Open a new window and pass parameters to it vb.net asp

I solved this problem by creating new querys with parameters inside (replacing older ones) for SqlDataSource on events.

Related

ASP.NET SqlDataSource, like on SelectCommand

I'm working on asp.net. I Have a SqlDataSource with a query hardcoded on selectcommand:
<asp:SqlDataSource ID="DataSource1" runat="server" CancelSelectOnNullParameter="False"
ConnectionString="<%$ ConnectionStrings:S.Properties.Settings.ConnectionString %>"
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like Case #col1_param When null Then col1 Else #col1_param End)
and ([col2] like Case #col2_param When null Then col2 Else #col2_param End)"
SelectCommandType="Text">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="col1_param" PropertyName="Text"
Type="String" />
<asp:ControlParameter ControlID="TextBox2" Name="col2_param" PropertyName="Text"
Type="String" />
</SelectParameters>
What I want is that if you enter data on one textbox only, the data will display according with that textbox value only on the where clause. And if no values are placed for neither of the textboxes, the the query executes as if there is no where.
Right now with this code,what happens is if you put on one textbox only no data is displayed. The same if all textboxes are empty.
I don't want to use sql stored procedure.
How can I solve this?
Thanks...
Assuming it passes null when there is no text entered, otherwise you will need to check for the empty string
SelectCommand="SELECT * FROM [table]
WHERE ([col1] like '%#col1_param%' or #col1_param is null)
and ([col2] like '%#col2_param%' or #col2_param is null)"
It sounds like you want your query to optionally search a column.
You can use the format
WHERE #col1_param IS NULL OR [col1] LIKE '%#col1_param%'
to property handle the case where the parameter is not specified.
See my question on the issue for a full answer. Granted it was done as a stored procedure, but the concept will hold the same for your SQLDataSource.

Passing an SQL datasource variable in page load?

I have an SQL query but it will will only know the movie type in page load. This will be connected to a datalist.
My query is
select * from movies where movieType = #type
Is there a way to pass parameters at runtime to the sql datasource?
Thanks
You will need to add the parameter to your SqlCommand when you make your SQL call.
myCommand.Parameters.AddWithValue("#type", type);
In this example, the variable you are passing in from .Net is 'type'
This MSDN article should help you understand parameter passing.
If you are asking about SqlDataSource control, yes you can use SQL queries and statements that use parameters.
For example:
var source = new SqlDataSource(myConnectionString,
"SELECT * FROM TableName WHERE ColName = #M);
source.SelectParameters.Add("#M", DbType.Int32, ddl.SelectedValue);
MSDN has good article on this topic.
This can be done with SelectParameters in the markup or codebehind
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
SelectCommand="select * from movies where movieType = #type">
<SelectParameters>
<asp:ControlParameter name="type" ControlID="MyDropdownList" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
More on ControlParameters

How to set parameters for SqlDataSource UpdateCommand

For a Gridview:
I am trying to use a stored procedure for the first time in a SQLDataSource for the UpdateCommand:
<asp:SqlDataSource ID="TECT_DataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"
ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"
SelectCommand="SELECT MPID, User_Id, Last_Name, First_Name
FROM Scripts.vw_Tect_Exam"
UpdateCommand="P_TECT_UPD_EXAM_ID" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="MPID" Type="Int32" />
<asp:Parameter Name="User_Id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
I am wondering how the UpdateParameters get their values set, since I only specify a name?
The procedure P_TECT_UPD_EXAM_ID expects two parameters as input: "in_MPID" and "in_UserId"
I am also wondering how to map those values to the input parameters for the procedure as the names are different?
You can set them something like this :
Example : In the example MPID is the sql parameter name #MPID
<UpdateParameters>
<asp:ControlParameter Name="MPID" ControlID="MPID_TextBox" PropertyName="Text />
<asp:ControlParameter Name="User_Id" ControlID="User_Id_TextBox" PropertyName="Text />
</UpdateParameters>
Correction: Just spotted your proc param names so it must be
<asp:ControlParameter Name="in_MPID" ...............
<asp:ControlParameter Name="in_User_Id" ...............
Hope this helps....
I really wouldn't use a SqlDataSource. It will be much easier if you make the call to the database in the code-behind (or a better yet in a Data Access Layer).
If you use a SqlDataSource the stored procedure call will only be available on that page. Every time you want to make that same call you will have to copy and paste the SqlDataSource or make a UserControl out of it.
The following example uses the Entity Framework to connect to the database and retrieve records:
public List<Record> GetAllRecordsByUserName(string credentials)
{
List<Record> recordList;
using (CustomEntities context = new CustomEntities())
{
IQueryable<Section> recordQuery = from records in context.Records
where records.UserName == credentials
select records;
recordList = recordQuery.ToList<Record>();
}
return recordList;
}

SqlDataSource SelectCommand using 2 Parameters and LIKE does not work

I have the following selectcommand in a SqlDataSource, my problem is that it only returns data when I have values for both Parameters. I need it to also return data when only 1 has a value.
SelectCommand="SELECT user.UserId, UserProfiles.Company, UserProfiles.Address, UserProfiles.JV2, UserProfiles.DB1, aspnet_Membership.Email, user.UserName
FROM user INNER JOIN UserProfiles ON user.UserId = UserProfiles.UserId WHERE (#UserName IS NULL OR #UserName = '' OR user.UserName LIKE '%'+#UserName+'%' ) AND
(#Email IS NULL OR #Email = '' OR user.Email LIKE '%'+#Email+'%')
with the following Parameters
<SelectParameters>
<asp:ControlParameter Name="Email" ControlID="eMail" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="UserName" ControlID="userName" PropertyName="Text" Type="String" />
When I run the query in SQL Server Management Studio and it returns data when I enter only 1 value or both.
When I run the form I only get data returned when I enter values in to both fields.
I found this post: SqlDataSource SelectCommand using LIKE does not work which is similar but the solution hasn't helped.
Obviously I've done something wrong, I just can't see what.
I have had a similar issue and adding this to the sqldatasource fixed it:
<asp:SqlDataSource CancelSelectOnNullParameter="false">

Filter expression not working properly

I am using a GridView and I require the user to be able to filter using 2 controls. One simply filters the type of row - there is a column called action, and the user selects one of the distinct values from the database in a dropdown box, and the gridview only displays the rows with that value in the action column. On it's own this works perfect.
But I am also adding a textbox where the user can type in an ID of either the 'itemID', or the 'parentID'. This works fine even with the previous control.
Problem is, the dropdown box does not work when the textbox is empty (i.e. all ID's are being shown). If I choose a value and click Submit, it just doesn't do anything. Now if I am filtering by ID and then choose a value from the dropdown box, it works perfect.
Here is my filter expression and parameters of the datasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="cleared for privacy"
SelectCommand="SELECT * FROM [Audit] WHERE ([source] = #source)" FilterExpression="action like '{0}%' and (itemID like '{1}' or parentID like '{1}')">
<SelectParameters>
<asp:Parameter DefaultValue="LOGISTICS" Name="source" Type="String" />
</SelectParameters>
<FilterParameters>
<asp:ControlParameter Name="action" ControlID="DropDownList1" PropertyName="SelectedValue" />
<asp:ControlParameter Name="legorinvid" ControlID="txtFilter" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
In my case, replacing the SQL filter expression
LIKE '{1}%' by LIKE '{1}'
made the difference.
The former did not work, the latter did.
Have you tried setting the ConvertEmptyStringToNull="false" field on the control parameter?

Resources