I just start using apex from few time. So I hope you'll forgive me if I ask things that are very simple.
I have a select list populated by a SQL Query, my SQL instruction is SELECT NUM, ID FROM TABLE.
I'd like to change the query dynamically adding a " WHERE NUM LIKE %myVar%", where "MyVar" is the test of a text Item, so I'd like to change the content of the select list pressing the button.
Is it possible?
thanks in advance for any answer.
I find a partial solution. I bind to select list a PL/SQL function returning a SQL Script and I add an text item called filter.
My function is:
declare
q varchar2(4000);
begin
q:='select numero, ';
q:=q||'id from t_doc ';
q:=q||'where numero = :FILTROPT';
return q;
end;
But if I use like with a percent in the function instead of "=", apex raise me an error.
Any suggestion?
I got stored function which returns query. In ASP project I got GridView which I bind to SqlDataSource element (it named SqlDataProjectWells).
So, when I try to call it there are error appears which says something like
"error while trying to execute query"
But in pgAdmin select command works perfectly
Code calls on page load
void bindToTable(){
SqlDataProjectWells.SelectCommand = "SELECT get_zonetable()";
SqlDataProjectWells.Select(DataSourceSelectArguments.Empty);
myGridView.DataBind();
}
Stored procedure code
CREATE OR REPLACE FUNCTION get_ZoneTable()
RETURNS SETOF RECORD
AS
$$
BEGIN
return QUERY SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "WELLS" LEFT JOIN "ZONES" ON "WELLS".well_id = "ZONES".id_well;
/*RETURN;*/
--return result_record;
END
$$ LANGUAGE plpgsql;
Whats wrong?!
UPD:
If I use stored procedure - there are such error appears
ERROR [0A000] Error while executing the query
If I use select like this
SqlDataProjectWells.SelectCommand = "SELECT \"WELLS\".well_name, \"ZONES\".id_zones, \"ZONES\".top, \"ZONES\".botom FROM \"WELLS\" LEFT JOIN \"ZONES\" ON \"WELLS\".well_id = \"ZONES\".id_well;"
It caused error which says
A field or property with the name 'id_well' was not found on the
selected data source
Shouldn't you be calling DataBind on the GridView instead of the SqlDataSource?
Well, kinda solved problem this way. Stored procedure creates all_zones view which I use aftel in ASP code like SqlDataProjectWells.SelectCommand = "SELECT * FROM all_zones";
Stored procedure text
CREATE OR REPLACE FUNCTION get_zones_view()
RETURNS void AS
$BODY$
DECLARE
BEGIN
execute 'CREATE OR REPLACE VIEW "all_zones" AS SELECT "WELLS".well_name, "ZONES".id_zones, "ZONES".top, "ZONES".botom FROM "ZONES" JOIN "WELLS" ON "WELLS".well_id = "ZONES".id_well';
END;
$BODY$
LANGUAGE plpgsql
but I assume that it's not okay to do like that
Dim Application = From AL In db.AnnualLeave _
Where AL.Approval <> True _
Select LeaveID, EmpID, Name
GridView3.DataSource = Application
GridView3.DataBind()
after calling `GridView3.DataBind(), why do i still get
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.
at this line of code GridView3.Columns(1).Visible = False yet the grid has rows and more than 2 columns. i found a thread about similar problem here http://forums.asp.net/t/1025678.aspx/1
Note that the Gridview columns have NOT been defined at design time.
`
You need to be careful where you place your code in asp.net. If you placed that GridView3.Columns(1).Visible = False
code in the wrong place at the wrong time then yes, it would throw an error.
I suggest reading up on ASP.NET Page Lifecyle
I'm trying to make an insertion from one database called suspension to the table called Notification in the ANimals database. My stored procedure is this:
ALTER PROCEDURE [dbo].[spCreateNotification]
-- Add the parameters for the stored procedure here
#notRecID int,
#notName nvarchar(50),
#notRecStatus nvarchar(1),
#notAdded smalldatetime,
#notByWho int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
INSERT INTO Animals.dbo.Notification
(
NotRecID,
NotName,
NotRecStatus,
NotAdded,
NotByWho
)
values (#notRecID, #notName, #notRecStatus, #notAdded, #notByWho);
END
The null inserting is to replenish one column that otherwise will not be filled, I've tried different ways, like using also the names for the columns after the name of the table and then only indicate in values the fields I've got. I know it is not a problem of the stored procedure because I executed it from the sql server management studio and it works introducing the parameters. Then I guess the problem must be in the repository when I call the stored procedure:
public void createNotification(Notification not)
{
try
{
DB.spCreateNotification(not.NotRecID, not.NotName, not.NotRecStatus,
(DateTime)not.NotAdded, (int)not.NotByWho);
}
catch
{
return;
}
}
And I call the method here:
public void createNotifications(IList<TemporalNotification> notifications)
{
foreach (var TNot in notifications)
{
var ts = RepositoryService._suspension.getTemporalSuspensionForNotificationID(TNot.TNotRecID);
Notification notification = new Notification();
if (ts.Count != 0)
{
notification.NotName = TNot.TNotName;
notification.NotRecID = TNot.TNotRecID;
notification.NotRecStatus = TNot.TNotRecStatus;
notification.NotAdded = TNot.TNotAdded;
notification.NotByWho = TNot.TNotByWho;
if (TNot.TNotToReplace != 0)
{
var suspensions = RepositoryService._suspension.getSuspensionsAttached((int)TNot.TNotToReplace);
foreach (var sus in suspensions)
{
sus.CtsEndDate = TNot.TNotAdded;
sus.CtsEndNotRecID = TNot.TNotRecID;
DB.spModifySuspensionWhenNotificationIsReplaced((int)TNot.TNotToReplace, (int)sus.CtsEndNotRecID, (DateTime) sus.CtsEndDate);
}
DB.spReplaceNotification((int)TNot.TNotToReplace, DateTime.Now);
createNotification(notification);
}
else
{
createNotification(notification);
}
}
}
deleteTemporalNotifications(notifications);
}
It does not record the value in the database. I've been debugging and getting mad about this, because it works when I execute it manually, but not when I automatize the proccess in my application. Does anyone see anything wrong with my code?
Thank you
EDIT: Added more code. It still doesn't work changing that, I mean, the procedure works if I execute it, so I don't know what could be the error. In fact, I don't get any error. Could it be a matter of writin in a table that is not in the database where you have your stored procedure?
I would specify your column names and DONT incude the NULL at all for that column. Just let SQL Server deal with it.
INSERT INTO Animals.dbo.Notification
(
RecID,
[Name],
RecStatus,
Added,
ByWho
)
values (#notRecID, #notName, #notRecStatus, #notAdded, #notByWho);
Run profiler when you try to run it from the application and see what values it realy is sending. That will tell you if the application is creating the correct exec statment to exec the proc.
Also it may be a permissions problem.
Specify your column names:
INSERT INTO Animals.dbo.Notification
(RecID, Name, RecStatus, Added, ByWho)
VALUES
(#notRecID, #notName, #notRecStatus, #notAdded, #notByWho);
"Could it be a matter of writin in a table that is not in the database where you have your stored procedure?"
That may be the problem. You could try adding the "WITH EXECUTE AS OWNER" clause to your stored procedure so that it executes as the owner of the stored procedure. Or grant write permissions for the executing user to the table.
http://msdn.microsoft.com/en-us/library/ms188354.aspx
ok, I finally found out what noone realized lol. It was a very stupid error but got me really mad till I found the problem. It wasn't a problem of permissions, the problem was that I was not executing the procedure from my application, so where I wrote this:
DB.spCreateNotification(not.NotRecID, not.NotName, not.NotRecStatus,
(DateTime)not.NotAdded, (int)not.NotByWho);
When I had to write:
DB.spCreateNotification(not.NotRecID, not.NotName, not.NotRecStatus,
(DateTime)not.NotAdded, (int)not.NotByWho).Execute();
so as you see I was focusing my efforts in much more complex things and I wasn't even executing it...lol.
Thank you all for your answers anyway:)
I am using Linq to Sql with Predicate Builder and am trying to optimize how much information is retrieved from the database. I would like to select only certain fields to display them in a gridview. When I select only what I want, the search parameters I add (see below) don't work, and neither does PredicateBuilder. Here's what I'm currently doing (that works, but gets EVERYTHING which is way too much info)
' Initial Setup '
Dim db As New MyDataContext()
Dim results = From p In db.Products _
Select p
' Search '
If (testCase) Then
results = results.Where(Function(p) p.SomeAttribute = 123)
End If
If I change that to only select what I need, like this:
Dim results = From p In db.Products _
Select p.Name, p.SomethingElse
then I've noticed if the information is selected (ie I select p.SomeAttribute) then I can search (add the where clause) on that attribute, but if its not, I can't. And with predicate builder it only works if I select the entire item (ie select p). All this should be doing is creating SQL statements which don't have to select the attribute to search by it. How can I get this to work and select only what I need, but search by anything and keep prediate builder working? Any help MUCH APPRECIATED! Thanks
You could try to initially do a "select p" at the beginning, then add all your where clauses, and at the very end, select just what you need from it.
' Initial Setup '
Dim db As New MyDataContext()
Dim results = From p In db.Products _
Select p
' Search '
If (testCase) Then
results = results.Where(Function(p) p.SomeAttribute = 123)
End If
' trim down the columns after you've added the wheres...
Dim results2 = from p in results
Select p.Name, p.SomethingElse
You can't modify the "select list" (this is how I understood your question. I might have misunderstood it) with predicate builder (which builds boolean expressions). You should manually use stuff in System.Linq.Expressions namespace to do that but I suggest using Dynamic LINQ instead.
Sounds like you are doing the where on the projection and not the original Product. Do the projection Select p.Name, p.SomethingElse at the end after all the search criteria has been applied.