how to bind Combo Box using Stored Procedure in WPF & Entity Framework - combobox

I have a stored procedure
CREATE PROCEDURE GetFreeRooms
AS
BEGIN
SELECT [RoomId]
FROM [dbo].[tblRooms]
WHERE [RoomId] NOT IN (SELECT [RoomId]
FROM [dbo].[tblRoomsAndLevels]
WHERE [TermId] = (SELECT MAX([TermId])
FROM [dbo].[tblTerms]))
END
How can I bind combo box using it?
i use this code C#
dpRooms.ItemsSource = dbs.GetFreeRooms();
//dbs is my entity model
dpRooms.DisplayMemberPath = "RoomId";
and this on xaml
<ComboBox Name="dpRooms" ItemsSource="{Binding}" DisplayMemberPath="RoomId" Grid.Column="1"/>
but it doesn't work.
if i can't using this way, tell me entity query of this .please.
Sorry I can't speak English very well.

Related

Access Listbox query preventing SQL Server Update Query

I'm developing an Access application and a SQL Server backend simultaneously. I have a Form with a listbox which, when a record is double clicked, opens an unbound form and loads data into it based on the record selected. When changes are made in this second form, a button initiates a pass through query that executes a stored procedure updating the details of the record in the base table in SQL Server.
Here's the thing. As long as Form1 (with the listbox) is open, the stored procedure times out without running. If I close that form, it takes less than a second. It behaves this way when run from Access, when run from management studio, and when run in management studio as a query with hard values (not a sproc with parameters).
The row source for the listbox is a linked table that references a View in SQL Server. The query within the view is a recursive common table expression of two different tables, one of which is the table being edited by the sproc. I've set the view to read only. Is there another setting that I can do to help here?
Here's the stored procedure:
PROCEDURE [dbo].[spSalesPlanUpdate]
#Salesyear numeric(4,0),
#ItemNumber varchar(20),
#Baseline int,
#Speculation int,
#Comments varchar(max)
AS
declare #SY numeric(4,0),
#ItN varchar (20),
#BL int,
#SPL int,
#CmT varchar(max)
set #SY = #Salesyear
set #ItN = #ItemNumber
set #BL = #Baseline
set #SPL = #Speculation
set #CmT = #Comments
BEGIN
SET NOCOUNT ON;
update SalesPlan
set Baseline = #BL
,Speculation = #SPL
,DateModified = getdate()
,Comments = #CmT
where SalesYear = #SY and ItemNumber = #ItN
END
I used both parameters and local variables because at first I was thinking it might be about parameter sniffing.
Here's the view the listbox is queried from:
view [dbo].[vwSalesPlan] as
with cte
as
(
select Item, year(getdate()) as SY
from vwItemsAndLiners il
union all
select ial.Item,
(cte.SY + 1)
From vwItemsAndLiners ial join cte on ial.Item = cte.Item
Where SY < (year(getdate())+ial.YearsFromProp)
)
select sp.ItemNumber, ial.Variety, ial.Size, ial.PerTray, sp.SalesYear, sp.SalesYear - ial.YearsFromProp as PropYear,
sp.SalesYear - ial.YearsFromProduction as ProductionYear,
sp.Baseline, sp.Speculation,
CEILING((CAST(SP.BASELINE AS NUMERIC (12,2)) + CAST(SP.SPECULATION AS numeric(12,2)))/IAL.PerTray)*IAL.PerTray as Total ,
sp.DateModified, ial.Segment ,'Entered' as [Status], sp.Comments
From SalesPlan sp inner join vwItemsAndLiners ial on sp.ItemNumber = ial.Item
Where ial.status = 'Sell'
union
select cte.Item, ial.Variety, ial.Size, ial.PerTray, SY, cte.sy - ial.YearsFromProp as PropYear,
cte.SY - ial.YearsFromProduction as ProductionYear,'', '', 0, null, ial.Segment , 'Not Entered', null
from cte inner join vwItemsAndLiners ial on cte.Item = ial.Item
where cte.Item not in (select ItemNumber from SalesPlan where salesplan.SalesYear = CTE.SY) and ial.Status = 'Sell'
with check option
Table being updated: SalesPlan
View that the listbox is queried from: vwSalesPlan
I realize that there's a lot of stuff here. Really, I'm just hoping this generates some ideas of why a form being open would lock the original table from an update query. Thanks!
I tried:
Indexing the views in SQL Server that provide the rowsource for the listbox, but because they contain CTE's they cannot be indexed.
lstbox.recordset.movefirst then lstbox.recordset.movelast to force access to read the entire list, but whenever the list was filtered or requeried it would throw an error saying the recordset object had changed and the movefirst command was invalid.
So I wrote this sub:
Private Sub readtheData()
Dim i As Integer
i = Me.lstSalesPlan.ListCount
End Sub
Simply forcing it to count the records every time the form was loaded or the query behind the listbox was filtered forced access to release the lock. Hope this helps somebody down the road!

