0

I read lots of topics and tried many solutions, but it is not working for me, to insert my data to the database.

public static void Feltoltes(string szo_var, string szotagolva_var)
{
    string query = "";
    using (var conn = new SqlConnection(connectionString))
    {
        conn.Open();
        using (var command = new SqlCommand(query, conn))
        {
            command.Parameters.Clear();
            command.CommandText = "INSERT INTO Szavak (Szo,Szotagolva) VALUES ('@szo','@szotagolva')";
            command.Parameters.AddWithValue("@szo", szo_var);
            command.Parameters.AddWithValue("@szotagolva", szotagolva_var);
            System.Windows.Forms.MessageBox.Show(command.CommandText);
            command.ExecuteNonQuery();
        }
        conn.Close();
    }
}

This is my code. My connection string is the right one. If I insert to the database manually, than I can make SELECTs etc. Only the Insert is not working properly. It don't get any exception, looks like everything works, but nothing changes.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • What is this for? `('"+szo_var+"','"+ szotagolva_var+"')`? I am talking about the `+`. Why not just give it a value normally. – Syber Nov 17 '18 at 23:54
  • I see that you have used `DataDirectory`. So this can help you https://stackoverflow.com/a/31605045/2946329 – Salah Akbari Nov 18 '18 at 13:38

1 Answers1

1

Every thing looks OK except for the insert command text. Try the following:

command.CommandText = "INSERT INTO Szavak (Szo,Szotagolva) VALUES (@szo,@szotagolva)";

If you use single quates (') inside a SQL command text it will treat what is inside as a literal. And hence it cancels out your parameter designation @

DaniDev
  • 2,471
  • 2
  • 22
  • 29
  • The problem is that, if i use the command.Parameters.AddWithValue instead the +-s so like below, and not above, than my CommandText broke and looks like this: https://prnt.sc/ljozli Checked for szo_var and szotagolva_var, and those have the right strings that i need. – Bence Muzellák Nov 18 '18 at 00:16
  • This doesn't solve the problem since you didn't show how to deal with parameters. – JohnyL Nov 18 '18 at 07:51
  • @BenceMuzellák the alert you are displaying seems to be a browser alert? which would not replace the parameter values that happens on the server (back-end). what error message are you getting (?) are you executing in debug mode can you put a break point at command.ExecuteNonQuery(); and tell me if you. How are you sure the connection string is correct? – DaniDev Nov 19 '18 at 07:48
  • @JohnyL, I believe that Bence is already dealing with (adding) the Parameters propely, with his AddWithValue(.... – DaniDev Nov 19 '18 at 23:01