ASPX FormView data not displayed with valid data from datasource - asp.net

I have an aspx Formview set to 'Edit' mode and a valid SQL datasource that returns the correct data row based on the row ID. These lines in my grid rowcommand event:
DailyDataSource.SelectParameters["ID"].DefaultValue = recid.ToString();
FormView1.DataBind();
DataRowView drv = (DataRowView)FormView1.DataItem;
returns a valid DataRowView object with the correct data record. However, the DataBind() appears to not be working - no data appears in the entry text boxes. Unless I set the entry textboxes to 'ReadOnly'. They then display the returned data, but I (obviously) cannot edit it.
I can edit and enter data in the non-readonly text boxes, and the Save command saves the data, and it displays in the gridview, but if I try to edit it, I again get a valid set of data, but no displayed data.
My question: Why is making these fields readonly allowing the data to be displayed?
Second question: Why is the data in the datasource returned record not appearing in the editable textboxes?

BLUF: I needed to do the formview data load in the PostBack event, in order to get the correct session variables which were set by other events. Question of "How can I be so dumb when I am so smart?"

Related

VB Load stored session variable into an asp dropdown list

I have a VB web app where I am allowing the user to enter data into textboxes and allowing the user to select several dropdown values from a sql data table. I'm trying to save the data into session variables and load the data on a later page.
I've figured out how to save and load the data in the textboxes but am having issues with the dropdown lists.
To set the session variables to the user entered textbox values:
Session("CompanyName") = txtCompanyName.Text
Session("AnnualRevenue") = txtAnnualRevenue.Text
And I load similarly:
txtCompanyName.Text = Session("CompanyName")
txtAnnualRevenue.Text = Session("AnnualRevenue")
That all works properly, but i'm not sure how do user dropdowns in the same way.
Here is my code to set the session variable to the user selected value:
Session("DDLHQ") = DropDownListHQ.SelectedValue
This sets the session variable to the string of the selected value.
How do I load the saved session variable from the previous selection of the dropdown list into a dropdown list on another page?
I'm completely lost on this right now.
You should make sure your DropDownList is populated with items first, and you should check to see if the item you are attempting to assign as the SelectedValue actually exists.
If Not (DropDownListHQPage2.Items.FindByValue(Session("DDLHQ")) Is Nothing) Then DropDownListHQPage2.SelectedValue = Session("DDLHQ")

Assign Current GridView Data Table to Session?

I have a GridView consisting of editable text boxes. When the user enters a value, the OnTextChanged function calls my UpdateColumns() method, which performs various calculations and alters the whole GridView. I have limited user input to only numbers so that the operation errors out when the user enters alpha characters in any cell. Upon this error, I would like to revert to the Data Table that existed immediately prior to the user entering the alpha character.
Is there a way to capture the current GridView's data source and store in a DataTable?
I have tried the following (after the data table has been bound):
Dim dt As DataTable = DirectCast(gvBuildingBlocks.DataSource, DataTable)
Session("buildingBlocks") = dt
'If textboxes are all numeric, perform calculations here
'Else display error here and bind table below
gvBuildingBlocks.DataSource = CType(Session("buildingBlocks"), DataTable)
gvBuildingBlocks.DataBind()
The above results in an empty gridview, as the value of gvBuildingBlocks.DataSource is empty. Is there anything else I can do to get the values?
I imagine I could loop through all the rows and save them somehow as follows:
For Each row As GridViewRow in gvBuildingBlocks.Rows
'Save cell value here
Next
But I don't know what code to use to save the values?
Any help is much appreciated.
Thanks!
I understand you are doing calculations on rows and you want to rollback to original values if the user input is invalid. You could accomplish this at the row level by calling DataRow.BeginEdit() before the edit, and DataRow.CancelEdit() if the input is invalid. See this example
Don't forget to call DataRow.EndEdit() if the edit is valid to commit changes.

Bind a Text Box to a Field Selected by SQLDataSource (VB.NET)

