SqlDataSource with DropDownList Control Parameter doesn't read value - asp.net

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.

Related

How to filter using a dropdown in a gridview and display all values when none is selected

I have a web page with 3 values (1 text box, and 2 drop-downs) and displays result in a gridview. This is working great when the value that I want to search is in the drop-down. However, if the user didn't select one of the dropdownlist ( facilityCode), we would like to display all facility codes versus only one. What is the easiest way to accomplish this? Below is the code related to the sqldatasource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:HCSCRMConnectionString %>"
SelectCommand="SELECT MMM_Id_Nbr, Item_Desc, Supplier_Nbr, Supplier_Name, Stocking_Facility_Code, Reorder_Point_Qty, BodID, Active_Ind FROM BOD_ROP_TBL WHERE (MMM_Id_Nbr = #MMM_Id_Nbr)and active_Ind=#Active_Ind and Stocking_Facility_Code=#FacilityCode"
UpdateCommand="UPDATE BOD_ROP_TBL SET Reorder_Point_Qty = #Reorder_Point_Qty, Active_Ind = #Active_Ind WHERE (BodID = #BodID)">
<SelectParameters>
<asp:FormParameter FormField="txt3MID" Name="MMM_Id_Nbr" Type="String" />
<asp:FormParameter FormField="dropActive" Name="Active_Ind" Type="String" />
<asp:FormParameter FormField="FacilityCode" Name="FacilityCode" Type="String" />
</SelectParameters>

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" />

Control doesn't bind to SqlDataSource on page load with blank parameters

Suppose you have a SQLDataSource that looks like this:
<asp:SqlDataSource ID="sqldsSample" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [col1], [col2] FROM [tbl] WHERE [col3] = #val) ORDER BY [col1] DESC;">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="val" Type="Boolean" />
<asp:Parameter DefaultValue="" Name="val2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Just assume you have decided you want to declare the 'val2' parameter here and you don't want to bother with adding and/or removing parameters later in the code-behind (say, to change the SelectCommand to do some filtering with some extra criteria).
It will fail without an error - the control will just show up empty.
You have to specify a default value for the parameter even if it's not used. For example, just putting in a space character will work:
<asp:Parameter DefaultValue=" " Name="val2" Type="String" />
Note that it still won't work if you omit the DefaultValue property.
There is also the CancelSelectOnNullParameter parameter on SqlDataSource - you need to set this to false if you expect any of your parameters to be null.
It's not very obvious and it has caught me out a few times!

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.

Change the property "DefaultValue" of the asp.net "ControlParameter" control with javascript

I already have an idea on how to do this, but I realized that the control "ControlParameter" did not have an "Id" property (which is needed for the JS). Is there a different way to use JavaScript to change the "DefaultValue" property without the need of using the "Id" property?
Here is the JavaScript and asp.net code that I have been working with:
JavaScript:
function ChangePropertyValue(propertyName, newpropertyValue) {
var ControlParameter = document.getElementById(propertyName)
ControlParameter.DefaultValue = newpropertyValue
}
asp.net:
<asp:Button ID="btntest" runat="server" Text="try" OnClick="ChangePropertyValue(??, 17)"/>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString1 %>"
SelectCommand="SELECT [Id], [ContentTitle], [Content] FROM [Table1] WHERE ([Id] = #Id)">
<SelectParameters>
<asp:ControlParameter ControlID="ListView1" DefaultValue="16" Name="Id"
PropertyName="SelectedValue" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
You can't access a ControlParameter client side. ControlParameters are used to bind the value of a Control property server side, they are not rendered to the client. You can, however, set the Default value of the ControlParameter programmatically in your Code Behind.
SqlDataSource1.SelectParameters["id"].DefaultValue = "value";

Resources