Data binding directly to a store query - asp.net

I am trying to group amount and load into a drop down box in vb.net (asp.net)
But I get the following error:
Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see
My Code:
'Load Amounts
Dim SourceAmounts = (From p In db.PayoutAdjustments
Order By p.Amount
Where p.PayoutId = PayoutId
Group p By Key = p.Amount Into Group
Select Amount = Group)
cmbAmount.DataSource = SourceAmounts
cmbAmount.DataTextField = "Amount"
cmbAmount.DataValueField = "Amount"
cmbAmount.DataBind()
cmbAmount.Items.Insert(0, New ListItem("Select Amount", 0))

you need to execute the query before data binding. Using ToList() will force your query to execute.
cmbAmount.DataSource = SourceAmounts.ToList()

Related

Transfer Array into SQLite and vice versa without looping especially in ActionScript3

I am very new to programming in ActionScript3 and using SQLite. Currently I try to make an AIR application where it will handle from small to large dataset from excel, import into and calculate in AIR using ActionScript3 and stored the calculated data in SQLite.
Right now I have 2 question regarding using array to transfer data into SQLite
1. Is it possible to INSERT data or UPDATE data in SQLite without using looping?
In my AIR application, I have an object called DataInput in which have 62 variables/properties which will contain 62 different values from 62 columns in excel. I do know that if you create new instance of DataInput and push into array e.g arDataInput in loop, you can access each of the data in the array using arDataInput[i].variablename where i is the index of the array and variablename is the variables/properties of the object.
Obviously, right now I do actually using for loop to access the value of each cell in excel then calculate in AIR application before transfer the calculated data into SQLite, row by row within sql transaction.
Is there actually a way to transfer data in array into SQLite (either INSERT new row or UPDATE existing row) without using loop like INSERT INTO tblDataInputUW VALUES arDataInput given that each column name in SQLite table is the same as variables/properties of object within the array eg. SQLite table, tblDataInput have column name namePlat and array have variables/properties of arDataInput.namePlat?
2. Is it possible to split the data array i got from SQLite into multiple array without looping?
Right now, I use the following code to extract data from SQLite and stored as an array;
txtSQL = new String();
arData = new Array();
txtSQL = "SELECT namePlat, platLat, platLong FROM tblDataInput";
arData = getSQLData(txtSQL);
function getSQLData(text: String): Array
{
sqlCon.begin();
sqlStat = new SQLStatement();
sqlStat.sqlConnection = sqlCon;
sqlStat.text = text;
sqlStat.execute();
sqlCon.commit();
var result: SQLResult = new SQLResult();
var arData: Array = new Array();
result = sqlStat.getResult();
if (result != null)
{
arData = result.data;
}
return arData;
}
If my assumption are correct, I can access each of the value in arData by using arData[i].variablename where i is the index of the array and variablename is the table name within the SQLite table tblDataInput.
If there are way to split the data in arData into 3 different array e.g arNamePlat without using looping like arNamePlat = arData.variablename because I have many different chart to draw in my AIR application and each chart will have its own array to get value from.
Right now, I actually using different sql statement for different chart like;
txtSQL = new String();
arData = new Array();
txtSQL = "SELECT namePlat FROM tblDataInput";
arData = getSQLData(txtSQL);
dgNamePlat.dataProvider = new DataProvider(arData);
txtSQL = new String();
arData = new Array();
txtSQL = "SELECT platLat, platLong FROM tblDataInput";
arData = getSQLData(txtSQL);
dgPlatLatLong.dataProvider = new DataProvider(arData);
I use the same arData and txtSQL for each chart as I dont store the value anymore after the chart been drawn.
There are actually no restriction for me to just use for loop, I asking this question as I don't see any topic regarding on this question and as a self-learning programmer, I like to explore different way of coding and way to incorporate that knowledge into my projects.
The best way to send and receive data and do database stuff is through form submissions. AIR can POST regular form submissions like html pages but can also load a response. Regular serverside software like php handles the form submission, does whatever with the database, and returns data or simple success message.
The best way to handle data is to use XML (look into RESTful architecture for reasons why). PHP creates XML for the app. The xml gets loaded and should go into arrays of objects.
Here are some good links to get going on submitting data with forms and reading XML:
republic of code tutorials:
http://www.republicofcode.com/tutorials/flash/as3contactform/2.php
http://www.republicofcode.com/tutorials/flash/as3xml/
https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequest.html
https://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/URLRequestMethod.html

XPODataSource: Select from TableValuedFunctions

Is it possible to select from a tabled-valued function using xpoDataSource instead of selecting from a table.
note: Im using xpoDatasource with serverMode = true
Source - Table Valued Function in XPO (and XAF)?
You can accomplish this task via direct SQL queries. Here is an example:
IDataLayer dal = XpoDefault.GetDataLayer(MSSqlConnectionProvider.GetConnectionString("(local)", "TestDatabase"), AutoCreateOption.None);
Session session = new Session(dal);
SelectedData data = session.ExecuteQueryWithMetadata("SELECT * FROM TrackingItemsModified(2)");
XPDataView view = new XPDataView();
foreach (var row in data.ResultSet[0].Rows) {
view.AddProperty((string)row.Values[0], DBColumn.GetType((DBColumnType)Enum.Parse(typeof(DBColumnType), (string)row.Values[2])));
}
view.LoadData(new SelectedData(data.ResultSet[1]));
GridControl control = new GridControl();
control.DataSource = view;
control.Dock = DockStyle.Fill;
Form form = new Form();
form.Controls.Add(control);
form.ShowDialog();
Reference these:
How to populate an XPServerCollectionSource/InstantFeedbackCollectionSource with a runtime designed SQL statement result?
How to pass a table valued parameter to Session.ExecuteSProc method

