I have a small example application with articles and comments.
Users can view a specific Article by passing the article ID value in URL:
http://localhost:56079/viewArticle.aspx?id=123456
I want the Article id to be used to find corresponding comments and populate a gridview with them in the same form.
viewArticle.aspx:
<dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" Width="100%" DataSourceID="ObjectDataSource1">
<Columns>
<dx:GridViewDataTextColumn FieldName="field1" Caption="Field #1" VisibleIndex="0" />
<dx:GridViewDataTextColumn FieldName="field2" Caption="Field #2" VisibleIndex="1" />
</Columns>
</dx:ASPxGridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="searchComments"
TypeName="App.CommentManager">
<SelectParameters>
</SelectParameters>
</asp:ObjectDataSource>
If id is "hard-coded" in the searchComments method, gridview is populated with correct entries.
My only problem is passing the article id to searchComments method.
I was thinking about a <%# %>" style databinding approach, but it would be extremely dirty, and it still doesn't work.
SelectMethod="searchComments(<%# Request.QueryString["id"] %>)"
Another approach I tried was setting the selectmethod in codebehind like this:
ObjectDataSource1.SelectMethod = "searchComments('123456')";
That results in error: ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'searchComments('123456')' that has no parameters.
Pass a QueryStringParameter directly into SelectParameters - ie:
<SelectParameters>
<asp:QueryStringParameter QueryStringField="id" />
</SelectParameters>
You can use QueryStringParameter in SelectParameters section
<SelectParameters>
<asp:QueryStringParameter QueryStringField="id" Name="id"/>
</SelectParameters>
Related
I have a formview that is populated with an ObjectDataSource and it works great. Nested in that is a gridview also populated with a nested ODS that works perfect 99% of the time. Also nested in the formview is another ODS populated from the same control that populates the other nested datasource that works perfect all of the time. Code below to make that make sense.
The problem is if you change pages it all works fine most of the time. There are 2 search features built into the page. That is where the error occurs. If you use either of them without changing pages it works great. If you use one of them and then change pages and try to use either one of them the page crashes with an error that says something like Control Parameter 'X' is not found in 'Y'. The problem is that it is not a true error because there is a button that displays the bootstrap model which displays its gridview with no issues and the ODS is based on exactly the same control so it is obviously there. I imagine that it is related to the events, but they step through just fine. I am not sure what could be causing it.
Here is a little test project that is basically the same setup only paging is setup in the formview and the ODS1 in the real project and it works perfectly.
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
ImportantID:
<asp:Label Text='<%# Bind("ImportantID") %>' runat="server" ID="ImportantIDLabel" Visible="false" /><br />
Name:
<asp:Label Text='<%# Bind("Name") %>' runat="server" ID="NameLabel" /><br />
<div id="nestedContent">
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="listNested" TypeName="TestProject.DataSourceClass">
<SelectParameters>
<asp:ControlParameter ControlID="ImportantIDLabel" PropertyName="Text" DefaultValue="-1" Name="ImportantIDFromOnject1ForNestedObject" Type="Int32"></asp:ControlParameter>
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource2" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="YetAnotherProperty" HeaderText="YetAnotherProperty" SortExpression="YetAnotherProperty"></asp:BoundField>
<asp:BoundField DataField="OtherPropertiesGoInThisClass" HeaderText="OtherPropertiesGoInThisClass" SortExpression="OtherPropertiesGoInThisClass"></asp:BoundField>
</Columns>
</asp:GridView>
</div>
SomeProperty:
<asp:Label Text='<%# Bind("SomeProperty") %>' runat="server" ID="SomePropertyLabel" /><br />
SomeOtherProperty:
<asp:Label Text='<%# Bind("SomeOtherProperty") %>' runat="server" ID="SomeOtherPropertyLabel" /><br />
<asp:ObjectDataSource ID="ObjectDataSource3" runat="server" SelectMethod="GetDataObject2" TypeName="TestProject.DataSourceClass">
<SelectParameters>
<asp:ControlParameter ControlID="ImportantIDLabel" PropertyName="Text" DefaultValue="-1" Name="ImportantID" Type="Int32"></asp:ControlParameter>
</SelectParameters>
</asp:ObjectDataSource>
<div id="BootStrapModalPopup">
This displays the data with no problems
</div>
</ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetDataObject1" TypeName="TestProject.DataSourceClass">
<SelectParameters>
<asp:Parameter DefaultValue="-1" Name="ImportantID" Type="Int32"></asp:Parameter>
<asp:Parameter DefaultValue="" Name="Name" Type="String"></asp:Parameter>
<asp:Parameter Name="SomeProperty" Type="String"></asp:Parameter>
</SelectParameters>
</asp:ObjectDataSource>
I would appreciate any ideas of where to look next. By the way I have used a textbox, hiddencontrol and Session variable for the ID field and nothing makes a difference for it. If I disable the ODS2 it goes back to working perfectly also but I need that data.
Thanks
Jimmy
I actually lucked into the answer for this. It was not my nesting it was my paging. I will leave the question for anyone that has a similar problem since I don't see any successful errors to this. The issue I faced was I needed to reset my pageindex on search. It worked great searching from page one, but searching from any other page is an empty return due to the indexing so the controls did not exist. Hopefully it will help someone to search that if you run into this issue. Just set pageindex on the formview back to 0.
I use an asp:SqlDataSource Element to fetch Data and then display it in an asp:ListView.
For the sake of simplicity, let's assume the Database consists of the rows and id, author (it's actually more, but that doesn't matter).
This is the code I use:
<asp:SqlDataSource ID="NewsDataSource" runat="server"
ConnectionString="<%$ connectionStrings:RemoteSqlConnection %>"
ProviderName="System.Data.SqlClient"
SelectCommand="SELECT * FROM [news]"
UpdateCommand="UPDATE [news] SET author=#author WHERE id=#id"
DeleteCommand="DELETE FROM [news] WHERE id=#id"
InsertCommand="INSERT INTO [news] (author) VALUES (#author)">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
My problem is, that the parameter id which I define for UpdateParameters and DeleteParameters is always null.
It doesn't seem to be associated with the database field id.
One hack which allowed me to fix the problem (but only for the update case) was to insert an invisible asp:Label to which I binded the id (Just like I binded the Author field to a Textbox).
I don't think that the ListView code should be relevant, but I'll try to include some lines which are relevant here:
<asp:ListView runat="server" DataSourceID="NewsDataSource">
<LayoutTemplate>
<div id="itemPlaceholder" runat="server"></div>
</LayoutTemplate>
<ItemTemplate>
<%# Eval("author")%>
</ItemTemplate>
<EditItemTemplate>
<!-- This line is only the workaround solution --><asp:Label ID="idLabel" runat="server" Text='<%# Bind("id")%>'></asp:Label>
<asp:TextBox ID="authorTextbox" runat="server" Text='<%# Bind("author")%>'></asp:TextBox>
</EditItemTemplate>
</asp:ListView>
There is an attribute on the asp:ListView called DataKeyNames, adding id to this should make it store that parameter value without the need for a hidden element.
For more information on the DataKeyNames attribute see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.datakeynames(v=vs.110).aspx
Visual Studion 2012 Update 3 with mySQL .NET Connector 6.6.5.0
I cannot get a GridView to post back any updated data
My GridView is configured as:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Unit" />
<asp:BoundField DataField="Price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mysqlConn %>" ProviderName="<%$ ConnectionStrings:mysqlConn.ProviderName %>"
SelectCommand="SELECT ID, Name, Unit, Price FROM product"
UpdateCommand="UPDATE product SET Name = ?, Unit = ?, Price = ? WHERE ID = ?">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" Direction="Input" />
<asp:Parameter Name="Unit" Type="String" Direction="Input" />
<asp:Parameter Name="Price" Type="Single" Direction="Input" />
<asp:Parameter Name="ID" Type="Int64" Direction="Input" />
</UpdateParameters>
</asp:SqlDataSource>
The grid displays the list of products fine. However when I select Edit in the grid view and change a value and then click Update the resulting change is not posted back to the database. What am I doing wrong with my Update statement or parameters?
All pointers appreciated.
please use GridView1.DataBind();
then gridview will bind with new data.
please refer this link
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
I was missing:
DataKeyNames="ID"
as part of the GridView definition which was preventing the UPDATE from happening.
I have a DropDownList that lists the users in a table of the database (.accdb) and a GridView that should list the Dates when the user logged in which are stored in another table of the same database.
I linked the dropdown with a ObjectDataSource that is linked to the users table and the gridview to an objectdatasource that is linked to the lof table and has a select query like this :
SELECT Data
FROM log
WHERE (User = ?)
I set the parameter to be what is selected in the DropDownList.
And here is my problem : the DropDownList is showing all the users correctly, but the GridView doesn't even show on the page. I tried with postback and GridView.DataBind() but none works.
I am pretty sure it's a stupid mistake i did, but i can't find it.
Generated ASPX code :
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ObjectDataSource1" DataTextField="Utilizator"
DataValueField="ID"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.DataTable1TableAdapter">
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetDataBy" TypeName="DataSet1TableAdapters.logTableAdapter">
<InsertParameters>
<asp:Parameter Name="Data" Type="String" />
<asp:Parameter Name="Utilizator" Type="String" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" DefaultValue=""
Name="Utilizator" PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource2" Height="145px" Width="107px">
<Columns>
<asp:BoundField DataField="Data" HeaderText="Data" SortExpression="Data" />
</Columns>
</asp:GridView>
I have a simple gridview (well it is now) it populates, I can edit it. but when it comes to updating I just cant get it to work.
The following code creates a gridview which searches for users whose name starts with a "c" (Thats part of my filtering I stripped out)
The problem is when I click the update button it won't update. The Stored procedure gets called but all parameters passed to it are null. therefore the database is not updated.
I know its a simple problem with a simple solution but I just can't see it.
I tried using ExtractValuesFromCell within the onupdating event and still only got nulls. I had to add the CausesValidation="false" because without it the update events were not even called.
Any and all help is appreciated
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="SqlDataSource3"
DataKeyNames="UserId">
<Columns>
<asp:CommandField ShowEditButton="True" CausesValidation="false" />
<asp:BoundField DataField="UserName" HeaderText="UserName"
SortExpression="UserName" />
<asp:BoundField DataField="MobileAlias" HeaderText="MobileAlias"
SortExpression="MobileAlias" />
<asp:BoundField DataField="DistrictId" HeaderText="DistrictId"
SortExpression="DistrictId" />
<asp:BoundField ReadOnly="true" DataField="UserId" HeaderText="UserId"
SortExpression="UserId" />
</Columns>
</asp:GridView> <asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:REMConnectionString_development_dev %>"
SelectCommand="LoadUser" SelectCommandType="StoredProcedure"
UpdateCommand="UpdateUser" UpdateCommandType="StoredProcedure"
onupdating="SqlDataSource3_Updating">
<SelectParameters>
<asp:Parameter DefaultValue="c" Name="UserName" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="UserId" Type="String" />
<asp:Parameter Name="DistrictID" Type="Int32" />
<asp:Parameter Name="UserName" Type="String" />
<asp:Parameter Name="MobileAlias" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
the only code in the codebehind is
protected void SqlDataSource3_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
}
It is there so I can drop in a breakpoint and inspect the parameters, which are all there but with null values
Something else has to be at work here. I replicated the code you provided and when I break down the event arguments I see the values and it all works correctly. Could you edit your post and include all the code?
Ok I have had a chance to do some more research and confirmed the issue.
Setting the Master Page ID in the Page_load function is the issue. It needs to be set in the Page_Init.
This page provides a good explanation
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=469
So by moving
ID= "REM"; from page_load to Page_init the problem magically went away.
Unfortunately it is common to advise setting the ID in Page_Load which is what I relied on and turned out to be bad advice.
DC