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)
Related
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();
I have a textbox that I want to make into a search bar. My table has an int "id" and varchar(50) "name". I want to search for either the name or the id in the same textbox. Is that even possible? I've tried many things but haven't gotten to a solution yet.
Here's what I have:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:New_QuickBooksConnectionString %>"
SelectCommand="SELECT * FROM [Testing1]" FilterExpression="([id] = {0}) OR ([name] LIKE '%(CAST {0} AS VARCHAR(50))%')">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" Type="int32" />
</FilterParameters>
</asp:SqlDataSource>
This just returns the id when I type in a number. It doesn't work for name. What can I do?
Try changing the type = "String" and convert ID to String, like CONVERT([Id],'System.String') like '{0}'
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'm trying to use a asp:SqlDataSource driven by a SQL Select with Arguments. It works as long as I don't have any arguments. I can run the parameterized query via studio and it works and returns rows. If I any arguments, then the asp:View doesn't render.
First, the code returns 2 rows when I execute the SQL via Studio.
Second, I don't want it as a stored proc. Deployment issue. Smile, let it go. :-)
<asp:DropDownList ID="lstUsers" runat="server" DataSourceID="sqlGetCSGUsersOnClaim" DataTextField="username" DataValueField="userID" />
<asp:SqlDataSource ID="sqlGetCSGUsersOnClaim" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer%>" SelectCommandType="Text" SelectCommand="
SELECT aspnet_Users.UserId as userID, aspnet_users.username as username
FROM claims, aspnet_Users
WHERE claims.claimid = #ClaimID and
(Claims.AdjusterID = aspnet_Users.UserId or Claims.SupervisorID = aspnet_Users.UserId )">
<SelectParameters>
<asp:SessionParameter Name="ClaimID" SessionField="ClaimID" Type="Int32" DbType="Int32" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the #ClaimID and SelectParameters arguments, the dropdown populates. It only fails once I add the parameter.
What am I doing wrong?
Thanks in advance,
Jason
Well it turned that if I removed the Type and DBType attributes of the SelectParameter, the code worked. sigh....
I have no idea, why though.
I have a page 3 datacontrols (in order, datalist->grid->listview, the selection from one feeds into the next) works perfectly locally, using dot net connector for mysql. My webhost uses ODBC, and I had to remove [ ] from the select statements in my code and put the table names. I removed the [ ] 's and my page runs, and my 1st/datalist control shows but now my "selected value" of my datalist, doesn't populate my grid control.
I imagine theres another peculiarity with the way odbc handles parameters.
Here is my original, for my grid:
<asp:SqlDataSource ID="recipegrid" runat="server"
ConnectionString="<%$ ConnectionStrings:exoticingConnectionString %>"
ProviderName="<%$ ConnectionStrings:exoticingConnectionString.ProviderName %>"
SelectCommand="SELECT [Id], [Name], [Cal], [Pro], [Fat], [Carb], [Fiber], [Chol], [Sod] FROM [tblrecipes] WHERE ([filenameid] = #filenameid) ORDER BY [name]">
<SelectParameters>
<asp:ControlParameter ControlID="DataList1" Name="filenameid"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
example of changes to select that allowed my page to run again:
SelectCommand="SELECT tblrecipes.Id, tblrecipes.Name, tblrecipes.Cal, tblrecipes.Pro, tblrecipes.Fat, tblrecipes.Carb, tblrecipes.Fiber, tblrecipes.Chol, tblrecipes.Sod FROM tblrecipes WHERE tblrecipes.filenameid = #filenameid ORDER BY tblrecipes.name">
BTW,
I also tried removing my scriptmanager & updatepanel,, and using autopostbacks instead in my controls, jik, and nothing changed so I put it back.
changed :
WHERE ([filenameid] = #filenameid)
to:
WHERE ([filenameid] = ?)
and it worked