SQL overwrites instead off adds
public bool AddNewCustomer(CustomerClass customer)
{
SqlCommand command = new SqlCommand();
command.CommandText = string.Format("INSERT INTO tblCustomer VALUES('{0}', '{1}', '{2}', '{3}', '{4}', '{5}')", customer.CTitle, customer.CForename, customer.CSurname, customer.CEmail, customer.CContactNum1, customer.CContactNum2);
return ExecuteNonQuery(command);
}
so when i run this it adds a new customer just fine, i close the programme open it again add new customer it does it but replaces the previously made customer, i have a near identical prodject and run this same sql and it works perfectly anyone know if its a visual studio problem or is their a fix?
19 Replies
Holy SQL injection
SQL injection aint a problem its just a school project
Could it be the database you're using is not persistent?
If it's a school project, I would fail it on the grounds of SQL injection 😛
its only alevel we dont learn about that stuff 😅
waht does not persistant mean
Means it gets deleted between individual runs of the application
It's not saved anywhere on disk, exists only while the app runs
that sounds probable how would i go about fixing that because it used to work
You'd use a persistent database instead
First step would be to figure out what database you're using, though
ive got chat gpt cooking probably shouldnt blindly copy it but we'll see what happens
idk what happened but if you want some resolution it kinda works, i have the same problem with other queries that doesnt work but i manually added customers and then ran the system to see if it would do anything and it works, idk why it was getting tripped up and idk if adding things manually will fix the others but thanks anyway
an
INSERT
literally cannot remove dataI don't see anywhere in your code where you actually connect to a data source that would actually house this information. Usually you do something like:
That SqlConnection piece being the actual part that connects to a database, which would run your SqlCommand
return ExecuteNonQuery(command);
this is odd
i guess there's another method in that class that may handle the connection?Yeah I thought that returned an int
look closely
Oh private method
it's called
ExecuteNonQuery
but it's defined in their own class and takes the SqlCommand as a parameter
:LUL:I guess I need more of the implementation and table schema. Do you have a non-incrementing primary key? If so, is CTitle the primary key?
maybe more detail as to how you're determining that it is "replacing" old data
for all we know it's a bug on the read side that doesn't account for duplicates
Idk if it can even update data in the same row if the primary key is the same, but that's all I can think of with what I've been given.
an insert will never update, it will give you an error if you try to insert a row with a PK that already exists
basically, the original description of the problem can't possibly be right and there's something else going on