ASP.NET FormView fails to execute a stored procedure - c#

I encounter difficulties to update an item into an ASP.NET GridView (old code).
My stored procedure can be simplified to:
UPDATE myTable SET FieldA = #FieldA, FieldB = #FieldB, FieldC = #FieldC WHERE Id = #Id
My DataSource is a s follows:
<asp:SqlDataSource ID="DS" runat="server" ConnectionString="<%$ ConnectionStrings:wsg_DataWriter %>"
SelectCommand="MySelectSP" SelectCommandType="StoredProcedure"
UpdateCommand="MyUpdateSP" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter Name="Id" QueryStringField="Id" />
</SelectParameters>
<UpdateParameters>
<asp:QueryStringParameter Name="idWsgProgrammeOption" QueryStringField="idWsgProgrammeOption" />
</UpdateParameters>
</asp:SqlDataSource>
My FormView is as follows :
<asp:FormView ID="FV" runat="server" DataKeyNames="Id" DataSourceID="DS" >
<EditItemTemplate>
<asp:TextBox ID="TB_FieldA" runat="server" Text='<%# Bind("FieldA") %>' />
<asp:ImageButton ID="IB_FieldB" runat="server" CommandName="UpdateProgramme" CommandArgument='<%# Eval("Id") %>'
ImageUrl='<%# (!Convert.ToBoolean(Eval("FieldB"))) ? "~/1.gif" : "~/2.gif" %>' OnCommand="On_FieldB"/>
<asp:TextBox ID="TB_FieldC" T runat="server" Text='<%# Bind("FieldC") %>' />
<asp:ImageButton ID="i_imgbtnEnregistrer" runat="server" AlternateText="Enregistrer"
CommandName="Update" ImageUrl="~/App_Themes/admindefaut/image/bouton/enregitrer.gif" OnClick="OnClick"/>
</EditItemTemplate>
</asp:FormView >
It does not work: I suppose the stored procedure fails to execute.
I am very close of having it working. The issue seems to come from the binding of FieldB into the SP.
It works fine if I change it this way:
<UpdateParameters>
<asp:QueryStringParameter Name="idWsgProgrammeOption" QueryStringField="idWsgProgrammeOption" />
</UpdateParameters>
-->
<UpdateParameters>
<asp:QueryStringParameter Name="idWsgProgrammeOption" QueryStringField="idWsgProgrammeOption" />
<asp:Parameter Name="FieldB" />
</UpdateParameters>
And add:
protected void OnClick(object sender, EventArgs e)
{
FV.UpdateParameters["FieldB"].DefaultValue= "1";
}
So somehow, .NET manages to get correct values of FieldA and FieldB and pass them to the SP. But it does not manage to grab the correct FieldB value.
Can anyone give me a hand there? I would need to pass the correct FieldB value value without using the code behind.

Try binding FieldB to a hidden field on your aspx page using the Bind keyword, not Eval.

Related

Refresh content of DropDownList fetching data from same DataSource

