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();
Related
I am trying to update my dropdownlistB according to the categoryId chosen in dropdownlistA Using this code:
<asp:DropDownList ID="DropDownListA" runat="server" DataSourceID="SqlDataSourceA" DataTextField="Description" DataValueField="Description" AutoPostBack="True"></asp:DropDownList>
<asp:DropDownList ID="DropDownListB" runat="server" DataSourceID="SqlDataSourceB" DataTextField="Title" DataValueField="Title"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceA" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Description] FROM [BookCategory]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSourceB" runat="server" ConnectionString="<%$ ConnectionStrings:MainDbConnectionString1 %>" SelectCommand="SELECT [Title] FROM [BooksInfo] WHERE ([CategoryId] = #CId)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="" Name="CId" QueryStringField="SELECT [CategoryId] FROM [BookCategory]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
I am new to using SQL and queries in ASP.NET and cant figure out what Im doing wrong, dropdownlistB stays empty. (AutoPostBack = true in dropdownlistA, so it should update?)
I think what you are looking for is the asp:ControlParameter like this
<asp:ControlParameter ControlID="DropDownListA" PropertyName="SelectedValue"
Name="EmpID" Type="Int32" DefaultValue="0" />
So the the query is based on the selection of DropDownListA.
I want to pass session value from .aspx code , i.e the source code of the web
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Album.AlbumID, Album.DefaultPhotID, Album.AlbumName, PhotAlbum.Photo FROM Album INNER JOIN PhotAlbum ON Album.DefaultPhotID = PhotAlbum.PhotoID where userid=<% Session["UserId"] %>">
</asp:SqlDataSource>
I am doubtful if this code will work.
Use SessionParameter with SelectParameters to pass Session values
MSDN Doc
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT Album.AlbumID, Album.DefaultPhotID, Album.AlbumName, PhotAlbum.Photo FROM Album INNER JOIN PhotAlbum ON Album.DefaultPhotID = PhotAlbum.PhotoID where userid=#userid">
<SelectParameters>
<asp:SessionParameter Name="userid" Type="String" SessionField="UserId" />
</SelectParameters>
</asp:SqlDataSource>
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
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.
Consider a page, when the page loads, nothing shows up.
It works when i pass the querystring on the browser as this:
http://localhost:51765/foo/foo.aspx?ID=c516f4f4-36a9-40a7-baad-d2419ea631b9
I want it to work when the page load not when i pass the querystring on the browser.
Can someone help me with this?
<asp:SqlDataSource ID="categoriesDataSource" runat="server"
connectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [CategoryID], [Name] FROM [Categories] WHERE ([UserId] = #UserId) ORDER BY [Name]">
<SelectParameters>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="categories" runat="server" AutoPostBack="True"
DataSourceID="categoriesDataSource" DataTextField="Name"
AppendDataBoundItems="True" DataValueField="CategoryID">
<asp:ListItem Value="">-- All Albums --</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="picturesDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [PictureID], [Title], [UploadedOn] FROM [Pictures] WHERE UserId = #UserId AND
(CategoryID = #CategoryID Or #CategoryID IS NULL) ORDER BY UploadedOn DESC"
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="categories" Name="CategoryID" PropertyName="SelectedValue"/>
<asp:QueryStringParameter Name="UserId" QueryStringField="ID" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="picturesDataSource">
</asp:GridView>
It is difficult to answer your question without showing the code of the page or at least explaining what it does. From the url it seems that the page relies on the ID parameter and tries to parse it to a Guid. You need to test whether the ID parameter is passed and use it only in this case:
string id = Request["ID"];
if (!string.IsNullOrEmpty(id))
{
// The ID parameter has been passed => use its value here
}