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

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

Related

SqlDataSource with DropDownList Control Parameter doesn't read value

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.

Dynamic query in ASP.Net (WHERE clause in SQLDataSource)

I'm currently trying to loop through an array (two values) and use both values in a query inside the loop. Please see code below. Right now my code doesn't work. I'm trying to populate dynamically the "appType" parameter within the "SelectParameters" tags of the SQLDataSource, but this won't work.
Any suggestion?
<%
Dim appTypes() As String = {"Extranet", "Internet"}
For Each appType As String In appTypes
%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ProviderName = "<%$ ConnectionStrings:CMS.ProviderName %>"
SelectCommand = "SELECT Applications.Name as AppName, Applications.Abbr as AppAbbr, Types.Name as TypeName, Managers.LastName as LastName, Managers.FirstName As FirstName, Managers.EDKeyEmpID as EDKeyID
FROM Types INNER JOIN (Managers INNER JOIN Applications ON Managers.ID=Applications.Manager) ON Types.ID=Applications.Type
WHERE (Types.Name = #appType)
ORDER BY Types.Name, Applications.Name;"
ConnectionString="<%$ ConnectionStrings:CMS %>">
<SelectParameters>
<asp:Parameter DefaultValue="<%=appType%>" Name="appType" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" DataSourceID="SqlDataSource1" runat="server">
<ItemTemplate>
<%#Eval("AppName")%> (<%=appType%>)
</ItemTemplate>
</asp:Repeater>
<% Next %>
Modify your SqlDataSource to have an OnSelecting member:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnSelecting="OnSelecting"
Reset your SelectParameters back to normal:
<SelectParameters>
<asp:Parameter Name="appType" Type="String" />
</SelectParameters>
Define this method in your code-behind. Implement the logic as required. Here I've shown a dummy value from your Repeater. It'll be up to you to determine how/where that actually comes from (SelectedItem or something similar).
Protected Sub OnSelecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
e.Command.Parameters("#appType").Value = Repeater1.SomeValue
End Sub

Accessing query string variable in the sql query in aspx page

I am quite new to asp.net,I am building an application where I need to show the in the grid view,Now the query I am generating for fetching the data from database containing one parameter which comes from the query string.I am using this code
<asp:SqlDataSource runat="server" ID="MySQLData"
ConnectionString="server=localhost;port=3309; User Id=xxxxx;password=xxxxx;database=xxxxx"
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT contenthead.lastmodifieddate,contenthead.heading,lk_technology.technology FROM contenthead JOIN lk_technology WHERE contenthead.techID=lk_technology.techID AND contenthead.authorid='<%=Request.QueryString["uid"]%>'" />
Now when I am using <%..%> tag I am getting parser error that says: Server tags cannot contain <% ... %> constructs.
Now I want to use this variable from query string.Please tell me how I can access this variable in this context.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..." ProviderName="System.Data.SqlClient" SelectCommand="SELECT [UserId], [LastUpdatedDate] FROM [vw_aspnet_Profiles] WHERE ([UserId] = #UserId)">
<SelectParameters>
<asp:QueryString ParameterDefaultValue="0" Name="UserId" QueryStringField="Id" Type="Object"/>
</SelectParameters>
</asp:SqlDataSource>
You have to add a parameter to your SqlDataSource

execute stored procedure using sqldatasource and get return value in vb.net

How can I execute a stored procedure using sqldatasource and get the return value in vb.net.
Thanks,
Terri
The method you are looking for is DataBind. Call it using mySqlDataSource.DataBind()
<asp:SqlDataSource
ID="sds2"
runat="server"
ConnectionString="..."
SelectCommand="spTest"
SelectCommandType="StoredProcedure"
>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text"
Name="ParamName" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="sds2"></asp:GridView>
The stored procedure is executed when you call DataBind. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.
You need to use a SqlConnection with a SqlCommand, like this:
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedureName", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("SomeParam", someValue);
object result = command.ExecuteScalar();
}
If you already have the SP returning a value then you have to grab the value in the corresponding event for the data source. AKA - Inserted, Selected, etc...
Here's a couple links illustrating the point.
http://fredrik.nsquared2.com/viewpost.aspx?PostID=162
http://www.velocityreviews.com/forums/t86158-re-how-to-retrieve-an-output-parameter-using-sqldatasource-control.html
<asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
GetProfile is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox

Pull one record from the database and bind to an HTML tag

In ASP.NET, what is the definitive way to pull one record from the database and bind it and HTML tag? Brevity and style points count.
For brevity, you'll want to use the SqlDataSource.
<asp:SqlDataSource ID="sql" runat="server" ConnectionString='<%$ ConnectionStrings:MyConnectionString %>'
SelectCommandType="Text"
SelectCommand="select MyField FROM MyTable WHERE ID = #id"
>
<SelectParameters>
<asp:ControlParameter ControlID="txtUserName" PropertyName="Text" Name="id" />
</SelectParameters>
</asp:SqlDataSource>
<asp:BulletedList runat="server" DataTextField="MyField" DataSourceID="sql">
</asp:BulletedList>
It's not clear what you want but, detailsview control is designed for displaying only one record from data source.
DetailsView is a data-bound user
interface control that renders a
single record at a time from its
associated data source, optionally
providing paging buttons to navigate
between records.

Resources