I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
Related
My Table structure
Courses
Id
Description
Subjects
Id
Description
Courses (Relation to Courses table).
My code as follows
td>
<asp:DropDownList ID="ddlCourses" runat="server" DataSourceID="LinqCourses"
DataTextField="Description" DataValueField="Id"/>
<asp:LinqDataSource ID="LinqCourses" runat="server"
ContextTypeName= "DataAccess.SchoolStudyDataContext" Select="new (Description, Id)"
TableName="Courses" />
</td>
<td align="left" valign="top" class="style5" style="width: 124px">
<asp:DropDownList ID="ddlSubjects" runat="server" DataSourceID="LinqSubjects"
DataTextField="Description" DataValueField="Id"/>
<asp:LinqDataSource ID="LinqSubjects" runat="server"
ContextTypeName= "DataAccess.SchoolStudyDataContext" Select="new (Description,Courses,Id)"
TableName="Subjects" Where="Courses == #Id" />
<WhereParameters>
asp:ControlParameter ControlID="ddlCourses" Name="Id"
PropertyName="SelectedValue" Type="Int32" />
</WherePrameters>
<asp:DropDownList ID="ddlCourses" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Description" DataValueField="Id"/>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT [Description],[Courses],[Id] FROM [table_name] WHERE ([Courses ] = #Courses ) ">
<SelectParameters>
<asp:Parameter Name="Courses" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is not a exact answer but this might help i guess
Your Where Property is wrong. Change this (==):
Where="Courses == #Id" />
To this (=):
Where="Courses = #Id" />
Source: MSDN
I have a simple GridView (with an ID of 'GridViewAttribs') which displays some values from a database.
Within my GridViewAttribs I wish to have a (nested?) ListView but for some reason I cannot use the asp:ControlParameter ControlID="GridViewAttribs" PropertyName as no results are returned in my (nested) ListView.
If I do NOT use the ControlParameter and hardcode my SELECT statement for the ListView, everything works as expected.
Here's my code:
<asp:SqlDataSource ID="SqlDataSourceAttribHeadings" runat="server"
ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>"
ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>"
SelectCommand="SELECT equipment_attrib_heading_id, equipment_model_id, equipment_attrib_name FROM equipment_new_attrib_headings WHERE (equipment_model_id = #equipment_model_id) AND (equipment_attrib_heading_deleted = 0) ORDER BY equipment_attrib_name">
<SelectParameters>
<asp:SessionParameter DefaultValue="" Name="equipment_model_id"
SessionField="EquipmentModelID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridViewAttribs" runat="server" AutoGenerateColumns="False"
CellPadding="4" DataKeyNames="equipment_attrib_heading_id"
DataSourceID="SqlDataSourceAttribHeadings" ForeColor="#333333" GridLines="None"
style="margin-right: 0px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="equipment_attrib_heading_id"
HeaderText="equipment_attrib_heading_id" ReadOnly="True"
SortExpression="equipment_attrib_heading_id" />
<asp:BoundField DataField="equipment_model_id" HeaderText="equipment_model_id"
SortExpression="equipment_model_id" />
<asp:BoundField DataField="equipment_attrib_name"
HeaderText="equipment_attrib_name" SortExpression="equipment_attrib_name" />
<asp:TemplateField HeaderText="equipment_attrib_value_details" SortExpression="equipment_attrib_value_details">
<ItemTemplate>
<asp:SqlDataSource ID="SqlDataSourceAttribValues" runat="server"
ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>"
ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>"
SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = #head_id) AND (equipment_id = #equipment_id) AND (equipment_attrib_value_deleted = 0)">
<SelectParameters>
<asp:QueryStringParameter Name="equipment_id" QueryStringField="id"
Type="Int32" />
<asp:ControlParameter ControlID="GridViewAttribs"
Name="head_id" PropertyName="SelectedDataKey.Values[equipment_attrib_heading_id]" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListViewAttribValues" runat="server"
DataSourceID="SqlDataSourceAttribValues">
<EmptyDataTemplate>
<table runat="server" style="">
<tr><td>No data was returned.</td></tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td><asp:Label ID="equipment_attrib_value_detailsLabel" runat="server" Text='<%# Eval("equipment_attrib_value_details") %>' /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The GridView displays all columns fine except the "equipment_attrib_value_details" column where I get "No data was returned."
However, if I take out the asp:ControlParameter which calls upon #head_id and use this hardcoded SELECT statement then I see the correct (albeit hardcoded) values:
SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = 3) AND (equipment_id = #equipment_id) AND (equipment_attrib_value_deleted = 0)">
Therefore, for some reason, it would appear that the GridViewAttribs value of "equipment_attrib_heading_id" is not being passed to / picked up by the nested asp:ControlParameter
On the off chance, I've also tried replacing:
PropertyName="SelectedDataKey.Values[equipment_attrib_heading_id]"
With:
PropertyName="SelectedValue"
But this didn't resolve the issue either, still not data was returned.
If it helps, I'm using Visual Studio 2010 with ASP.NET4 (vb).
What you could try is using a hidden field and use that control for the controlparameter
<asp:TemplateField HeaderText="equipment_attrib_value_details" SortExpression="equipment_attrib_value_details">
<ItemTemplate>
<asp:HiddenField runat="server" ID="heading_id" Value='<%# Eval("equipment_attrib_heading_id") %>' />
<asp:SqlDataSource ID="SqlDataSourceAttribValues" runat="server"
ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>"
ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>"
SelectCommand="SELECT [equipment_attrib_value_details] FROM [equipment_new_attrib_values] WHERE (equipment_attrib_heading_id = #head_id) AND (equipment_id = #equipment_id) AND (equipment_attrib_value_deleted = 0)">
<SelectParameters>
<asp:QueryStringParameter Name="equipment_id" QueryStringField="id" Type="Int32" />
<asp:ControlParameter ControlID="heading_id" Name="head_id" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListViewAttribValues" runat="server"
DataSourceID="SqlDataSourceAttribValues">
<EmptyDataTemplate>
<table id="Table2" runat="server" style="">
<tr><td>No data was returned.</td></tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td><asp:Label ID="equipment_attrib_value_detailsLabel" runat="server" Text='<%# Eval("equipment_attrib_value_details") %>' /></td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table ID="itemPlaceholderContainer" runat="server" border="0" style="">
<tr ID="itemPlaceholder" runat="server">
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</ItemTemplate>
</asp:TemplateField>
This only works (it seems) if the HiddenField is inside the same ItemTemplate. If the DataSource is somewhere outside the TemplateField, in cases where you want to data-populate a control that is say on another column or field of the GridView, then the HiddenField will not work, as it's technically hidden.
The way to overcome this is in code-behind. Here is how I do it...
I have a TemplateField that is referring to #StudentID, which is actually a HiddenField in another TemplateField.
<asp:TemplateField HeaderText="Currently<br/>Tracking<br/>(past weeks)">
<ItemTemplate>
<asp:CheckBoxList ID="listWeeksTracking" runat="server" DataSourceID="sdsTETpastWeeks" DataTextField="WeekNo" DataValueField="WeekNo"
OnDataBound="listWeeksTracking_DataBound" OnDataBinding="listWeeksTracking_DataBinding"></asp:CheckBoxList>
<br />
<asp:SqlDataSource ID="sdsTETpastWeeks" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="SELECT tb.WeekNo, ISNULL((SELECT TOP 1 ts.ETRTWtracking FROM tblTETMeetingStudent AS ts INNER JOIN tblTETMeeting AS tm ON tm.TETmeetingID = ts.TETmeetingID AND tm.WeekNo = tb.WeekNo WHERE ts.StudentID = #StudentID),0) AS Tracking FROM tblTETInstructionalTrainingBlocks AS tb WHERE tb.WeekNo <= #WeekNo AND tb.Active = 1 AND tb.YearNum = YEAR(#TETdate) ORDER BY tb.WeekNo">
<SelectParameters>
<asp:Parameter Name="StudentID" Type="string" />
<asp:ControlParameter ControlID="ddlWeekNo" Name="WeekNo" PropertyName="SelectedValue" Type="int16" />
<asp:ControlParameter ControlID="lblTETdate" Name="TETdate" Type="DateTime" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Wrap="False" />
</asp:TemplateField>
So I define the #StudentID parameter as simple a parameter, and not a ControlParameter, is it won't be able to pick up the hiddenfield elsewhere in my GridView.
In code-behind (C#), I trap the Selecting event via the DataBinding.
And in there I specifically look for the row in relation to the control I've just populated (or about to), and then I look for the HiddenField.
protected void listWeeksTracking_DataBinding(object sender, EventArgs e)
{
//set the parameter for the StudentID here as using hiddenfields does not work directly in asp.net, as they are hidden!
CheckBoxList cbl1 = (CheckBoxList)sender;
GridViewRow gvRow = (GridViewRow)cbl1.NamingContainer;
if (gvRow != null)
{
SqlDataSource sdsTETpastWeeks = (SqlDataSource)gvRow.FindControl("sdsTETpastWeeks");
HiddenField hfStudentID = (HiddenField)gvRow.FindControl("hfStudentID");
if (hfStudentID != null) sdsTETpastWeeks.SelectParameters["StudentID"].DefaultValue = hfStudentID.Value.ToString();
}
}
Voila!
Hi I am new to ASP and was able to create a dropdown list which list all the items from my table (DB2_INSTANCES).
Now I need to update a second table (PROCESS) with the value selected DB2_DSN. The table does not allow Null values.
If I use "SelectedValue='<%# Bind("DB2_DSN") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"'DropDownList6' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "
If I change to "SelectedValue='<%# Bind("INSTANCE") %>' within my asp:DropDownList after I set the
DataSourceID="SqlDataSource6" DataTextField="INSTANCE" DataValueField="INSTANCE"
I get an error:
"DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'INSTANCE'."
If I remove the "SelectedValue..." I get the error:
Cannot insert the value NULL into column 'DB2_DSN', table 'xxx.dbo.PROCESS'; column does not allow nulls. UPDATE fails.
The statement has been terminated.
I also tried using the DataBind() but it also gives me an error.
The other value (FTP_IND) works fine because it is listed/hardcoded.
Why does it work for FTP_IND and not for DB2_DSN?
First:
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
DeleteCommand="DELETE FROM PROCESS WHERE NAME = #NAME AND ENVIRONMENT = #ENVIRONMENT"
SelectCommand="SELECT * FROM [PROCESS]"
UpdateCommand="UPDATE PROCESS SET ENVIRONMENT = #ENVIRONMENT, PROCESS_STATUS = #PROCESS_STATUS, IC_VALUE = #IC_VALUE,
FTP_IND = #FTP_IND, DB2_DSN = #DB2_DSN, DB2_REGION = #DB2_REGION,
ALERT_EMAIL = #ALERT_EMAIL, NOTIFY_EMAIL = #NOTIFY_EMAIL, DEBUG_INFO = #DEBUG_INFO,
LAST_UPDATE_DATE = CURRENT_TIMESTAMP, LAST_UPDATE_USERID = #UID, LAST_UPDATE_IP = #UIP WHERE (NAME = #NAME)">
<UpdateParameters>
<asp:SessionParameter Name="UID" SessionField="User_ID" Type="String" />
<asp:SessionParameter Name="UIP" SessionField="User_IP" Type="String" />
<asp:Parameter Name="ENVIRONMENT" />
<asp:Parameter Name="PROCESS_STATUS" />
<asp:Parameter Name="IC_VALUE" />
<asp:Parameter Name="FTP_IND" />
<asp:Parameter Name="DB2_DSN" />
<asp:Parameter Name="DB2_REGION" />
<asp:Parameter Name="ALERT_EMAIL" />
<asp:Parameter Name="NOTIFY_EMAIL" />
<asp:Parameter Name="DEBUG_INFO" />
<asp:Parameter Name="NAME" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource6" runat="server"
ConnectionString="<%$ ConnectionStrings:xxxConnectionString2 %>"
SelectCommand="SELECT [INSTANCE] FROM [DB2_INSTANCES] ORDER BY [INSTANCE] ASC">
</asp:SqlDataSource>
Second:
<asp:TemplateField HeaderText="FTP" SortExpression="FTP_IND">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList5" runat="server"
SelectedValue='<%# Bind("FTP_IND") %>'
ToolTip="Use this to turn off and on the FTP process" Font-Size="Small">
<asp:ListItem>N</asp:ListItem>
<asp:ListItem>Y</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label11" runat="server" Text='<%# Bind("FTP_IND") %>'
ToolTip="Indicates if FTP will take place" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText = "DB2 Instance" SortExpression="DB2_DSN">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList6" runat="server"
ToolTip="Use this to choose the DB2 Instance" Font-Size="Small"
DataSourceID="SqlDataSource6"
DataTextField="INSTANCE"
DataValueField="INSTANCE">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label12" runat="server" Text='<%# Bind("DB2_DSN") %>'
ToolTip="Indicates the DB2 Instance being used" Font-Size="Small">
</asp:Label>
</ItemTemplate>
<ItemStyle Font-Size="Small" HorizontalAlign="Center" />
There are 3 dropdownlists which are parent-child, after the 3rd dropdownlist is selected, the DetailsView will display result from all 3 dropdownlist selected. first is ServerName, second is Instance, then third is DatabaseName. After select first dropdownlist of servername, then new list of instance value appear on second dropdownlist. When I select anything on second dropdownlist. There a error message that say, "Must declare the scalar variable "#ServerName"". I don't understand what it mean and please help. Here a dropdownlist codes,
<ajaxToolkit:ComboBox ID="ComboBox1" runat="server" AutoCompleteMode="SuggestAppend" AutoPostBack="True" DataSourceID="SqlDataSource4" DataTextField="ServerName" DataValueField="ServerName" DropDownStyle="Simple" MaxLength="0" style="display: inline;">
</ajaxToolkit:ComboBox>
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [ServerName] FROM [tblServer]">
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
Then I pass those dropdownlist function to DetailsView that come with SQLDataSource2,
on SelectCommand in SQLDataSource2, I wrote
SelectCommand="SELECT * FROM [tblDatabase] WHERE (([DatabaseName] = #DatabaseName) AND ([Instance] = #Instance) AND ([ServerName] = #ServerName))"
Then after that I add SelectParameter codes,
<SelectParameters>
<asp:ControlParameter ControlID="DropDownDatabase" Name="DatabaseName" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" Type="String" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
In SqlDataSource3, your query takes two parameters: #ServerName and #Instance. But in the select parameters, you only define #Instance. Yes, you defined #ServerName in the previous data source, but not in this one.
I fixed from what Jay metioned recently and there also another issue that I fix it on instance and database dropdownlist codes, where what I fix and it work fine.
<asp:Label ID="Label11" runat="server" Text="Select Instance:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownInstance" runat="server" AutoPostBack="True" DataSourceID="Instance" DataTextField="Instance" DataValueField="Instance">
</asp:DropDownList>
<asp:SqlDataSource ID="Instance" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [Instance] FROM [tblDatabase] WHERE [ServerName] = #ServerName">
<SelectParameters>
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Label ID="Label10" runat="server" Text="Select Database:"></asp:Label>
<br />
<asp:DropDownList ID="DropDownDatabase" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource3" DataTextField="DatabaseName" DataValueField="DatabaseName">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:Database_Shared_NotebookConnectionString %>" SelectCommand="SELECT [DatabaseName] FROM [tblDatabase] WHERE [ServerName] = #ServerName AND [Instance] = #Instance">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownInstance" Name="Instance" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="ComboBox1" Name="ServerName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
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;
}
}