XPODataSource: Select from TableValuedFunctions - devexpress

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

Related

Query through SPListCollection

I wonder if it's somehow possible to query a SPListCollection object using SPQuery, as with SPListItemCollection. Imagine that you want to find out which lists were created by a given Author or visible for a given user, for example.
No, this is not possible with SPQuery! But i would prefer you using KeywordQuery:
using (SPSite siteCollection = new SPSite("http://server/sitecollection"))
{
KeywordQuery keywordQuery = new KeywordQuery(siteCollection);
keywordQuery.QueryText = "SharePoint";
SearchExecutor searchExecutor = new SearchExecutor();
ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery);
var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults);
var resultTable = resultTables.FirstOrDefault();
DataTable dataTable = resultTable.Table;
}
Within the Keywordquery you could use for example contentclass STSList to retrieve only lists. And in this case when only using contentclass:"STSList" then you would get all lists where the executor has permissions. You can narrow down by adding additional query parameters. SharePoint search is what you are looking for.

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.

Data binding directly to a store query

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()

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);

How to write a filter condition when using a dataview

I have a datatable which contains all these records (see SQL query). Instead of writing a new stored procedure, how can I filter this condition in my datatable using dataview?
SELECT *
FROM students
WHERE class = '10'
AND Names IN ('kiran', 'manju', 'ram' , 'peter')
AND Language = 'english'
If you use the SQL in your question to create a new view, you could then run other queries against that view and have that filtering done automatically:
CREATE VIEW MYVIEW AS
SELECT * FROM students WHERE class='10'
AND Names IN ('kiran', 'manju', 'ram' , 'peter')
AND Language = 'english'
There is a nice article how to filter data using dataview there.
you would have to play with the RowFilter property of the Dataview.
private void MakeDataView()
{
DataView view = new DataView();
view.Table = DataSet1.Tables["Suppliers"];
view.AllowDelete = true;
view.AllowEdit = true;
view.AllowNew = true;
view.RowFilter = "City = 'Berlin'";
view.RowStateFilter = DataViewRowState.ModifiedCurrent;
view.Sort = "CompanyName DESC";
// Simple-bind to a TextBox control
Text1.DataBindings.Add("Text", view, "CompanyName");
}
Note: You have to note since the filtering and sorting would be done on client side it might perform really poorly if you have too much rows.

Resources