I'm looking for the easiest way to bind data from a SqlDataSource to textboxes dropped in Visual Studio 2008.
For example, I have 4 textboxes currently that have Address, City, State, Zip. I also have a SqlDataSource on the page fetching the ID of the record and selecting those 4 fields based on ID.
How am I able to quickly bind each box to those particular fields selected? I would think this would be really straight forward - but seems it's not. Seems like the answer is funneled towards having to create a GridView or some type of control.
Be gentle...I'm a nub :)
In general you are correct, if you want to use databinding you'll need to use an appropriate control. For this example I'd suggest using a FormView - it is designed to display the results from a single database record and uses templates, meaning you'll have complete control over the output. This article is probably a good place to start: FormView Control: Step by Step.
To read the values bound to the FormView in the code-behind class you would need to create an event handler for the FormView's DataBound event. In that event handler you would reference the controls programmatically via FindControl, like so:
Dim myLabel As Label = CType(FormViewID.FindControl("id"), Label)
Here, id would be the ID of the Label whose value you were interested in. Once you have a reference to the Label you can get its value using myLabel.Text.

GridView Initial Editing mode?

I am writing a web application that is used for tracking and entering some everyday user data... Every day I need to enter some data for multiple users manually...
I will choose the date with the calendar, and for that date I will get DB values assigned for all users on chosen date...If there are some entries in database for that date i will display it in gridview...
Here is the look of gridview for chosen date...
USERS DATA
------------
User1 Data1
User2 Data2
User3 Data3
But, if I choose the date for which there are no entries in DB, I would like to display the gridview containg User names in each row, but I want the second column (DATA field) to be initially editable for ALL rows (the whole second column should contain textboxes for inserting values, not only selected row as in standard editable gridview mode) allowing inserting of wanted values...
I want it to look something like this
USERS DATA
----------------
User1 TextBox
User2 TextBox
User3 TextBox
My idea is to get Users list from DB and create a datatable with first column containg returned user names, and second column empty... This datatable will be datasource for gridview in which I am going to disable editing mode, and in normal (displaying) mode I am going to have TemplateField containg of textboxes bound to that datatable in second column, and labels displaying usernames in first column... When entering all values there will be SAVE button who is calling a method that will read entered values from datatable and save it to database...
Is there a better way to implement this functionality? If Templatefield textboxes are bound to datatable (as gridview datasource) textboxes entered values are going to be accessible from datable object, but Save button click causes postback, and I don't know how to preserve datatable state? Is there a better way to get wanted gridview with whole second column availibe for inserting new values?
Thank you in advance for your help & time!
Try this
http://www.highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx
http://csharpdotnetfreak.blogspot.com/2009/05/edit-multiple-records-gridview-checkbox.html
I would retrieve the full dataset using a left join in the database, and if there is no entry for a user, that user will be returned with a blank value. Something like this:
select users.user,
isnull(entries.entry,'')
from users
left join entries on users.userid = entries.userid
I think you can override the OnDataBinding (or OnDataBound) method to check and see if the second column is empty, and place the field in edit mode if it is. This way, if some users have entries but some don't, only the users with no entry will be in edit mode. Any user with an entry will be in display mode. There is a sample for this at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onrowdatabound.aspx.

Webcombo return error when trying to populate based on another WebCombo

This has gotten me completely frustrated
I have a few webcombo boxes that are hierachial:
Continent
Country
When the form loads everything works fine, when I change the continent the first time, the country repopulates correctly. However if I change the continent a second time I receive an error:
Specified argument was out of the range of valid values.Parameter name: The DataValueField of ValueField was not found in the Columns collection.
Can anyone tell me why?
P.S. This is all I have in the Page_Load event
if (!IsPostBack)
{
this.Load_AreaList();
this.Load_AreasOfInterest();
this.Load_Degrees();
this.Load_GenderList();
this.Load_ParticipationDateModifiers();
this.Load_ProgramCategories(nEventID);
this.Load_YesNoList();
this.Load_ParticipantInformation(nParticipantID);
}
Also this happens in another part of the form with another Country webcombo which is supposed to populate a state combo box.
In many cases, columns in database tables return null when no value is stored in that column. However, a null value can present challenges when you are working with ASP.NET code or with data-bound Web controls. For example, an exception is thrown if you try to bind the SelectedValue of a DropDownList control to null. If this is not the case change AppendDataBoundItems property. The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items.
Let me know if it works...
s

Resources