I have an asp.net intranet site hosted on IIS on my computer.
On one page you can enter two user id's and get a side by side comparison of their roles in our (custom app) system.
I often use it to add roles when I'm setting up new users. After running the query, if i change a role and run the query again it will not show updated results. It's being cached somehow. I have to go to another page and come back and them run the query to get updated results.
How can avoid having to navigate away to get the updated query results displayed.n
here's the code
<asp:Label ID="Label1" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Username"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server"
Text="Compare Permissions" />
<br /><br />
<asp:GridView ID="GridView1" runat="server" CssClass="mGrid" DataSourceID="SqlDataSource1">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="WITH
firstPerson AS
(
select username, security_role from user_role
where username=upper(:Username1)
),
secondPerson as
(
select username, security_role from user_role
where username=upper(:UserName2)
)
select firstPerson.username , firstPerson.security_role,secondPerson.username,secondPerson.security_role from firstPerson FULL JOIN secondPerson
on firstPerson.security_role=secondPerson.security_role order by firstPerson.security_role, secondPerson.Security_role">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="Username1"
PropertyName="Text" />
<asp:ControlParameter ControlID="TextBox2" Name="UserName2"
PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
I think the problem is simply that after you add these new values, you are not refreshing your gridview. There aren't any controls that can "automagically realize" the data has changed. Instead, you need to do it manually, by rebinding your data view control to it's (new) data with DataBind().
Check where you are getting your data.
If you have it in an if statement like
if(!IsPostBack)
That means you will only get the data when you first run. When you are navigate away and then back you are loading the page again for the first time.
Related
I've been looking online for a while and I may not be asking my question right but I can't find what I am looking for.
I have a dropdownlist on a web form that is databound to a SQL select statement that returns 3 items. I appended the list with a blank item at the top so that I am not selecting the other options on page load.
I also have a listview that has a ControlParameter of the DDL.
What I want to do is when the page loads to have all items selected instead of none. Do I need to set Selected to True on each of the items in the list and how would I do that if the information is fetched from a SQL query.
A snippet of the code with the queries:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSourceDockNumber"
DataTextField="DockID" DataValueField="DockID"
AutoPostBack="True" AppendDataBoundItems ="true">
<asp:ListItem Selected="False" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceDockNumber" runat="server"
ConnectionString="<%$ ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT DISTINCT [DockID] FROM [Slip]">
</asp:SqlDataSource>
<br />
<asp:SqlDataSource ID="SqlDataSourceAvailSlips" runat="server"
ConnectionString="<%$ConnectionStrings:MarinaConnectionString %>"
SelectCommand="SELECT * FROM [avilableSlips] WHERE ([DockID] = #DockID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="DockID"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Pretty sure I know why I'm getting the error, just not sure how to go about fixing it. In a FormView ItemTemplate I've got a Label control for the Project ID# bound to the FormView's SQLDatasource. I've also got a DropDownList whose SQLDatasource I've got tied to the Label.Text as a ControlParameter. I am guessing since the Label control isn't bound yet, that's causing my DDL to kick out that error.
ASPX
<asp:FormView ID="FvChangeOrder" runat="server" DataKeyNames="ProjectChangeOrderID"
DataSourceID="FvChangeOrderSQL" OnItemCommand="FvChangeOrder_OnItemCommand"
OnDataBound="FvChangeOrder_OnDataBound">
<ItemTemplate>
<div>Project #: </div>
<div>
<asp:Label ID="LblProjectID" runat="server" Text='<%# Bind("ProjectID") %>' /></div>
<div>Shipment #: </div>
<div>
<asp:DropDownList runat="server" ID="DdlShipment" DataSourceID="DdlShipmentSQL"
SelectedValue='<%# Bind("ProjectShipmentID") %>' DataValueField="ProjectShipmentID"
DataTextField="ShipmentNo" Enabled="False" />
</div>
...
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="FvChangeOrderSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT [ProjectID], [ProjectChangeOrderID], [ProjectShipmentID], [SeqNo],
[Date], [EnteredBy_UserID], [Source], [Initiator], [Reason], [ReasonNotes],
[ApprovalCode], [Description], [NumPanels], [Amount], [IsCommissionable], [DateDue],
[DateRecd], [Status]
FROM [tblProjectChangeOrder] WHERE ([ProjectChangeOrderID] = #PCOID)">
<SelectParameters>
<asp:QueryStringParameter Name="PCOID" QueryStringField="PCOID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DdlShipmentSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT pco.ProjectShipmentID, ps.ShipmentNo FROM tblProjectChangeOrder pco
LEFT JOIN tblProjectShipment ps ON pco.ProjectShipmentID = ps.ProjectShipmentID
WHERE pco.ProjectID = #ProjectID">
<SelectParameters>
<asp:ControlParameter ControlID="FvChangeOrder$LblProjectID"
Name="ProjectID" PropertyName="Text"/>
</SelectParameters>
</asp:SqlDataSource>
Would I be better off leaving out the WHERE and ControlParameter on the DDL SQLDatasource and changing the DDL's SQL Source in codebehind on the OnDataBound method of the FormView?
Think I got it worked out. I changed the ControlParameter to just Parameter Type="String" and on Form_Load set a Label to FvChangeOrder.FindControl("LblProjectID"), assigned LblProject.Text to a string, then passed that as the DefaultValue for the Select Parameter.
On an semi-related problem I had to remove the SelectedValue on the markup side and handle SQLCommand in the Form_Load for the DDL SelectedValue. Probably a bad dataset but I kept getting an error that the value in the field wasn't in the List or something like that. There are multiple Change Orders for a given project that have NULL values for the ShippingID.
Anywho, all is working on the ItemTemplate side. Now to see if I can keep everything working on the EditItemTemplate. :P
I'm using ASP.NET (C#), MySQL.
I'm trying to use a FormView component feed by a SQLDataSource ... the goal is to create a form to handle data from a single table without too much coding and relying on the framework as much as I can.
I found how to show data and to edit them but when I come to 'date' fields I found an early stop in my mission.
Since I'm italian I use a standard 'italian' date format (dd/MM/yyyy) that's quite different from MySQL internal date format (yyyy-MM-dd) this lead to a date format error when I try to update fields.
The form which include the 'date' field is defined this way:
<asp:FormView
ID="_frmData"
runat="server"
DataKeyNames="id"
DataSourceID="_sdsContatto"
DefaultMode="Edit">
<EditItemTemplate>
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:TextBox
ID="_txtDate"
runat="server"
Text='<%#Bind("date_field","{0:dd/MM/yyyy}")%>' />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
</EditItemTemplate>
</asp:FormView>
The SQLDataSource is defined this way:
<asp:SqlDataSource ID="_sdsData
runat="server"
ConnectionString="<%$ ConnectionStrings:data_db %>"
ProviderName="<%$ ConnectionStrings:data_db.ProviderName %>"
SelectCommand=" SELECT
date_field
FROM
table
WHERE
id=#id;"
UpdateCommand=" UPDATE
table
SET
date_field = #date_value
WHERE
id=#id;">
</asp:SqlDataSource>
Using the 'italian' date format lead to this error:
Incorrect date value: '9/12/1971' for column 'date_field' at row 1
There's a way to fix this type of problem ?
I tried with MySQL STR_TO_DATE() without luck.
Best Regards for any help or suggestion.
Mike
The solution relay into 'separate' visualization from data submission. To cut it short the best way to ovveride such problems is this one:
<asp:FormView
ID="_fvData"
runat="server"
DataKeyNames="id"
DataSourceID="_sdsTable1"
DefaultMode="Insert" Width="100%">
<InsertItemTemplate>
<asp:TextBox
ID="_txtDate"
runat="server"
Text='<%#Eval("date_field","{0:dd/MM/yyyy}")%>' />
</InsertItemTemplate>
</asp:FormView>
In the sqldatasource (either in InsertParameters or UpdateParameters sections) the we need to reference the field trough a ControlParameter entry, in this way:
<asp:SqlDataSource
ID="_sdsTable1"
runat="server"
InsertCommandType="StoredProcedure"
InsertCommand="create_table1">
<InsertParameters>
<asp:ControlParameter
Name="date_field"
ConvertEmptyStringToNull="true"
Type="DateTime"
ControlID="_fvData$_txtDate"
PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
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 have an access data source which loads a listbox values. I am trying to pass a parameter to the accessdatasource but I'm not sure what I am doing wrong.
<asp:DropDownList ID="customerName" runat="server">
<asp:ListItem value="69" Text="Danny"></asp:ListItem>
<asp:ListItem value="23" Text="Sarah"></asp:ListItem>
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID='?'">
<selectparameters>
<asp:ControlParameter Name="customerID" ControlID="customerName" PropertyName="SelectedValue" />
</selectparameters>
<asp:ListBox ID="lstCustomers" runat="server" AutoPostBack="True"
DataSourceID="AccessDataSource1" DataTextField="customerName"
DataValueField="customerID" Width="175px" Height="365px"
onselectedindexchanged="lstCustomers_SelectedIndexChanged"></asp:ListBox>
The list comes back as blank... not sure what I'm doing wrong!
Just to clarify.. This was solved by Richard Deeming, but there's no answer to answer. I'll post the code here
It was solved by removing the single quote marks around the ? parameter in the SQL
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="/App_Data/data.mdb" SelectCommand="SELECT * FROM table WHERE customerID=?">