Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi im having a error with my database updating code.The error says "must declare the scalar variable @numara"
Here is the code that i had error with (This code belongs to my form.cs):
C#
IslemVeritabani ekleme = new IslemVeritabani();<
ekleme.GuncelleIslem("update tbl_temeldb  set ogrenci_adi='" + ad.Text + "',ogrenci_soyadi='" + soyad.Text + "',ders1='" + dersler.SelectedItem + "',ders2='" + dersler.SelectedItem + "',ders3='" + dersler.SelectedItem + "',ders4='" + dersler.SelectedItem + "',ders5='" + dersler.SelectedItem + "',ilk_dersin_ogretmeni='" + ogretmenler.SelectedItem + "',ikinci_dersin_ogretmeni='" + ogretmenler2.SelectedItem + "',ucuncu_dersin_ogretmeni='" + ogretmenler3.SelectedItem + "',dorduncu_dersin_ogretmeni='" + ogretmenler4.SelectedItem + "',besinci_dersin_ogretmeni='" + ogretmenler5.SelectedItem + "',notu='" + int.Parse(not.Text) + "'where id=@numara");
           ekleme.komut.Parameters.AddWithValue("@numara", dataGridView1.CurrentRow.Cells[0].Value.ToString());
            listeleme();
            MessageBox.Show("Kayıt Başarıyla Güncellenmiştir.");


Here is the problem code(Compiler shows the error at this code section) :
C#
class IslemVeritabani : TemelVeritabani
public void GuncelleIslem(string sorgu)
{
    Baglan();
    SqlCommand cmd = new SqlCommand(sorgu);
    cmd.Connection = baglanti;
    cmd.ExecuteNonQuery();
    baglanti.Close();
    baglanti.Dispose();
}


What I have tried:

I tried everything I know but i couldn't solve this problem
Posted
Updated 6-Jun-20 21:39pm
v2

1 solution

Don't do it like that! 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 the parameter you are using - @numara - has to be added before you call ExecuteNonQuery, not after - which is why you are getting the error.

Don't try to create a method to do the DB stuff - Create the Connection inside a using block, create the Command object inside another using block, and always use parameterised queries. Trying to get a "generic" method to work with parameters is a PITA - so use inline code!
 
Share this answer
 
Comments
OriginalGriff 6-Jun-20 15:17pm    
No, you didn't.
You call GuncelleIslem and pass it the concatenated SQL command string, and it calls ExecuteNonQuery.

public void GuncelleIslem(string sorgu)
{
Baglan();
SqlCommand cmd = new SqlCommand(sorgu);
cmd.Connection = baglanti;
cmd.ExecuteNonQuery();
baglanti.Close();
baglanti.Dispose();
}

When it returns, you add the parameter value, then call listeleme
Member 14855516 6-Jun-20 15:22pm    
Hi thanks but i still dont understand and i feel like im fool can you just write the correct code for me that need to be changed please ?
OriginalGriff 6-Jun-20 15:27pm    
Read what I said above: don't - EVER - concatenate strings to build the command; always use parameterised queries. Dump the Guncellelslem method completely, and inline the code with "using" blocks. Pass all values as parameters

In addition, check your user inputs using int.TryParse etc., and pass the converted value as a parameter only if they are all correct.
Member 14855516 6-Jun-20 15:33pm    
I'm sorry but I dont know how to use "using" blocks I dont even know if its a code or not.Can you just write it for me if its not too long.I only understand that I need to remove GuncelleIslem completely that's all.
Member 14855516 6-Jun-20 15:36pm    
I didn't use inline code because my teacher want us to call update methods from a class

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