Displaying data from the database on a textbox in the presentation layer. Three tier architecture

Create procedure sp_getTotal
#InvoiceID varchar(50)
As
Begin
(Select Invoice.Total
from Invoice
where Invoice.InvoiceID = #InvoiceID)
End
I have created the above procedure to get the "total" from Invoice table in my database on SQL Server. I need to make this value be displayed on a textbox in the presentation layer on button click of an app created using three tier architecture. The procedure has already been passed into the Data Access Layer and the Business Logic Layer.
How do I code the button for this?
Thanks in advance for replies
Well, basically on the presentation layer, create a button, hook-up its click event.
Then, on the button click event, call your middle tier method.
This method will basically only call your data tier, which contacts the database and runs the stored procedure and maps the return to a proper int.
On the way back to the presentation, simply assign the returned value to the desired text box...
On the button click:
MyMiddleTierController lvl2 = new MyMiddleTierController();
myTextBox.Text = lvl2.GetInvoice("12345");
On the data tier:
//Open sql connection
//Map stored proc command to SqlCommand
using(SqlDataReader reader = myCommand.ExecuteReader()){
if(reader.Read())
return reader[0];
}

F# FSharp.Data.SqlClient not recognizing multiple return tables from Stored Procedure

I am not sure if this is possible but I have not been able to come across clear documentation for this use case. I am using F# 4 and the FSharp.Data.SqlClient library to connect to SQL Server 2016. I am wanting to call a stored procedure that returns multiple tables and turn those tables into the corresponding records. In this case the first table is made up of items and the second table is made up of customers.
My instinct is that it should look something like this:
let items, customers = cmd.Execute()
My gut is that items would be an IEnumerable<item> and customers would be an IEnumerable<customer> where item and customer are both Record types. What it appears is happening though is that FSharp.Data.SqlClient is only seeing the first returned table from the stored procedure. I am working on a SQL Server 2016 Developer instance. Here is the T-SQL to setup the example:
create table Item (
ItemID int identity(1, 1) primary key,
ItemName nvarchar(50)
)
go
create table Customer (
CustomerID int identity(1, 1) primary key,
CustomerName nvarchar(50)
)
go
insert into Item (ItemName) values ('A');
insert into Item (ItemName) values ('B');
insert into Item (ItemName) values ('C');
insert into Customer (CustomerName) values ('Gary');
insert into Customer (CustomerName) values ('Sergei');
insert into Customer (CustomerName) values ('Elise');
go
create procedure dbo.ExampleProcedure
as
begin
set nocount on;
select
ItemID,
ItemName
from Item
select
CustomerID,
CustomerName
from Customer
end;
And here is the F# script that I am testing with. It shows what I would like to be able to do but I get a compile error on the last line:
#r "../packages/FSharp.Data.SqlClient.1.8.2/lib/net40/FSharp.Data.SqlClient.dll"
#r "../packages/FSharp.Data.2.3.2/lib/net40/FSharp.Data.dll"
#r "System.Xml.Linq.dll"
open FSharp.Data
[<Literal>]
let connStr =
"Data Source=**connection string**;"
type queryExample = SqlProgrammabilityProvider<connStr>
do
use cmd = new queryExample.dbo.ExampleProcedure(connStr)
let items, customers = cmd.Execute()
I am wanting items to correspond to the first returned table and customers to correspond to the second returned table. The intellisense suggests that FSharp.Data.SqlClient is only seeing the first table. When I hover over cmd.Execute() the popup says "This expression was expected to have type 'a*'b but here has type System.Collections.Generic.IEnumerable<SqlProgrammabilityProvider<...>.dbo.ExampleProcedure.Record>". If I do the following I get access to the Items query in the stored procedure:
// Learn more about F# at http://fsharp.org. See the 'F# Tutorial' project
// for more guidance on F# programming.
#r "../packages/FSharp.Data.SqlClient.1.8.2/lib/net40/FSharp.Data.SqlClient.dll"
#r "../packages/FSharp.Data.2.3.2/lib/net40/FSharp.Data.dll"
#r "System.Xml.Linq.dll"
open FSharp.Data
[<Literal>]
let connStr =
"Data Source=**connection string**;"
type queryExample = SqlProgrammabilityProvider<connStr>
do
use cmd = new queryExample.dbo.ExampleProcedure(connStr)
for item in cmd.Execute() do
printfn "%A" item.ItemID
Is this even possible? Is my approach wrong? I could not find clear documentation on this use case but I thought it would be common enough it would be covered.
Update
Just to clarify what I am trying to achieve I am showing how I solve this in C#. In C# I create a DataSet object and populate it with the results of the Stored Procedure. From there I pick out the individual tables to work with. After extracting the tables I then use LINQ to transform the rows into the corresponding objects. It often looks something like the following:
using System.Data;
using System.Data.SqlClient;
var connStr = "**connection string**"
var sqlConnection = new SqlConnection(connStr );
var sqlCommand = new SqlCommand("ExampleProcedure", sqlConnection);
sqlCommand.CommandType = CommandType.StoredProcedure;
var dataSet = new DataSet();
var adapter = new SqlDataAdapter(sqlCommand);
adapter.Fill(dataSet);
var itemsTable = dataSet.Tables[0];
// Turn the itemsTable into a List<Item> using LINQ here
var customersTable = dataSet.Tables[1];
// Turn the customersTable into List<Customer> using LINQ here
I find this to be overly verbose for such a simple thing as extracting the individual tables but perhaps I am too sensitive to code clutter. I know that F# must have a more elegant and terse way to express this.
I don't know F#, however this is a data access problem.
When a stored procedure returns multiple resultsets, you need to access they in sequence, one by one.
cmd.ExecuteReader() returns an instance of a datareader pointing to the first resultset. You need to process this resultset, may be filling a list with instances of a custom class, than you call the method "NextResult" and you will have access to the next resultset and so on.
A reference for the method "NextResult": https://msdn.microsoft.com/pt-br/library/system.data.sqlclient.sqldatareader.nextresult(v=vs.110).aspx

SqlDataAdapter taking too long to Fill VB.NET SQL Server 2012

I'm working on a winform application and I'm using a table named [File] in my SQL Server database.
I have a form that views some of "[File]" fields fID and fName in a combobox named SearchName. fID for Value and fName for Display.
SearchName Combobox is bound to dataset with dataadapter filling table with fID, fName, fPhoneNumber, fBalance, so I can use fName and fID.
I have also textboxes to add new "File" data like : fName, fAge, fNationality,fSex with a Save button with another combobox showing something called "Source".
When User clicks Save the data is saved to table [File] In DB and the adapter is filled again.
The dataset tableadapter was using a stored procedure as the following:
create proc [dbo].[ReadFileData](#fid int,#filter varchar(20))
as
begin
declare #f varchar(20)=#filter;
declare #id int =#fid;
if(#id=-1)
begin
if(#f='All')
select fID,fName,fPhoneNumbers,fBalance from [File]
else
if(#f='Blocked')
select fID,fName,fNotes,fBalance,fBlockDate,uFullName
from [File],[User] where fBlocked='True' and fBlocker=[uID]
order by fBlockDate desc
else
if(#f='nonBlocked')
select fID,fName,fPhoneNumbers,fBalance from [File] where fBlocked='False'
else
if(#f='notReady')
select fID,fName,fPhoneNumbers,fBalance from [File] where fAllTestsOK='False' and fBlocked='False'
else
if(#f='Ready')
select fID,fName,fPhoneNumbers,fBalance from [File] where fAllTestsOK='True' and fBlocked='False'
else
if(#f='NegBalanced')
select fID,fName,fPhoneNumbers,fBalance from [File] where fBalance<0
end
else
select f.fID,fName,fSex,fBirthDate,fPhoneNumbers,fAddress,fNationality,fNotes,fBalance,fBlocked,(select uFullName from [User] where uid=f.fBlocker) as fBlocker,
fLastEdited,(select uFullName from [User] where [uID]=f.fEditor) as fEditor, fBlockDate from [File] f where fID=#fid
end
It was taking too much time to save and fill the combobox again. I searched over the internet and I found out the problem is called "Patamter Sniffing/spoofing", because my procedure was selecting fields based on the values of the parameter it receives. I tried different ways to solve it, but nothing worked out for me. (P.S. I am using the same SP on other forms and data is filled immediately with no problems).
I deleted the whole dataset and created a new one with a new dataadapter using new Stored Procedure, this:
create proc [dbo].[GetAllFiles]
as
begin
select fID,fName,fPhoneNumbers,fBalance from [File]
end
Now first time the save and fill is done in no time, but after that it takes like 10+ seconds to fill.
I want to know what can I do to continue using the dataadapter to fill the combobox and solve time consuming problem?
If you have any suspicions that might cause these kind of problems, please let me know.
What other code parts or even design pictures can I provide to make my problem clearer?
Thanks to #Plutonix. He wrote it in a reply, just to make it clearer.
"You should spend a few hours on MSDN. You do not need to requery/refill/rebuild a datatable when you add rows; they can be refreshed. – Plutonix"
I used DataAdapterName.Adapter.Update(DatasetName) in save button and other update places. And kept fill only in page load event.

How to create VIEW in MS Access Database using Delphi Application without installing MSAccess on PC?

I want to create VIEW definitions on MS Access. I have used following CREATE VIEW Statement:
SELECT
MFP.FollowUpPlan_Id,
MFP.FollowUpPlan_Name AS PlanName,
DFP.Sequence_No AS SequenceNo,
MFS.FollowUpSchedule_Name AS ScheduleName
FROM
MAS_FollowUp_Plan AS MFP,
DET_FollowUp_Plan AS DFP,
MAS_FollowUp_Schedule AS MFS
WHERE
(((MFP.FollowUpPlan_Id)=DFP.FollowUpPlan_Id) AND
((DFP.FollowUpSchedule_Id)=MFS.FollowUpSchedule_Id)) AND
MFP.is_Deleted = FALSE AND
DFP.is_Deleted = false
ORDER BY
MFP.FollowUpPlan_Id, DFP.Sequence_No;
but it throw an error:
Only Simple Select Queries are allowed in view.
Please Help, Thanks in Advance.
The issue here, as Jeroen explained, is a limitation of Access' CREATE VIEW statement. For this case, you can use CREATE PROCEDURE instead. It will create a new member of the db's QueryDefs collection --- so from the Access user interface will appear as a new named query.
The following statement worked for me using ADO from VBScript. From previous Delphi questions on here, my understanding is that Delphi can also use ADO, so I believe this should work for you, too.
CREATE PROCEDURE ViewSubstitute AS
SELECT
MFP.FollowUpPlan_Id,
MFP.FollowUpPlan_Name AS PlanName,
DFP.Sequence_No AS SequenceNo,
MFS.FollowUpSchedule_Name AS ScheduleName
FROM
(MAS_FollowUp_Plan AS MFP
INNER JOIN DET_FollowUp_Plan AS DFP
ON MFP.FollowUpPlan_Id = DFP.FollowUpPlan_Id)
INNER JOIN MAS_FollowUp_Schedule AS MFS
ON DFP.FollowUpSchedule_Id = MFS.FollowUpSchedule_Id
WHERE
MFP.is_Deleted=False AND DFP.is_Deleted=False
ORDER BY
MFP.FollowUpPlan_Id,
DFP.Sequence_No;
You cannot mix ORDER BY with JOIN when creating views in Access. It will get you the error "Only simple SELECT queries are allowed in VIEWS." (note the plural VIEWS)
Having multiple tables in the FROM is a kind of to JOIN.
either remove the ORDER BY,
or have only one table in the FROM and no JOINs.
I remember from the past (when I did more Access stuff than now) seeing this for a large query with a single table select with an ORDER BY as well.
The consensus is that you should not have ORDER BY in views anyway, so that is your best thing to do.
Another reason that you can get the same error message is if you add parameters or sub selects. Access does not like those in views either, but that is not the case in your view.
Declare variable olevarCatalog ,cmd as OleVariant in Delphi, Uses ComObj
olevarCatalog := CreateOleObject('ADOX.Catalog');
olevarCatalog.create(YourConnectionString); //This Will create MDB file.
// Using ADO Query(CREATE TABLE TABLEName....) add the required Tables.
// To Insert View Definition on MDB file.
cmd := CreateOleObject('ADODB.Command');
cmd.CommandType := cmdText;
cmd.CommandText := 'ANY Kind of SELECT Query(JOIN, OrderBy is also allowed)';
olevarCatalog.Views.Append('Name of View',cmd);
cmd := null;
This is a best way to Create MS ACCESS File(.MDB) and VIEWs using Delphi.

Resources