Click here to Skip to main content
15,917,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a table with 3 column ( username , from , take ) what i looking for is making loop to sum number
i have one button in my form it is UPDATE button
C#
mycon.Open();
            string query = "UPDATE tbluser set take='" + txt1.text + "'";
            SqlDataAdapter sda = new SqlDataAdapter(query, mycon);
            sda.SelectCommand.ExecuteNonQuery();
            mycon.Close();

i want to make another button to make a sum or count number start with 0

the numbers in the example is the record numbers
that what i have
username . form . take
    test  . 0  . 2000
    test2 . 0  . 2000
    test3 . 0  . 2000

and that what i result what i looking for
username . form . take
     test    .   0  . 2000
     test2   . 2000 . 2000
     test3   . 4000 . 2000


i want in test sum ( 0 + 2000 ) and but the result for sum in (test2(from)) =(2000)

and in test2 ( 2000 + 2000 ) and but the result for sum in (test3(from)) =(4000)

What I have tried:

i'm new in brogramming and i search bout that im didn't find
Posted
Updated 25-Dec-18 22:38pm
v4
Comments
OriginalGriff 26-Dec-18 3:31am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
el_tot93 26-Dec-18 3:39am    
i update it i hope it will be easy now
OriginalGriff 26-Dec-18 3:54am    
No it still doesn't make a lot sense.
Perhaps if you show us input (I think you have that) and the output you want, and show us the code you have already?
Remember, we have no idea where your tabel comes from, how you are fetching it, or any real idea of what exactly you are trying to do!

I'm not trying to be annoying - it's just there are so many different ways that you might have got to this situation, that we can't say "do this" without a lot better idea of exactly what is going on. So stop trying to type as little as possible, and explain as if to your mother on the phone! :laugh:
el_tot93 26-Dec-18 4:04am    
:laugh: ok
el_tot93 26-Dec-18 4:18am    
what about now

1 solution

Quote:
what about now

Ooooo. No, no - not like that. I know you are a beginner, but, that's very dangerous! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
Fix that throughout your whole app before you try to move on ... or somebody is going to delete your DB for you ...

Then look at the problem - the easiest way to do it is to use a SUM OVER:
SQL
SELECT Username, SUM(Take) OVER (ORDER BY Username) AS Form, Take FROM tbluser



Quote:
i need a example for Parameterized queries to do like it i didn't understand what you try to till me

You haven't been told about parameterized queries? OMG.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
v2
Comments
el_tot93 26-Dec-18 5:25am    
i need a example for Parameterized queries to do like it i didn't understand what you try to till me
OriginalGriff 26-Dec-18 5:30am    
Answer updated
[no name] 26-Dec-18 5:34am    
That was a close call *lol*
el_tot93 26-Dec-18 5:48am    
using (SqlCommand cmd = new SqlCommand("SELECT Username, SUM(Take) OVER (ORDER BY Username) AS Form, Take FROM tbluser) VALUES (@C1)", con))
{
cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
cmd.ExecuteNonQuery();
}
OriginalGriff 26-Dec-18 5:55am    
No, don't just assemble SQL queries by guess work.
Think about the command you are trying to execute, and use the example as a "pattern".

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