how to use url parameters in commandparameters - asp.net

i'm working on asp project and i want to use commandparameters
my question is how to put the value of controlid by using url parameter
i try this but doesnt working
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=DentistEntities" DefaultContainerName="DentistEntities" CommandText="SELECT p.[id],p.[firstName],p.[lastName],p.[DOB],p.[firstVisiting] FROM DentistEntities.Patients AS p WHERE p.[firstName]=#firstname">
<CommandParameters>
<asp:ControlParameter Name="firstname" ControlID='<% Request.QueryString["firstName"] %>' Type="String"/>
</CommandParameters>
</asp:EntityDataSource>
error on page
Could not find control '<% Request.QueryString["firstName"] %>' in ControlParameter 'firstname'.

There is a special QueryStringParameter for that:
<asp:QueryStringParameter Name="firstname" QueryStringField="firstName" Type="String"/>

Related

populating dropdownlist from another dropdown selection in ASP.Net Web Form

I am trying to Populate data into the Company DropDownList after selecting the Country from the CountryDropDownList. Complete newbiew on ASP.Net Web Form. So please excuse little knowledge.
I have been trying to do it only on the .aspx page which I am not entirely sure if I could do it. My code is as follows, which doesn't populate anything on the CompanyDropDownList at all. The problem is I have been writing the code looking at different sources but I guess I am not entirely sure where the problem is and what am I doing wrong. If anyone can explain a little bit and spot my errors would be really helpful.
<asp:DropDownList
ID="CompanyDropDownList"
runat="server"
OnSelectedIndexChanged="CompanyDropDownList_SelectedIndexChanged"
DataSourceID="CompanyNameSqlDataSource"
DataTextField="CompanyName"
DataValueField="CompanyID"
AppendDataBoundItems="True"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource
ID="CompanyNameSqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:BIGroupCostConnectionString %>"
OnSelecting="CountryNameSqlDataSource_Selecting"
SelectCommand="SELECT CompanyName, countryID FROM Company Where CompanyID = #CompanyID">
<SelectParameters>
<asp:ControlParameter
Name="CountryID"
ControlID="CountryDropDownList"
PropertyName="SelectedValue"
Type= "String"/>
<asp:Parameter Name="CountryID" />
</SelectParameters>
</asp:SqlDataSource>
Thanks.
I think the problem is that you are passing the company drop-down as parameter instead of the country you mention.
<SelectParameters>
<asp:ControlParameter Name="CompanyID" ControlID="CompanyDropDownList"
PropertyName="SelectedValue" Type= "String"/>
<asp:Parameter Name="CompanyID" />
Shouldn't it be your Country control?
Ademar
Found the problem. The problem was the following line of code. Removing the line showed the data correctly.
<asp:Parameter Name="CountryID" />

Using a dropdownlist control inside a formview

I am new to asp.net and i have a problem using a dropdownlist control inside a formview and passing its value to the related sqldatasource. When i use the code below i get the following exception
Exception Details: System.InvalidOperationException: Could not find control 'ddlCategory' in ControlParameter 'categoryId'.
The Dropdownlist inside a formview.
<asp:DropDownList ID="ddlCategory" DataSourceID="ObjectDataSourceCategory" DataTextField="NAME" DataValueField="ID" runat="server" />
The SQL Data Source
<asp:ObjectDataSource ID="sqlDataSourceItem" TypeName="Item" runat="server"
SelectMethod="getItem"
InsertMethod="insertItem"
UpdateMethod="updateItem">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="id" Name="id" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter ControlID="ddlCategory" Name="categoryId" PropertyName="selectedValue" />
</InsertParameters>
</asp:ObjectDataSource>
And i have found the solution to this problem. I have changed the ID of the DDL in the control parameter. It works like below since this is the final generated id of that control. But i think there must be an easier and better way. Any help would be appriciated.
<asp:ControlParameter ControlID="ctl00$main$frmViewItem$ddlCategory" Name="categoryId" PropertyName="selectedValue" />
This answer will provide a solution to your problem:
You need a recursive findcontrol() method.
It is because of your ddlCategory is inside formview and you are using the master page. The best way is to overwrite the 'FindControl' function of master page.
pls see the following link for detail:
http://geekswithblogs.net/AzamSharp/archive/2006/08/27/89475.aspx