I'm trying to update the contents of one DropDownList based on the value of 3 other DropDownLists. I have 2 tables, one containing Projects and one containing SubProjects. These 2 have 3 matching Properties:
Year
CountryID
OEID
I want to assign SubProjects to Projects using these matching criteria. Thus my SubProject Adapter has a method that wetches a view containing this data. In the view I try to use following code:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:DetailsView ID="DetailsViewProject" runat="server" AutoGenerateRows="False" DataKeyNames="Guid" DataSourceID="ObjectDataSourceProject" DefaultMode="Insert" Height="50px" Width="125px" OnItemInserted="DetailsViewProject_ItemInserted">
<Fields>
<asp:TemplateField HeaderText="Name">
<InsertItemTemplate>
<asp:TextBox ID="TextBoxName" Width="245px" runat="server" Text='<%# Bind("Name") %>' MaxLength="100" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorName" ControlToValidate="TextBoxName" runat="server" ErrorMessage="Please specifiy a Name">*</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtenderName" runat="server" TargetControlID="RequiredFieldValidatorName" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<InsertItemTemplate>
<asp:DropDownList ID="lstCountries" Width="250px" runat="server" DataSourceID="ObjectDataSourceCountries" DataTextField="Name" DataValueField="ID" SelectedValue='<%#Bind("CountryID") %>' AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Organisation Unit">
<InsertItemTemplate>
<asp:DropDownList ID="lstOEs" Width="250px" runat="server" DataSourceID="ObjectDataSourceOEs" DataTextField="Name" DataValueField="ID" SelectedValue='<%#Bind("OrganisationUnitID") %>' OnDataBinding="lstOEs_DataBound" AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Year">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectYears" Width="250px" runat="server" DataSourceID="SqlDataSourceProjectYears" DataTextField="ProjectYear" DataValueField="ProjectYear" SelectedValue='<%#Bind("ProjectYear") %>' OnDataBinding="lstProjectYears_DataBound" AutoPostBack="True"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectOverall" Width="250px" runat="server" DataTextField="ProjectName" DataSourceID="ObjectDataSourcePOs" DataValueField="ID" SelectedValue='<%#Bind("ProjectOverallID")%>'></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowCancelButton="False" ShowInsertButton="True" InsertText="Insert" />
</Fields>
</asp:DetailsView>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<asp:ObjectDataSource ID="ObjectDataSourceProject" runat="server" ...and so on
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourceOEs" runat="server" ...and so on
</asp:ObjectDataSource>
<-- here -->
<asp:ObjectDataSource ID="ObjectDataSourcePOs" runat="server"
SelectMethod="GetProjectsOverallByParameters" TypeName="Projects">
<SelectParameters>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectYears" name="ProjectYear" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstOEs" name="OrganisationUnitID" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstCountries" name="CountryID" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourceCountries" runat="server" OldValuesParameterFormatString="{0}" SelectMethod="GetCountries" TypeName="Countries"></asp:ObjectDataSource>
<asp:SqlDataSource ID="SqlDataSourceProjectYears" runat="server" ...and so on
</asp:SqlDataSource>
</asp:Content>
But I'm getting this error:
System.InvalidOperationException: 'Databinding methods such as Eval(),
XPath(), and Bind() can only be used in the context of a databound
control.'
Why am I getting this error and how can I solve this? How can I refetch the data from the adapter using either code behind or the aspnet update mechanism?
you have to use property AppendDataBoundItems="True|False" when you have to put existing Item as it is.
I solved the issue:
The problem was, that Project scheme and ProjectOverall scheme didn't match.
I changed the adapter and added an access method for ProjectOverall.
SELECT ID, CountryID, OrganisationUnitID, ProjectYear, ProjectName
FROM wc_ProjectsOverall
WHERE (CountryID = #CountryID) AND (OrganisationUnitID = #OrganisationUnitID) AND (ProjectYear = #ProjectYear)
In ProjectOverall.cs:
[System.ComponentModel.DataObjectMethodAttribute
(System.ComponentModel.DataObjectMethodType.Select, false)]
public WebControlling.ProjectsOverallDataTable GetProjectsOverallByAttributes(Guid CountryID, Guid OrganisationUnitID, Int32 ProjectYear)
{
if (CountryID == null || OrganisationUnitID == null || ProjectYear == 0)
return new WebControlling.ProjectsOverallDataTable();
return Adapter.GetProjectsByAttribute(CountryID, OrganisationUnitID, ProjectYear);
}
In the view ProjectAdd.aspx:
<asp:TemplateField HeaderText="Project">
<InsertItemTemplate>
<asp:DropDownList ID="lstProjectOverall" Width="250px" runat="server" DataTextField="ProjectName" DataSourceID="ObjectDataSourcePOs" DataValueField="ID" AppendDataBoundItems="False"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:ObjectDataSource ID="ObjectDataSourceProject" runat="server" InsertMethod="AddProject" OldValuesParameterFormatString="{0}" SelectMethod="GetProjects" TypeName="Projects" OnInserting="ObjectDataSourceProject_Inserting" >
<InsertParameters>
<asp:Parameter Name="CountryID" Type="Object" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="Objective" Type="String" />
<asp:Parameter Name="ObjectiveQuantitativ" Type="String"/>
<asp:Parameter Name="ProjectYear" Type="Int32" />
<asp:Parameter Name="BaseCampaign" Type="Boolean" />
<asp:Parameter Name="ManagerID" Type="Object" />
<asp:Parameter Name="OrganisationUnitID" Type="Object" />
<%-- This parameter is different because the ProjectOverall ID is not part of the Project model, thus we insert the DropDownList value --%>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectOverall" name="ID" PropertyName="SelectedValue"/>
</InsertParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSourcePOs" runat="server"
SelectMethod="GetProjectsOverallByAttributes" TypeName="ProjectsOverall">
<SelectParameters>
<asp:ControlParameter ControlID="DetailsViewProject$lstProjectYears" name="ProjectYear" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstOEs" name="OrganisationUnitID" PropertyName="SelectedValue"/>
<asp:ControlParameter ControlID="DetailsViewProject$lstCountries" name="CountryID" PropertyName="SelectedValue"/>
</SelectParameters>
</asp:ObjectDataSource>
And that solved my issue.

Procedure or function has too many arguments specified in FormView and SQL SERVER c#

I have one stored procedure called VideoGalleryDelete . I am using FormView and Gridview to insert, update, show and delete data. VideoGallery is my primary table with PK VideoId and it has a foreign key as CategoryId .
When i click Delete linkButton, its says Procedure or function has too many arguments specified
This is my SP :
ALTER PROCEDURE [dbo].[VideoGalleryDelete]
#VideoId int
AS
BEGIN
DELETE FROM [dbo].[VideoGallery]
WHERE VideoId = #VideoId
END
GridView and its assigned SqlDataSource :
<asp:GridView ID="gvVideo" CssClass="table" runat="server" OnSelectedIndexChanged="gvVideo_SelectedIndexChanged" AutoGenerateColumns="False" DataKeyNames="VideoId,CategoryId" DataSourceID="sdVideoList">
<Columns>
<asp:BoundField DataField="VideoId" Visible="false" HeaderText="VideoId" InsertVisible="False" ReadOnly="True" SortExpression="VideoId" />
<asp:BoundField DataField="VideoLink" HeaderText="Video Link" SortExpression="VideoLink" />
<asp:BoundField DataField="CategoryId" HeaderText="Category Id" SortExpression="CategoryId" />
<asp:TemplateField ItemStyle-CssClass="Center" HeaderStyle-Width="15%">
<ItemTemplate>
<asp:ImageButton ImageUrl="~/images/edit.png" ID="lnkedit" Style="margin-left: 15px" CommandName="Select" runat="server" Width="20px"></asp:ImageButton>
<asp:ImageButton ImageUrl="~/images/delete.png" ID="lnkDelete" CommandName="Delete" Style="margin-left: 15px" runat="server" Width="20px" OnClientClick="return ConfirmDelete();"></asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="sdVideoList" runat="server" ConnectionString="<%$ ConnectionStrings:WebAAERT_DBConnectionString %>" DeleteCommand="VideoGalleryDelete" DeleteCommandType="StoredProcedure" SelectCommand="VideoGallerySelect" SelectCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="VideoId" Type="Int32" />
</DeleteParameters>
<SelectParameters>
<asp:Parameter DefaultValue="0" Name="CategoryId" Type="Int32" />
</SelectParameters>
I think you're misusing the DataKeyNames property of the grid view control.This property should ideally contain only the primary key field(s) to identify the current row.Also every single value set on this property will be passed through to the Delete command.
Your delete stored proc takes one parameter but two values are being passed through beacuase of DataKeyNames="VideoId,CategoryId".So you can do what #Chris Flynn suggested or change your grid view to DataKeyNames="VideoId"

Asp.net update DropDownList selected item

I'm stuck into a problem using a DropDownList item into a form.
The main form is feed by an SQLDataSource ... all form widget are bound to a specific record field etc.
I changed one of the form widget (a TextBox) with a DropDownList to allow the user to only select between a number of values... I feed this control trough another SQLDataSource. While the form is in Insert mode there are no problems but when I switch into Edit mode I'm not able to set the DropDownList selected item with the value coming from the main record. Ho can I fix this?
Here's a sample of my .aspx code:
<asp:FormView ID="_frmData" runat="server" DataKeyNames="id" DataSourceID="_sdsTable1" DefaultMode="Insert">
<InsertItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" CssClass="input_medium" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="_sdsTable1" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field0, field1
FROM
table1
WHERE
id=#id;"
UpdateCommand=" UPDATE
table1
SET
`field0` = #field0
`field1` = #field1
WHERE
id = #id;""
InsertCommandType="StoredProcedure"
InsertCommand="create_table1_entry">
<InsertParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="_sdsTable2" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field_value, field_text
FROM
table2;" />
Try This
myDropDown.Items.FindByValue("Your Main Record Value").Selected=true;
OR
myDropDown.Items.FindByText("Your Main Record Text").Selected=true;
I CAN JUST use SelectedValue='<%#Bind("field1")%>' property in DDL to achieve the result, just as I was guessing. The only issue here is the why Intellisense is not suggesting SelectedValue as a propery for DropDownList ... thnx everyone.

DropDownList in DetailsView-Issue - ddl has a value which is invalid because it doesn't exist

well I've stumbled upon similar cases of this problem, and also followed the solution approaches. But my case is quite strange, as I have 2 different DetailsView-controls (with different data), one works, the other doesn't.
So here's the problem in detail. I get the following error message:
DropDownList2 has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I am aware of this thread, and maybe I'm stupid and not seeing something. But maybe you do. I have 2 Detailsviews, which bind their data based on a dataset for one user. Both DVs have DropdownList-Controls inside their EditItemTemplates, which bind the possible values for this very column. I use SelectedValue='<%# Bind("mycolumn") %>' for my DropDownList-template exactly the same way in the 2 DVs.
As said, I am aware of code-behind workarounds, but I want to avoid those, to keep my code clean and consistent. I can't really document why I use a workaround on one DetailsView, and why I don't on the other.
Here is the code of my 2 DetailsViews:
<asp:DetailsView ID="dv_theme_ava" runat="server" Height="50px" Width="125px" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" DefaultMode="Edit" AutoGenerateEditButton="True" DataKeyNames="Pat_ID">
<Fields>
<asp:TemplateField HeaderText="Theme">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource2"
DataTextField="theme" DataValueField="theme"
SelectedValue='<%# Bind("theme") %>'>
</asp:DropDownList>
<asp:Label ID="lolbel2" runat="server" Text='<%# Bind("theme") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [theme] FROM [gui_themes]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Avatar">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3"
DataTextField="avatar" DataValueField="avatar">
</asp:DropDownList>
<asp:Label ID="lolbel" runat="server" Text='<%# Bind("avatar") %>'></asp:Label>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [avatar] FROM [gui_avatars]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [pat_gui_config] WHERE ([Pat_ID] = #Pat_ID)" DeleteCommand="DELETE FROM [pat_gui_config] WHERE [Pat_ID] = #Pat_ID"
InsertCommand="INSERT INTO [pat_gui_config] ([Pat_ID], [theme], [avatar]) VALUES (#Pat_ID, #theme, #avatar)"
UpdateCommand="UPDATE [pat_gui_config] SET [theme] = #theme, [avatar] = #avatar WHERE [Pat_ID] = #Pat_ID">
<DeleteParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Pat_ID" Type="Int32" />
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:SessionParameter Name="Pat_ID" SessionField="sel_pat_id" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="theme" Type="String" />
<asp:Parameter Name="avatar" Type="String" />
<asp:Parameter Name="Pat_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:DetailsView ID="dv_treat_edit" runat="server" AutoGenerateEditButton="True"
AutoGenerateRows="False" DataKeyNames="Tr_ID" DataSourceID="sql_newcat" DefaultMode="Edit"
Height="50px" Width="90%" AllowPaging="True" CssClass="dv_details" Style="margin: 0 auto;">
<Fields>
<asp:BoundField DataField="Tr_ID" HeaderText="Tr_ID" InsertVisible="False" ReadOnly="True"
SortExpression="Tr_ID" />
<asp:BoundField DataField="description" HeaderText="Description" SortExpression="description" />
<asp:BoundField DataField="syn_ger" HeaderText="Display Name (German)" SortExpression="syn_ger" />
<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
<asp:DropDownList ID="ddl_type0" runat="server" DataSourceID="sql_ddl_type0" DataTextField="type"
DataValueField="type" SelectedValue='<%# Bind("type") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_type0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [type] FROM [entry_type]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Exclusive for Patient_ID">
<EditItemTemplate>
<asp:DropDownList ID="ddl_excl_pat0" runat="server" DataSourceID="sql_ddl_exclpat0"
DataTextField="Pat_ID" DataValueField="Pat_ID" SelectedValue='<%# Bind("custom_cat_for_Pat") %>'
AppendDataBoundItems="true">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="sql_ddl_exclpat0" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT [Pat_ID] FROM [patients]"></asp:SqlDataSource>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<CommandRowStyle CssClass="dv_footer" />
<RowStyle CssClass="dv_tr" />
<PagerSettings Mode="NumericFirstLast" Position="Top" Visible="False" />
</asp:DetailsView>
<asp:SqlDataSource ID="sql_newcat" runat="server" ConnectionString="<%$ ConnectionStrings:interacct_SQL_convConnectionString %>"
SelectCommand="SELECT * FROM [treat_cat]" DeleteCommand="DELETE FROM [treat_cat] WHERE [Tr_ID] = #Tr_ID"
InsertCommand="INSERT INTO [treat_cat] ([description], [syn_ger], [type], [custom_cat_for_Pat]) VALUES (#description, #syn_ger, #type, #custom_cat_for_Pat)"
UpdateCommand="UPDATE [treat_cat] SET [description] = #description, [syn_ger] = #syn_ger, [type] = #type, [custom_cat_for_Pat] = #custom_cat_for_Pat WHERE [Tr_ID] = #Tr_ID">
<DeleteParameters>
<asp:Parameter Name="Tr_ID" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="description" Type="String" />
<asp:Parameter Name="syn_ger" Type="String" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="custom_cat_for_Pat" Type="Int32" />
<asp:Parameter Name="Tr_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
Note: the data model is quite simple. For comparison I used the 2 fields "theme" and "type". Both tables only have 1 column in my database, holding string entries.
Now, the "type"-DDL gets it's items perfectly fine, and has the SelectedValue bound to the value brought to the DetailsView by the datasource. When I bind the "theme"-DDL with SelectedValue, I get the error. Interesting: in the same EditItemTemplate I have set up a Label (ID "lolbel2" :p) to check the databinding. It works (of course, when I remove the SelectedValue from the DDL). So without the SelectedValue in the DDL my output is like
[DROPDOWNLIST] with items "space", "magic"
[LABEL] with text "magic" (since this is the value of my test-user).
Am I missing something? Am I completely nuts?
So, sorry for re-asking this question for like the 10th-time, but I want to UNDERSTAND what my code does.
Thanks in advance!
Konrad
Ooooohkay. Found the problem, that's what you get when you are stubborn as a goat. :)
When debugging a workaround with a HiddenField I noticed that the value, which gets bound the same way as the Label-Control, has some trailing whitespace. In particular: instead of "dog" I got "dog ". While this isn't shown in the asp:Label, I guess this was the reason why the value wasn't found in the DropDownList.
Where did the whitespace come from? In my SQL table I created the columns for "theme" and avatar as "nchar", and not "nvarchar". Apparently when using "nchar" as DataType the not-used chars of the tupels are filled with whitespace, or let's say the fields have fixed width (always x chars).
Changing the datatype to "nvarchar" helped me to get rid of the whitespace, and now the databinding of the DDLs works just fine.
I am documenting this, as maybe someone else will stumble over this aswell - and since there are 50 solutions and workarounds, maybe just taking a look at the database sometimes does the trick.

bind sqldatasource result to textbox c#

I have a SqlDataSource that returns 1 field (1 row) for an ID. I would like to get that result and display it in a TextBox. I would normally do it using a stored procedure (since the stored procedure is already created), but I was thinking SqlDataSource would be easier. Is there a way to bind that result onto my TextBox?
<asp:SqlDataSource ID="ds1" runat="server"
ConnectionString="<%$ ConnectionStrings:conn1%>"
ProviderName="<%$ Connectionstrings:conn1.ProviderName %>"
SelectCommand="sp_1"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="accountID" Type="Int32" />
<asp:Parameter Name="activitydate" Type="DateTime" Direction="Output" />
</SelectParameters>
</asp:SqlDataSource>
You cannot just bind textbox like that, there is no DataSourceID property on textbox.
My suggestion, you may create a DataList using that DataSource and on ItemTemplate, you can do:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind ('activitydate') %>'></asp:TextBox>
It looks like you've already configured your sqldatasource to utilize a stored procedure. In order to bind activitydate to a textbox, please consider using:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind ('activitydate')
%>'></asp:TextBox>
Hope this helps:)

Categories