How to use "strongly typed data table" (or "row") in WebForms code-behind?

Our asp.net WebForms application is using table-adapters for "strongly typed data tables" and data-rows. In the code-behind I find I cannot use the return value directly from the BLL-class instance (such as our "Main_TblAdap.CostDataTable") as in referring to the strongly-typed data-rows and columns/fields within.
What is the proper way to make use of the strongly-typed data-table or data-row within the code behind events and methods?
Further, we want the ability to reference a specific data-row or sort/filter the data-table.
A good coding example would be very helpful showing the best way to (1) get from a strongly-typed data-table to reference values in a specific data-row from within the data-table and (2) how to sort/filter the strongly-typed data-table.
First create a DataView from the Table. You can either customize the view on initialization or call get default.
DataView dv = yourTable.DefaultView;
Or go ahead and filter the view from the table when you create your reference pointer like so:
DataView custDV = new DataView(YourTable[yourTableName],
"VehicleID = 'xxx'", // Row filter
"VehicleID", // Sort
DataViewRowState.CurrentRows);
Now you can sort the View and filter by Row state
dv.Sort();
view.RowStateFilter = DataViewRowState.Added |
DataViewRowState.ModifiedCurrent;
ROWS.
You can interact with the DataRowView IEnumerable within the DataView.
foreach (DataRowView rowView in thisDataView)
{
// your code here
}
OR
foreach(DataRow r in dt.Rows)
{
// your code here
}
To find rows in the table try:
var result = dt.AsEnumerable().Select(r => r["Id"] = 4);
if(result !=null)
{
int x = 0;
}
Now just bind to your view and if you add a row or update it, the view gets updated as well.

Unable to bind list to gridview after sorted using linq ASP.NET

I am sorting a list of object and binding the sorted list to a gridview that supports paging.
I tried:
var selectedNew = selected.AsQueryable<Customer>().OrderBy(sortExpression);
selectedNew.ToList<Customer>();
gdvEmployees.DataSource = selectedNew;
gdvEmployees.DataBind();
I am getting the following error:
The data source does not support server-side data paging
What is causing the above error to be thrown?
ToList produces an output, you cannot use it by itself. try this:
var selectedNew = selected.AsQueryable<Customer>().OrderBy(sortExpression);
var selectedNewList = selectedNew.ToList<Customer>();
gdvEmployees.DataSource = selectedNewList;
gdvEmployees.DataBind();

How to use SQL Server 2008 stored procedure in asp.net mvc

I have created a simple stored procedure in SQL Server 2008 as:
CREATE PROCEDURE viewPosts
AS
SELECT * FROM dbo.Post
Now, I have no idea how to use it in controller's action, I have a database object which is:
entities db = new entities();
Kindly tell me how to use stored procedure with this database object in Entity Framework.
For Details check this link:
http://www.entityframeworktutorial.net/data-read-using-stored-procedure.aspx
Hope this will help you.
See article about 30% in:
In the designer, right click on the entity and select Stored Procedure mapping.
Click and then click the drop down arrow that appears. This exposes the list of all Functions found in the DB metadata.
Select Procedure from the list. The designer will do its best job of matching the stored procedure’s parameters with the entity properties using the names. In this case, since all of the property names match the parameter names, it maps every one correctly so you don’t need to make any changes. Note: The designer is not able to automatically detect the name of the field being returned.
Under the Result Column Bindings section, click and enter variable name. The designer should automatically select the entity key property for this final mapping.
The following code is what I use to initialize the stored procedure, then obtain the result into variable returnedResult, which in this case is the record id of a newly created record.
SqlParameter paramResult = new SqlParameter("#Result", -1);
paramResult.Direction = System.Data.ParameterDirection.Output;
var addParameters = new List<SqlParameter>
{
new SqlParameter("#JobID", EvalModel.JobID),
new SqlParameter("#SafetyEvaluator", EvalModel.SafetyEvaluator),
new SqlParameter("#EvaluationGuid", EvalModel.EvaluationGuid),
new SqlParameter("#EvalType", EvalModel.EvalType),
new SqlParameter("#Completion", EvalModel.Completion),
new SqlParameter("#ManPower", EvalModel.ManPower),
new SqlParameter("#EDate", EvalModel.EDate),
new SqlParameter("#CreateDate", EvalModel.CreateDate),
new SqlParameter("#Deficiency", EvalModel.Deficiency.HasValue ? EvalModel.Deficiency.Value : 0),
new SqlParameter("#DeficiencyComment", EvalModel.DeficiencyComment != null ? EvalModel.DeficiencyComment : ""),
new SqlParameter("#Traffic", EvalModel.Traffic.HasValue ? EvalModel.Traffic.Value : 0),
paramResult
};
// Stored procedure name is AddEval
context.Database.ExecuteSqlCommand("AddEval #JobID, #SafetyEvaluator, #EvaluationGuid, #EvalType, #Completion, #ManPower, #EDate, #CreateDate, #Deficiency, #DeficiencyComment, #Traffic, #Result OUTPUT", addParameters.ToArray());
var returnedResult = paramResult.Value;
NewEvaluationID = Convert.ToInt32(returnedResult);

Resources