I am trying to use asp:QueryStringParameter to change witch SQL table column I want to get. But when I try, I just get the Query String Parameter as every row of a new column.
Here is what I have so far.
<asp:SqlDataSource ID="getContact" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString1.ProviderName %>"
SelectCommand="
SELECT
[fName],
[lName],
#c as contact
FROM
RidesMaster
WHERE
[userID] = #ID">
<SelectParameters>
<asp:QueryStringParameter Name="c" QueryStringField="c" Type="String" />
<asp:QueryStringParameter Name="ID" QueryStringField="ID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
You could do it using a CASE statement so long as your dynamic columns are of the same type or could be cast to the same type:
SELECT
[fName],
[lName],
CASE #c
WHEN 'column1' THEN [column1]
WHEN 'column2' THEN [column2]
WHEN 'column3' THEN [column3]
ELSE 'column4'
END as contact
FROM
RidesMaster
WHERE
[userID] = #ID
Related
How do you set a parameter passed to a SqlDataSource to be null? I have the code below where the parameter "#var1" is set from a TextBox. However, if that value is not defined I need to set "#var1" to be null so that the coalesce returns true.
Is there a way to do this or am I going about it the wrong way?
I've also tried setting the DefaultValue="null" and dsABC.SelectParameters["#var1"]=null which both lead to "Input string was not in a correct format" errors.
<asp:SqlDataSource ID="dsABC" runat="server" ConnectionString="<%$ ConnectionStrings:abc123 %>"
SelectCommand=" SELECT col1, col2, col3
FROM table1 as T1
WHERE COALESCE(#var1, T.ID) = T.ID">
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16" />
</SelectParameters>
</asp:SqlDataSource>
Add the ConvertEmptyStringToNull and the CancelSelectOnNullParameter attributes. The first sets the parameter to null when empty, the second lets the query run (default behavior is to not run a query with a null parameter).
<SelectParameters>
<asp:ControlParameter Name="var1" ControlID="inputA" PropertyName="Value" DefaultValue="0" Type="Int16"
ConvertEmptyStringToNull="true"
CancelSelectOnNullParameter="false" />
</SelectParameters>
I have this sqlDataSource
#userId is my parameter for Current user in a system .
<asp:SqlDataSource ID="dsProfit" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="select name, sum(value) as suma from AllProfitView where IdUser=#userId group by name order by sum(value) desc">
</asp:SqlDataSource>
In code Behind I have this in Page_Load :
dsProfit.SelectParameters.Add("#userId", cui.getCurrentId());
public Guid getCurrentId()
{
MembershipUser currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
return currentUserId;
}
when start the page it's blows with ERROR :Must declare the scalar variable "#userId".
Add SelectParameter to your sqlDataSource
<asp:SqlDataSource ID="dsProfit" runat="server"
ConnectionString="<%$ ConnectionStrings:DefaultConnection %>"
SelectCommand="select name, sum(value) as suma from AllProfitView
where IdUser=#userId group by name order by sum(value) desc">
<SelectParameters>
<asp:Parameter Name="userId" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
and assign likes this
dsProfit.SelectParameters["userId"].DefaultValue =
cui.getCurrentId().ToString();
try to define selectparameter inside of your SqlDatasource
<asp:SqlDataSource ID="dsProfit" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="select name, sum(value) as suma from AllProfitView where IdUser=#userId group by name order by sum(value) desc">
<SelectParameters>
<asp:Parameter Name="userId" Type="int32" />
</SelectParameters>
</asp:SqlDataSource>
so your parameter is int so convert it to string
dsProfit.SelectParameters["userId"].DefaultValue = cui.getCurrentId().toString();
<asp:SqlDataSource ID="HopefulDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand= "SELECT id, regGroupID, amountReceived, other FROM table"
UpdateCommand="UPDATE table
SET [amountReceived] = #amountReceived
WHERE [regGroupID] = #regGroupID">
<SelectParameters>
<asp:ControlParameter ControlID="ddlCourses" Name="ddlSelectedCourse" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="regGroupID" Type="Int32" />
<asp:Parameter Name="amountReceived" Type="Decimal" />
other parameters
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
The above works when I change "WHERE [regGroupID] = #regGroupID" to either
WHERE [id] = #id
or
WHERE [regGroupID] = 2
You need to add "regGroupID" to the DataKeyNames collection in your GridView declaration. Something like this:
<asp:GridView ID="yourGridViewId" DataKeyNames="regGroupID" ... >
...
</asp:GridView>
See the DataKeyNames documentation:
You must set the DataKeyNames property in order for the automatic
update and delete features of the GridView control to work. The values
of these key fields are passed to the data source control in order to
specify the row to update or delete.
Note: it's been a while since I used this, so you might need to include both the primary key, and your other key field. Like this:
DataKeyNames="id,regGroupID"
I have a textbox with id = txt_SearchLibrary which is also my controlparameter that I am using to filter my sqldatasource, I want to get all the results when I don't type something in my searchtext box, but below codes results 0 rows. I tried 2 way but both didn't work.
first one:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>"
SelectCommand="SELECT * FROM [Books] WHERE ([BookName] LIKE '%' + #searchText + '%') OR #searchText IS NULL">
<SelectParameters>
<asp:ControlParameter ControlID="txt_SearchLibrary" Name="searchText"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
second one:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>"
SelectCommand="SELECT * FROM [Books] WHERE ([BookName] LIKE '%' + #searchText + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="txt_SearchLibrary" DefaultValue="" Name="searchText"
PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The Text property is probably "" or String.Empty, rather than NULL which you're checking for in your SQL statement.
I am using the stored procedure below for the select command of a SqlDataSource, and I'm trying to pass a query string parameter, but I get this error:
Procedure or function 'ActivationCampaignGetById' expects parameter '#campaignId', which was not supplied.
The parameter is present in the query string:
http://localhost:62681/Activations/ActivationCampaignDetails.aspx?id=98
Here is my DataSource code:
<asp:SqlDataSource ID="campaignDataSource" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:ProVantageMediaManagement %>"
SelectCommand="ActivationCampaignGetById" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="98" Name="campaignId"
QueryStringField="id" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
And my stored procedure declaration:
ALTER procedure [dbo].[ActivationCampaignGetById]
#campaignId int
as
select
Not sure this will helps, but you need to set the parameter direction to Input:
<asp:QueryStringParameter DefaultValue="98" Name="campaignId"
Direction="Input" QueryStringField="id" Type="Int32" />