Im having a form with 2 buttons with 2 different events one sets an order to be ready the other to be declined. For my stringgrid i select records from database, and for the buttonClick events i create UPDATE query, with the advStringGrid.Selectedtext which value i put in the UPDATE query. But the problem is when i first click Ready button and after that i click Decline button,without closing form, then the string value remains only until first click. Perhaps theres an option to "immediately" or w/o closing form to get new value for advStringGrid.Selectedtext variable.
procedure TfTodoList.setRefusedbtnClick(Sender: TObject);
var
i: Integer;
begin
if (Sender = setRefusedbtn) and (State = 0) then
begin
qryExec.Sql.Text := 'Update KitchenOrderRow Set StatusID = 1' +
' Where (OrderId in (Select OrderId from KitchenOrderHeader ' +
' Where Invoice = ' + IntToStr(Orders[X].Invoice) +
')) and (InvRowId = ' + IntToStr(Orders[X].InvRowId) + ')';
advOrderGridClickCell(Sender, X, Y);
end
else if (State = 1) and (setRefusedbtn = Sender) then
begin
ProductName := advOrderGrid.SelectedText;
qryExec.Sql.Text := 'Update KitchenOrderRow Set StatusID = 1' +
' where productName = ''' + ProductName + ''' and StatusID = ' +
IntToStr(qryStatusStatusID.asInteger) + '';
State := 0;
// fk_lib.logisse(nil, qryExec.Sql.Text);
end;
qryExec.ExecSQL;
advOrderGrid.RemoveSelectedRows;
end;
If you update some values in dataset which are used in DbGrid, you should refresh the dataset connected to DbCrid. Close/Open form do this.
After execution of update query do this :
Grid.Datasorce.DataSet.Refresh;
or
Grid.Datasorce.DataSet.Close;
Grid.Datasorce.DataSet.Open;
Update:
If your grid is not connected directly with database through datalink(datasource) you can refresh it as execute the same procedure which is used to fill the grid with data.
Related
I am trying to extract the Name of a person out of my database, containing two tables, with only having the ID (primary key).
I am struggling to come up with a solution, although I do have notes that I've written and the logic seems to check out (to me at least).
if P1Score > P2Score
then winner := P1ID
else winner := P2ID
winner in tblGames = the ID of winner in tblPlayers
WinnerName := first name of Winner + surname of Winner in tblPlayers
So this is my logic, obviously it's missing a lot, but I can't seem to expand on it much more
I have the ID of the person from tblGames, but now I'm struggling to understand how to use that ID to extract the Name and Surname from my tblPlayers and assign it to a variable, so I can put it into the Winner Column of tblGames.
I have tried a few things using my own thought process, but I do not know enough about Delphi and databases to actually implement it correctly.
BEGIN
if (StrToInt(P1_score) - StrToInt(P2_score) = 0) then
Draw := True
else
Draw:= False;
if StrtoInt(P1_Score) > StrToInt(P2_Score) then
winnerID := P1_ID
else
winnerID := P2_ID;
with dmTournament do
begin
tblGames.Insert;
tblGames['Player1_Id'] := StrToInt(P1_ID);
tblGames['Player2_ID'] := StrToInt(P2_ID);
tblGames['Player1_score'] := StrToInt(P1_Score);
tblGames['Player2_Score'] := StrToInt(P2_Score);
tblGames['Draw'] := Draw;
tblGames['Winner'] := WinnerName; //How do I get WinnerName(?)
tblGames.Post;
end;
END;
You don't need to do a lot of code. You can let MySQL engine do job for you. Execute the following quires in same order they appeared. This will update the table data as you want.
/* 1: Set all games to draw */
UPDATE Games SET draw = true;
/* 2: Update when Player1 is winner :*/
UPDATE Games, Players SET
Games.draw = false,
Games.winner = CONCAT(Players.first_name, ' ', Players.last_name)
WHERE
(Games.player1_score > Games.player2_score) AND (Games.player1_id=Players.ID);
/* 3: Update when Player2 is winner */
UPDATE Games, Players SET
Games.draw = false,
Games.winner = CONCAT(Players.first_name, ' ', Players.last_name)
WHERE
(Games.player2_score > Games.player1_score) AND (Games.player2_id=Players.ID);
I'm working with a SQL Server stored procedure in Classic ASP with 3 parameters. I am attempting to find the recordcount, but it returns '-1'.
I saw a similar post, did what it suggested (check cursortype, and add 'set nocount on' in the stored procedure), but those changes did not impact the -1 recordcount.
Here is my code in the Classic ASP page, below.
strInterestName = request("InterestName")
strActiveDate = request("activedate")
strExpireDate = request("expiredate")
Set objCommandSec = CreateObject("ADODB.Command")
Set objRS = CreateObject("ADODB.RecordSet")
objRS.cursorlocation = 3
objRS.cursortype = adOpenStatic
With objCommandSec
Set .ActiveConnection = objConnection
.CommandText = "[01_cms_search_pg_select_news_items_4]"
.CommandType = 4
.Parameters.Append .CreateParameter("#InterestName",adVarChar,
adParamInput, 25)
.Parameters.Append .CreateParameter("#ActiveDate",adDate, adParamInput)
.Parameters.Append .CreateParameter("#ExpireDate",adDate,
adParamInput)
.Parameters("#InterestName") = strInterestName
.Parameters("#ActiveDate") = strActiveDate
.Parameters("#ExpireDate") = strExpireDate
set objRS =.Execute()
End With
Here is the code for the stored procedure, below:
ALTER PROCEDURE [dbo].[01_cms_search_pg_select_news_items_4]
#InterestName varchar(50),
#ActiveDate datetime,
#ExpireDate datetime
AS DECLARE #sql nvarchar(4000)
SELECT #sql = ' SELECT * ' +
' FROM news ' +
' WHERE ' +
' bulletin_display_indicator = ''true'' '+
' AND ' +
' website_homepg_display_indicator= ''false'' '
IF #InterestName is not null
SELECT #sql = #sql + ' AND (InterestName = #InterestName)
IF #ExpireDate is not null and #ExpireDate IS NOT NULL
SELECT #sql = #sql + ' AND (expiredate between #ActiveDate and #ExpireDate)
SELECT #sql = #sql + '; '
EXEC sp_executesql #sql, N'#InterestName varchar(50), #ActiveDate
DateTime, #ExpireDate DateTime',#InterestName, #ActiveDate,
#ExpireDate
I struggled with that for a while, then found something that works for me.
It's not pretty but it does the job.
Have your Stored Procedure return 2 recordsets: one with the table data you need followed by one with the recordcount:
SELECT <all_you_need> FROM <your_table> WHERE <your_arguments>
SELECT [myCount] = ##ROWCOUNT
Then, in your ASP file:
dim objRS, dataRS, countRS
Set objRS = CreateObject("ADODB.RecordSet")
Set dataRS = CreateObject("ADODB.RecordSet")
Set countRS = CreateObject("ADODB.RecordSet")
[your original call to the stored procedure]
set objRS =.Execute()
set dataRS = objRS
set countRS = objRS.nextrecordset()
countRS now contains a recordset with a single row and a single column named 'myCount' that you can query to get the recordcount.
dataRS contains your original dataset.
NOTE: if you don't need to know the recordcount BEFORE you process the dataset, you can simplify like this:
dim objRS, countRS
Set objRS = CreateObject("ADODB.RecordSet")
Set countRS = CreateObject("ADODB.RecordSet")
[your original call to the stored procedure]
set objRS =.Execute()
[process your dataset as you need to]
set countRS = objRS.nextrecordset()
I hope someone can me help again, regarding to my solved stored procedure problem
Is there any chance to make the operator ">" flexible?
Right now my stored procedure works very fine and I will get an eMail when the record count is bigger than the param Menge.
The code is this:
#Menge as int = 0,
#recordCount as int = 0,
set #MySQL = 'select #recordCount=count(2) from ' + #MyTable + ' where ' + #MyWhere
exec sp_execute #MySQL, N'#recordCount int OUTPUT', #recordCount=#recordCount OUTPUT
IF #recordCount > #Menge
begin
...
EXEC msdb.dbo.sp_send_dbmail
Now I want to make > flexible to get an e-mail when the record count is smaller than menge
I tried to declare the param but I don't know how to insert it into the if #recordcount > #Menge line :(
#OpInd as char(1) = null
I would call the stored procedure with
exec sp_eMail_Test3
#Menge = 0,
#eMail_TO = 'testuser#test.xx' ,
#eMail_Subject = 'test3 ',
#eMail_Body = 'Hallo, das ist ein Test',
#MyTable ='test' ,
#MyWhere = 'not [sys_completed] is null'
#OpInd = '<'
If I try IF #recordCount + #OpInd + #Menge then I get the error
An expression of non-boolean type specified in a context where a condition is expected, near 'begin'.
Hopefully you understand my Target and can help me.
If necessary I need to build a 2nd stored procedure :( one for "<" and one for ">"
Best regards Ralf
Just change your operand to <>
IF #recordCount <> #Menge
begin
...
EXEC msdb.dbo.sp_send_dbmail
There are only 3 possible operands you want to deal with. >, <, and =. Since you want an email if it's > or if it's < then just send the email when it's not =.
Another way would to repeat your code.
IF (#recordCount > #Menge or #recordCount < #Menge)
begin
...
EXEC msdb.dbo.sp_send_dbmail
If you are trying to make it either > or < then you can use DynamicSQL. Something like...
declare #sql varchar(max)
set #sql =
'IF ' + cast(#recordCount as varchar(16)) + ' ' + #OpInd + ' ' + cast(#Menge as varchar(16)) + '
begin
...
EXEC msdb.dbo.sp_send_dbmail'
EXEC(#sql)
When i try to search both column, there is no filter. i want to list 5 and 30.05.2016 in dbgrid.
adoquery1.Close;
adoquery1.SQL.CLEAR;
adoquery1.SQL.Add('select * FROM Sheet11 ');
adoquery1.SQL.Add('where field9 like :blm and field12 like :blm1');
adoquery1.Parameters.ParamByName('blm').Value:='5';
adoquery1.Parameters.ParamByName('blm1').Value:='30.05.2016';
adoquery1.Open;
So what is exactly the question there ?
Using parameters is right, one better not use string splicing due to all kinds of fragility and unexpected side effects - http://bobby-tables.com
And use proper data types!
adoquery1.SQL.Text := 'select * FROM Sheet11 ' +
'where ( field9 = :_p_field9 ) ' +
' and ( field12 = :_p_field12 )';
with adoquery1.Parameters do begin
ParamByName('_p_field9').AsInteger := 5;
ParamByName('_p_field12').AsDateTime := EncodeDate( 2016, 05, 30 );
end;
adoquery1.Open;
You've got a pretty bad SQL error. LIKE does not work for any types other than strings (CHAR, VARCHAR, etc.). It does not work for numbers or dates. You're looking for = instead, for exact matches, or BETWEEN if you want something between two values.
This should work for you:
adoquery1.Close;
adoquery1.SQL.CLEAR;
adoquery1.SQL.Add('select * FROM Sheet11 ');
adoquery1.SQL.Add('where field9 = :blm and field12 = :blm1');
adoquery1.Parameters.ParamByName('blm').Value:= 5; // Notice no quotes
adoquery1.Parameters.ParamByName('blm1').Value:= '2016-05-30'; // Note no extra quotes
adoquery1.Open;
Issue:
The database is not updating but I'm unsure where it drops.
I've used numerous ones in the past but I can't see why this is not working. If I could see how to run an error test on the stored procedure I suspect that would help.
Tests:
I pass the variables to a label, after the stored procedure request in the .aspx code to check the values exist (left in code)
Change int to varchar in the stored procedure
Query:
How do I fix this and then how can I run tests to find the issues in the future
Stored Procedure:
USE [DATABASE]
GO
/****** Object: StoredProcedure [dbo].[spChangeValue] Script Date: 11/08/2015 12:02:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
ALTER PROCEDURE [dbo].[spRiskRatingChange]
#ActionID int,
#EmployeeID int,
#NewRating varchar(10)
AS
DECLARE #DateChanged AS nvarchar(max)
SET #DateChanged = GETDATE()
DECLARE #OldRating AS nvarchar(max)
DECLARE #OldComments AS nvarchar(max)
DECLARE #EmployeeName AS nvarchar(max)
SET #OldRating =
(
SELECT OverallRiskCategory FROM TblAsbestos WHERE ID = #ActionID
)
SET #OldComments =
(
SELECT Comments FROM TblAsbestos WHERE ID = #ActionID
)
SET #EmployeeName =
(
SELECT UserFirstName + ' ' + UserSurname FROM SystemUsers WHERE ID = #EmployeeID
)
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
UPDATE TblAsbestos SET OverallRiskCategory = #NewRating, RiskCategory = #NewRating,
Comments= ('Rating changed from ' + RTRIM(#OldRating) + ' to '
+ RTRIM(#NewRating) + 'By ' + #EmployeeName + ' on ' + #DateChanged + ' -- ' + #OldComments)
WHERE ID = #ActionID
END
Code in .aspx.vb
Dim connection As SqlConnection
Dim command As New SqlCommand
Dim ds As New DataSet
Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("VINCI_SQL").ToString()
connection = New SqlConnection(ConnectionString1)
connection.Open()
With command
.Connection = connection
.CommandText = "spRiskRatingChange" 'include audit names
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
.Parameters.AddWithValue("#ActionID", Session("ActionID").ToString)
.Parameters.AddWithValue("#EmployeeID", Session.Item("EmployeeID").ToString)
.Parameters.AddWithValue("#NewRating", ddOverallRiskCategoryEdit.SelectedValue)
.ExecuteNonQuery()
lblErrorMessageRatings.Visible = True
lblErrorMessageRatings.Text = "Action ID: " & Session("ActionID").ToString
lblErrorMessageRatings.Text = lblErrorMessageRatings.Text & " EmployeeID: " & Session("EmployeeID").ToString
lblErrorMessageRatings.Text = lblErrorMessageRatings.Text & " NewRating: " & ddOverallRiskCategoryEdit.SelectedValue
Read items such as:
UPDATE Stored Procedure not Updating
Instead of
.Parameters.AddWithValue("#ActionID", Session("ActionID").ToString)
try
.Parameters.Add("#ActionId",SqlDbType.Int)
.Parameters("#ActionId").Value = CInt(Session("ActionId"))
I am assuming it is an int value you are passing
.Parameters.AddWithValue does an implicit type conversion and perhaps this is what is causing your problem