I am filtering the data of an SQL data source by setting a WHERE clause parameter in this formatting [#a] and in the event of a combo box, I would to set the value to this parameter, the previous sql data source for a gridview
SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters["#a"].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();
I need to pass value to the WHERE parameter.
I choose the filtering parsed on non choice ( not form or control or profile or session ...)
This way gives me an error.
Is there any way to give a value for this parameter [#a]?
Try the following:
ASPX:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" DataSourceMode="DataSet"
ConnectionString="<%$ConnectionStrings:MyConnectionString %>"
SelectCommandType="Text"
SelectCommand = "SELECT * FROM TABLE WHERE a=#a"
CancelSelectOnNullParameter="false">
<SelectParameters>
<asp:QueryStringParameter Name="a" QueryStringField="a"
DbType="String" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
C# Code-Behind:
change #a to a:
SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters["a"].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();
If #a is the FIRST parameter you can also set it using INDEX 0:
SqlDataSource2.SelectParameters.Clear();
SqlDataSource2.SelectParameters[0].DefaultValue = cid.ToString();
SqlDataSource2.DataBind();
Related
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.
I have a drop down list that bound to a SqlDataSource.
I have another drop down list that's bound to a different SqlDataSource.
The second SqlDataSource has the first drop down as a Control Parameter.
I'm trying to do this...
<asp:SqlDataSource ID="sqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM Test WHERE Param = #param;"
CancelSelectOnNullParameter="true">
<SelectParameters>
<asp:ControlParameter ControlID="dropDown1" Name="param"
PropertyName="SelectedValue"
ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
dropDown1.SelectedValue = "someValue"
dropDown2.DataBind()
but I don't get any results. However, if I set the second SqlDataSource's Control Parameter to a text box, it works. For example, this works:
<asp:ControlParameter ControlID="txt" Name="param"
PropertyName="Text"
ConvertEmptyStringToNull="true" />
txt.Text = "someValue"
dropDown2.DataBind()
Any ideas why this is?
I ended up figuring this one out. The problem was that the drop down was attempting to bind twice, much like the problem in this question.
I used the suggestion made by Joel Etherton, and now it works perfectly. Although I used a hidden control rather than a label.
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
I am using querystring for passing a value which is used to select the subscription category (variable name subno) on second page.
Based on the subscription category (subno is specified as integer in sql server as part of table categories) second page is invoked using the following URL-
http://localhost:53175/v1/ProductsList.aspx?subno=1
or
http://localhost:53175/v1/ProductsList.aspx?subno=2
In the second page subno is used to query the items based on subno passed.
<asp:SqlDataSource ID="subscription" runat="server"
ConnectionString="<%$ ConnectionStrings:aimnbde3ConnectionString %>"
SelectCommand="SELECT [subscription] FROM [subscription] WHERE ([subno] = #subno)">
<SelectParameters>
<asp:QueryStringParameter Name="subno" QueryStringField="subno" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
This code works well if category(varibale subno) is passed.
In case second page is called directly like
http://localhost:53175/v1/ProductsList.aspx
Then value of subno is interpreted as 0. Do we have a way to change the query(SelectCommand="SELECT [subscription] FROM [subscription] ) based on value of subno ?
Or please suggest the right way to handle the case where subno is not passed
Editing -
Sorry for missing an important part of question-
When http://localhost:53175/v1/ProductsList.aspx is accessing i want to display all the products so the query will be
SELECT [subscription] FROM [subscription]
and in case a value is passed query will be
SELECT [subscription] FROM [subscription] WHERE ([subno] = #subno)
I would change the query to this:
SELECT [subscription] FROM [subscription] WHERE (#subno is null or [subno] = #subno)
and then add the following to the parameter
ConvertEmptyStringToNull = "TRUE"
and the following to the SQLDataSource
CancelSelectOnNullParameter = "FALSE"
So your code would look like this:
<asp:SqlDataSource ID="subscription" runat="server" CancelSelectOnNullParameter="false"
ConnectionString="<%$ ConnectionStrings:aimnbde3ConnectionString %>"
SelectCommand="SELECT [subscription] FROM [subscription] WHERE (#subno is null or [subno] = #subno)">
<SelectParameters>
<asp:QueryStringParameter Name="subno" QueryStringField="subno" Type="Int32" ConvertEmptyStringToNull = "true"/>
</SelectParameters>
</asp:SqlDataSource>
You could simply change where part of SQL query to :
WHERE (#subno = 0 OR [subno] = #subno)
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;
}