Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've tried to write some code to do the function mentioned on the title but with no success...

What possibilities i have with stored procedures, and what to know about using a value to search for a selected row on sql table?

Code snippet:

C#
if (Grid1.SelectedRows.Count > 0)
                    {
                        MySqlCommand cmd = new MySqlCommand("entradas_sai", cn);
                        cmd.CommandText = "entradas_sai";
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@retcode", MySqlDbType.Int32);
                        cmd.Parameters["@retcode"].Direction = ParameterDirection.Output;
                        cmd.Parameters.AddWithValue("@id_veiculo", Grid1.SelectedCells[0].RowIndex);
                        cmd.ExecuteNonQuery();
                        var res = cmd.Parameters["@retcode"].Value;
                    }



Stored procedure snippet:

SQL
CREATE DEFINER=`root`@`localhost` PROCEDURE `entradas_sai`(
IN ID_VEICULO VARCHAR(45), OUT retcode INT)
BEGIN
    DECLARE _rollback BOOL DEFAULT 0;
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
    START TRANSACTION;
	SELECT * FROM `entradas` WHERE id_veiculo= "@id_veiculo";
	INSERT INTO `entradas`(datasai, horasai) VALUES(date(now()) ,time(now()));
	UPDATE entradas SET SAI=1 WHERE id_veiculo="@idveiculo" AND SAI=0;
    IF '_rollback' THEN
        SET retcode = 0;
        ROLLBACK;
    ELSE
        SET retcode = 1;
        COMMIT;
    END IF;
END


What I have tried:

Tried this code i got but with no success...
Posted
Updated 8-Mar-16 21:48pm
Comments
Suvendu Shekhar Giri 8-Mar-16 5:01am    
Did you get any error on executing this?
Scribling Doodle 8-Mar-16 5:03am    
No error shown... It looks like it executes the command but doesn't insert the value into database... I don't really know what's the problem here...

You are passing in a value of "ID_VEICULO" but not using it anywhere.

If entradas.id_veiculo is an integer column then your line
SELECT * FROM 'entradas' WHERE id_veiculo= "@id_veiculo";
will generate an error. I'm not sure of the MySQL error but in SQL Server this would be
Quote:
Conversion failed when converting the varchar value 'asdkfjasldj' to data type int.

Because of the error the exception handler will be fired and _rollback will have been set to 0. Did you debug and check the return code from
C#
var res = cmd.Parameters["@retcode"].Value;


In your stored procedure avoid calling the parameters the same name as the column and actually use them rather than the string "@id_veiculo" ... I think it is unlikely that that string is actually how you are identifying every single vehicle in the table.

Try changing the stored procedure like this
SQL
CREATE DEFINER='root'@'localhost' PROCEDURE 'entradas_sai'(
IN IDTOCHECK VARCHAR(45), OUT retcode INT)
BEGIN
    DECLARE _rollback BOOL DEFAULT 0;
    DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET _rollback = 1;
    START TRANSACTION;
	-- NO point in this at all as you are not using the data
        --SELECT * FROM `entradas` WHERE id_veiculo= "@id_veiculo";
	INSERT INTO `entradas`(datasai, horasai) VALUES(date(now()) ,time(now()));
	UPDATE entradas SET SAI=1 WHERE id_veiculo=IDTOCHECK AND SAI=0;
    IF '_rollback' THEN
        SET retcode = 0;
        ROLLBACK;
    ELSE
        SET retcode = 1;
        COMMIT;
    END IF;
END

When problems happen with stored procedures try running parts of the sql directly in MySQL (or at a push use SQL Fiddle[^]). Then you would have spotted the problem in the SELECT.
Also learn to use the Visual Studio debugger - Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

As a general rule of thumb, if something doesn't do what you expected then check the values that are being returned ... in this case I believe res==0
 
Share this answer
 
v2
Comments
Scribling Doodle 8-Mar-16 6:17am    
The only problem is this seems to not show any errors... it wont just insert the values. Thats all :/
Scribling Doodle 8-Mar-16 6:19am    
the res var is just giving null values. Not 0 or 1.
Scribling Doodle 8-Mar-16 6:26am    
And this cmd.Parameters that i have were sent by you on another question, that's why i think you were right. And it worked well to insert all values where sai was equal to 0 and change them all to 1... But i want only to set the selected row to sai being equal to 1. And no, id_veiculo is not a int. It's a masked varchar45. It's made to insert the licenseplate. And it acts as an identifier for the vehicle. Not as an integer :)
CHill60 8-Mar-16 6:32am    
Have you tried running the query INSERT INTO `entradas`(datasai, horasai) VALUES(date(now()) ,time(now())); directly in MySQL - does it work?
Scribling Doodle 8-Mar-16 6:41am    
in mysql wb it retrieves the error 1046. I'm kinda tired of trying all the possible ways and none does the job...
I've tryied to use a store procedure to execute 2 querys, but with no success, so why not try to use the same query with 2 syntaxes? Yep, that's what i did and work out pretty well.

C#
using (var cn2 = new MySqlConnection("server=localhost;user id=root;password=12345;persistsecurityinfo=True;database=portaria;allowuservariables=True"))
            {
                
                string SQL2 = "INSERT INTO entradas(datasai, horasai) VALUES(@datasai, @horasai); UPDATE entradas SET sai = 1 WHERE id_entrada =@entrada";

                using (var cmd = new MySqlCommand(SQL2, cn2))
                {
                    cmd.Parameters.AddWithValue("@datasai", DateTime.Now.ToString("yyyy-MM-dd"));
                    cmd.Parameters.AddWithValue("@horasai", DateTime.Now.ToString("hh:mm:ss"));
                    cmd.Parameters.AddWithValue("@entrada", Grid1.CurrentRow.Cells["#"].Value);
                    MySqlDataAdapter sda = new MySqlDataAdapter();
                    DataTable dt = new DataTable();
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                    Grid1.Update();
                    Grid1.Refresh();
                    cn2.Open();
                }
                cn2.Close();
            }


With that in mind i'll explain what i did, i used 2 vars(one for the connection, and one to execute the query on the connection string). And then i used some parameters to insert data and one field to get the fields from the selected row with the index of column "#". After that i simply created the datatable and forced a fill command and a grid update/refresh to simply force the grid to show this new data inserted. Then i simply opened the connection and closed after the var was used.


There were no need in using a stored procedure. That's why i did this way. Simplier and easier. Thanks for all the help here @CHILL60! You were a great help ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900