ASPx Display data in textbox from SQL source - asp.net

<asp:SqlDataSource ID="textdata" runat="server"
ConnectionString="<%$ ConnectionStrings:TextConnectionString %>"
SelectCommand="SELECT SUM(pkNot) FROM [Not]">
<SelectParameters>
<asp:ControlParameter ControlID="notTotal" Name="pkNot" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
And the textbox is:
<asp:TextBox runat="server" ID="notTotal"></asp:TextBox>
How can I get that value to show up in the textbox? It's not working.

What about something alike this :
DataView beh = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
TextBox1.Text = beh[0][0].ToString();
A other way might be :
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind ('activitydate') %>'></asp:TextBox>
In the code behind. I wrote this in c# as you did not point out what you are using.

Related

Data not being populated in dropdown using sqlDataSource

I need to populate the data from database in Dropdown using sqlDataSource. The SqlDataSource is using a querystring. The data is not being populated in dropdown. Can you please suggest what I am doing wrong here?
Code for Dropdown:
<ajaxToolkit:ComboBox ID="SelectDropDown1" runat="server" DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend" AppendDataBoundItems="true" Width="200px" Height="16pt"
Font-Size="8pt" DataSourceID="SqlDataSource1" DataTextField="Rubric"
DataValueField="Rubric">
<asp:ListItem Value="all">All</asp:ListItem>
</ajaxToolkit:ComboBox>
Code for SqlDataSource:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Education_Data %>"
SelectCommand="SELECT DISTINCT [Rubric] FROM [table1] WHERE ([Program] = #Program)">
<SelectParameters>
<asp:QueryStringParameter Name="Program" QueryStringField="Program"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
The #program needs to be set somehow.
This MSDN article will show you how to do it. Go to section "Passing Parameters to SQL Statements"

How to access a value which is associated with a dropdownlist item?

This is my dropdown list code........
<td valign="top" align="center">
<asp:DropDownList ID="StudentNameDropDownList" runat="server" Width="150px"
DataSourceID="SqlDataSource2" DataTextField="StudentName"
DataValueField="StudentName" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:dbbilling2.0ConnectionString3 %>"
SelectCommand="SELECT [StudentID], [StudentName] FROM [tblStudentInfo] WHERE ([Class] = #Class)">
<SelectParameters>
<asp:ControlParameter ControlID="ClassDropDownList" Name="Class"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<br />
Now I want to access the Student ID field in my code behind file..How can i achieve this?? What syntax i must use[like dropdownlist.selecteditem] ??
First of all in your DropDownList asp component you must set the property DataValueField="StudentID", then in your code behind you can get the Id of the selected student by writing : StudentNameDropDownList.SelectedValue
For Id You can try this code
StudentNameDropDownList.SelectedValue

Dropdown List appears empty when using #parameter

I am trying to make a dropdownlist work for me. A user can be in many 'Field1's, so I wanted the dropdown to show those Field1s.
Currently what happens is I just get a blank list. Here is a sample of the code:
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT DISTINCT Field1 FROM table
WHERE (Field3= #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
If I remove the select parameters and the WHERE clause the list of all DHBs appears - it just seems to have issues with #Field3.
Edit: I have tested the query in SQL Server and it works for me (by replacing #Field3 with a known value)
Any suggestions? I am very new to asp.net, and have next to nothing in the .cs file (besides some authorisation and such), so if people could point me in the right direction that would be fantastic.
Edit 2: It would probably help if I gave you the whole template:
<asp:TemplateField HeaderText="Field1" SortExpression="Field2">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Field2") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Field1" DataValueField="Field1">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="Field1 FROM table
WHERE (Field3 = #Field3)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="Field3" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
You're populating another control using Dropdownlist selectedValue as a parameter, that is dictated by your code. In this case
You're missing property in the controlParameter
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Property="SelectedValue" Name="Field3" Type="String" />
</SelectParameters>
If you're trying to populate DropDownList1 you're completely derailed. In this case you need to show up more markup code and details.
Update: You cannot take the parameters value from the DropDownList control which you're populating. Either remove the parameter or use another control to provide the value

How do I get the "#" value of a DropDownList when updating a DataGrid

I have a standard DataGrid that looks like this:
<asp:GridView id="MyGridView"
DataSourceID="MyDataSource1"
AutoGenerateColumns="false"
AutoGenerateEditButton="true"
DataKeyNames="Id"
Runat="Server">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="State">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("State") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="IdState1" DataSource='<%# GetCategoryNames() %>' DataTextField="State" DataValueField="State" runat="server"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
When updating the grid I have an UpdateCommand that looks like this:
UpdateCommand= "UPDATE [MauriceBlackburnOffices] SET [Name] = #Name, [Address1] = #Address1, [TheStates] = #State WHERE [Id] = #Id"
However the #State field is not recognized.
Must declare the scalar variable "#State".
What should the # value be?
How can I get the DropDownList value into the update statement?
I'm going to guess that you're using a SQLDataSource object to populate the GridView. If so, you're probably haven't set the UpdateParameters (MSDN Link).
Update your SQLDataSource as follows (you'll need to modify it slightly for your code):
<asp:SqlDataSource
id="MyDataSource1"
runat="server"
ConnectionString="MyDataConnectionString"
SelectCommand="SELECT * FROM Table"
UpdateCommand="UPDATE [MauriceBlackburnOffices] SET [Name] = #Name, [Address1] = #Address1, [TheStates] = #State WHERE [Id] = #Id">
<UpdateParameters>
<asp:ControlParameter Name="Name" ControlId="NameControl" PropertyName="Text"/>
<asp:ControlParameter Name="AddressID" ControlId="AddressControl" PropertyName="SelectedValue"/>
<asp:ControlParameter Name="Name" ControlId="StateControl" PropertyName="SelectedValue"/>
<asp:ControlParameter Name="ID" ControlId="IDControl" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
You can actually do this without the UpdateParameters
By carefully looking at the full source here http://msdn.microsoft.com/en-us/library/ms972948.aspx I found the issue.
Its real wierd.
Where I say:
[TheStates] = #State
"State" MUST be the SelectedValue value in EditItemTemplate and it must be attached by the "Bind" method.
Bind("State") creates #State.
Try this one
<asp:DropDownList ID="IdState1" runat="server" DataTextField="State" DataValueField="State" SelectedValue='<%# Bind("State") %>'></asp:DropDownList>

How to bind dropdownlist in EditItemTemplate in FormView control?

Using Visual Web Developer Express 2010 with ASP.NET 4.0.
I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:
<asp:FormView
ID="frmOrderDetails"
DataSourceID="sdsFormOrderDetails"
runat="server"
DataKeyNames="orderId">
<EditItemTemplate>
<h3>Edit Order Details</h3>
<asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server" />
<asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
ID="ddlStrategies"
runat="server"
DataTextField="strategy"
DataValueField="strategyId"
DataSourceID="sdsStrategies"
/>
<asp:LinkButton
id="lnkUpdate"
Text="Update Order"
CommandName="Update"
Runat="server" />
|
<asp:LinkButton
id="lnkCancel"
Text="Cancel"
CommandName="Cancel"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsStrategies" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetStrategiesDropDown">
</asp:SqlDataSource>
The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.
You need to do the following to bind a DropDownList to the EditItemTemplate:
Add the DropDownList and its DataSource to the EditItemTemplate.
Set the DropDownList parameters:
DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
DataTextField="ValueToDisplay" DataValueField="ValueToBind"
Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />
you need to use formview Databound event like
protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
{
DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
ddlStrategies.SelectedValue = Your DB Value Goes here;
}
}

Resources