Setting SqlDataSource Parameters from Request.Form

I'm trying to create simple search. I have "txtSearch" textBox and "search" button in MasterPage, button has PostbackUrl= Search.aspx
In MasterPage
<asp:TextBox ID="txtSearch" runat="server" ValidationGroup="b" Text="Users Search" ForeColor="Silver">Users Search</asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" ValidationGroup="b"
PostBackUrl="~/Search.aspx" onclick="btnSearch_Click" />
In Search.aspx i have GridView that shows me results and SqlDataSource
<asp:SqlDataSource ID="SqlDataSearchResult" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT vw_aspnet_Users.UserId, vw_aspnet_Users.UserName, Custon_UserInfo.UserId AS Expr1, Custon_UserInfo.FirstName, Custon_UserInfo.LastName, Custon_UserInfo.Location, Custon_UserInfo.Avatar, Custon_MoneyWork.UserId AS Expr2, Custon_MoneyWork.Money, vw_aspnet_Users.LastActivityDate FROM vw_aspnet_Users INNER JOIN Custon_UserInfo ON vw_aspnet_Users.UserId = Custon_UserInfo.UserId INNER JOIN Custon_MoneyWork ON vw_aspnet_Users.UserId = Custon_MoneyWork.UserId WHERE (vw_aspnet_Users.UserName LIKE '%' + #UserName + '%')">
<SelectParameters>
<asp:FormParameter FormField="txtSearch" Name="UserName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
In Define Parameters :
UserName =Request.Form("txtSearch") as it needs to be, but it's not working. I tried with session["search"]=txtSearch.text; and its working.. But not with Request.Form..
UPD
I tryed to use QueryStringField instead of FormField and it works, don't know whats wrong with FormField.
Why not avoid Session variables (I don't like the idea of a 'global' variable that sits above everything else, and no way of knowing if it was inadvertently changed or corrupted by bad code, for example), and use ControlParameter instead?
ie
<SelectParameters>
<asp:ControlParameter ControlID="txtSearch" Name="UserName" PropertyName="Text" Type="String" />
</SelectParameters>
Let ASP.net do most of the work for you.

QueryExtender with dropdown list

i am using QueryExtender with dropdownlist to filter gridview ( datasource : EntityDataSource).
<asp:SearchExpression SearchType="StartsWith" DataFields="Status" >
<asp:ControlParameter ControlID="ddlStatus" Type="String" />
</asp:SearchExpression>
Where i bind my ddlStatus from database with default value : "Select"
But when i run project it takes by default value "Select" for Field "status" and gives empty grid.
But on Pageload i want to show all the records after user can select different status from dropdownlist and based on that filter should work.
how can we show all the data with dropdownlist value selected as default "select"
Just Found solution here in book: Entity Framework 4.0 Recipes: A Problem-Solution Approach
Used PropertyExpression instead of SearchExpression
<asp:PropertyExpression>
<asp:ControlParameter ControlID="ddlStatus" Type="String" />
</asp:PropertyExpression>
and leave value blank according to Bala R comment
<asp:ListItem Text="Select" Value="" />
Try using DefaultValue like this
<asp:ListItem Text="Select" Value="Select" />
and
<asp:SearchExpression SearchType="StartsWith" DataFields="Status" >
<asp:ControlParameter ControlID="ddlStatus" Type="String" Default="Select" />
</asp:SearchExpression>

Server Tags in DataSource

Is Possible to put some server tag in Session Field to access a constant string???
<asp:EntityDataSource ID="EntityDataSourceProcesos" runat="server" ConnectionString="name=Entities"
DefaultContainerName="Entities" EntitySetName="PROCESO" EntityTypeFilter="PROCESO"
Select="it.[DE_PROC], it.[ID_PROC], it.[ST_PROC]"
Where="it.[ST_PROC] = 1 and it.[TMEMPR].[CO_EMPR] = #EmpresaID">
<WhereParameters>
<asp:SessionParameter Name="EmpresaID" SessionField='<%= stringPublicVariable %>' Type="String" />
</WhereParameters>
</asp:EntityDataSource>
Instead you can add your parameter at code behind :
EntityDataSourceProcesos.WhereParameters.Add(
new SessionParameter("EmpresaID", TypeCode.String, stringPublicVariable));

Resources