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:)
Related
I have an ASP.NET GridView supposed to exec a SQL Query.
The query is never executed so I strongly suspect there is a binding issue somewhere.
Here is the aspx code:
<asp:SqlDataSource ID="i_sdsGv" runat="server" ConnectionString="<%$ ConnectionStrings:XXX %>"
SelectCommand="INCOHERENT QUERY IN ORDER TO TRIGGER AN EXCEPTION"
SelectCommandType="Text"
<SelectParameters>
<asp:QueryStringParameter Name="MyTableId" QueryStringField="MyTableId" />
</SelectParameters>
</asp:SqlDataSource>
...
<asp:GridView ID="i_gv" runat="server" AutoGenerateColumns="False" DataKeyNames="MyTableId"
DataSourceID="i_sdsGv" OnRowDataBound="i_gvOptionChoix_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="i_tbName" runat="server" Text='<%# Bind("MyTableName") %>' />
</ItemTemplate>
</Columns>
<EmptyDataTemplate>
NO ROWS TO DISPLAY
</EmptyDataTemplate>
</asp:GridView>
I am 100% sure that if the query string is inexact, an exception is triggered.
In my case, no exception is triggered so the query is not executed.
I get the NO ROWS TO DISPLAY text.
The only obvious binding that I can think about is between the DataSource and the GridView. It does look OK to me.
So I would suspect an issue with :
DataKeyNames="MyTableId" ?
My i_gvOptionChoix_RowDataBound method is empty at the moment but I think it does not matter at this time?
What am I missing so the app actually attempts to execute the query?
Thx in advance for your kind assistance
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.
I have a WebForms GridView, where I want to update a BoundField conditionally (or I use a value from a DropDown or I use the text from a Textbox), based on the value from another DropDownList.
<asp:GridView ID="gv_results" runat="server" DataKeyNames="ID_RESULTS" DataSourceID="get_results">
<Columns>
<asp:TemplateField HeaderText="DETAIL" SortExpression="DETAIL">
<EditItemTemplate>
<asp:TextBox ID="tb_detail_edit" runat="server"></asp:TextBox>
<asp:DropDownList ID="dd_detail_edit" runat="server" AutoPostBack="True" DataSourceID="get_detail" DataTextField="DETAIL" DataValueField="DETAIL" ></asp:DropDownList>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:SqlDataSource runat="server" ID="get_results"
UpdateCommand="UPDATE RESULTS SET [DETAIL] = COALESCE(#DETAIL,#DETAILwelcome)">
<UpdateParameters>
<asp:ControlParameter ControlID="ctl00$MainContent$gv_results$ctl02$dd_detail_edit" PropertyName="SelectedValue" Name="DETAIL" Type="String"></asp:ControlParameter>
<asp:ControlParameter ControlID="ctl00$MainContent$gv_results$ctl02$tb_detail_edit" PropertyName="Text" Name="DETAILwelcome" Type="String"></asp:ControlParameter>
</UpdateParameters>
</asp:SqlDataSource>
Well, I found out in the google's that I have to use the Direct ID in the Control Parameter. And it works...the thing is , the $ctl02 (for example) before the id of the dd_detail_edit, changes, with different rows in the Gridview (so this id works for one row, but maybe for the next it doesn't work.
Is there some workaround for this?
Using only something like $gv_results$dd_detail_edit , doesn't work, I dunno why :S
TIA and Best Regards,
For one thing it looks like you have an open <ItemTemplate> tag without any closing one.
If you check out the .net documentation you can see where they use the regular control IDs, like ControlID = dd_detail_edit.
I do not think you're providing enough details for me to provide a complete answer, but those are a couple of things I noticed.
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.
I'm hoping to retrieve data-rows from a database using a query with a checkboxlist as a control parameter. The problem is multiple selected values on the checkboxlist only return one value as seen here...
Illustration
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" DataSourceID="SqlDataSource1" DataTextField="Fruit" DataValueField="Fruit" ></asp:CheckBoxList>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:f-StopConnectionString %>' SelectCommand="SELECT [Fruit] FROM [Table_1]"></asp:SqlDataSource>
<br />
<asp:Button ID="Button1" runat="server" Text="Select" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource4">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"></asp:BoundField>
<asp:BoundField DataField="Fruit" HeaderText="Fruit" SortExpression="Fruit"></asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource runat="server" ID="SqlDataSource4" ConnectionString='<%$ ConnectionStrings:f-StopConnectionString %>'
SelectCommand="SELECT * FROM [Table_1] WHERE ([Fruit] LIKE '%' + #Fruit + '%')">
<SelectParameters>
<asp:ControlParameter ControlID="CheckBoxList1" PropertyName="SelectedValue" Name="Fruit" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
Is there a way to pass multiple selections so that the query would read:
(WHERE [Fruit] LIKE '%'Apples'%') OR (WHERE [Fruit] LIKE '%'Oranges'%')
Any help at all would be greatly appreciated.
As far as I understand, as you have provided no code, you want to use linq (or entity framework) to query database.
You need then to build lambda expression depending on user selection and pass it to your query.
See Lambda expressions trees here for start:
http://www.codeproject.com/Articles/17575/Lambda-Expressions-and-Expression-Trees-An-Introdu
This can help also:
Can I use custom delegate method in Where method of Entity Framework?
If you want just to pass sql select command string, then you should build custom where clause depending on user selection.