Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm having trouble adding a new result to the database. I want to be result in the base to be the sum: z + ilo. when i check the variable "para" it gives the correct value. But there is still value in the database that was before.

sql = "select ilosc from zapasy  where login='" + Context.User.Identity.Name + "' and odmiana='" + odmiany.SelectedValue + "'";
                        SqlCommand cmq = new SqlCommand(sql, con);
                        con.Open();
                        int ilo = (int)cmq.ExecuteScalar();
                        
                        con.Close();    

                        sql = "Update zapasy set ilosc where login='" + Context.User.Identity.Name + "' and odmiana='" + odmiany.SelectedValue + "'";
                        con.Open();
                        SqlCommand cma = new SqlCommand(sql, con);
                        string y = TextBox1.Text;
                        int z;
                        z = int.Parse(y);

                        int para = z + ilo;
                        stani.Text = para.ToString();

                        cma.Parameters.AddWithValue("ilosc", para);
                     

                        con.Close(); // connection close 


What I have tried:

i try change the database query
Posted
Updated 6-Dec-20 4:25am

1 solution

Quote:
i try change the database query

That would be a very good idea. 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?

And when you've fixed that through your whole app, you can start looking at teh problem you have noticed: SQL Syntax.
SQL
UPDATE MyTable SET MyColumn = myValue WHERE MyOtherColumn = MyOtherValue



Quote:
Thank you, bu could you help me how use Parameterized queries? I am beginner programist and i don't have idea
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("UPDATE myTable SET myColumn1=@C1, myColumn2=@C2 WHERE Id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", id);
        com.Parameters.AddWithValue("@C1", myValueForColumn1);
        com.Parameters.AddWithValue("@C2", myValueForColumn2);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 
v2
Comments
Łukasz Szeszycki 6-Dec-20 10:42am    
Thank you, bu could you help me how use Parameterized queries? I am beginner programist and i don't have idea
Richard Deeming 7-Dec-20 8:26am    
Try something like this:
int z;
if (!int.TryParse(TextBox1.Text, out z))
{
    // TODO: Display an error to the user
    return;
}

using (var connection = new SqlConnection("..."))
using (var command = new SqlCommand("UPDATE zapasy SET ilosc = ilosc + @z OUTPUT inserted.ilosc WHERE login = @login And odmiana = @odmiana", connection))
{
    command.Parameters.AddWithValue("@z", z);
    command.Parameters.AddWithValue("@login", Context.User.Identity.Name);
    command.Parameters.AddWithValue("@odmiana", odmiany.SelectedValue);
    
    connection.Open();
    object result = command.ExecuteScalar();
    stani.Text = Convert.ToString(result);
}

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