❔ ✅ Issue with data reading from database
Hello i am trying to find an animal(table) inside this function and if it exists delete it(picture 1). The issue is that it returns nothing even if i give valid argument (photo 2). Can anybody help?
60 Replies
mayby there is something what im don wrong. Or error is somwhere else?
the 2 round trips seem odd
why not try to delete it right away and use the affected row count to determine if it existed or not?
thats smart ill try
but it still does not explein why does it not work
coz when i try to delete animal with this id
it just does not read it
i'd step through with a debugger and see what state the datareader is in before the call to
.ReadAsync
yea still same issue
might be something wrong with int given as a value ofa paramiter
?
maybe, frankly i have never used the raw sql client because any level of ORM makes me cry less
is your table actually named
ANIMAL
in all caps? i'm not sure if sql server's table names are case sensitive or notSQL should be able to handle table names not in the exact case
do you have access to the SQL server to verify your transaction string? (switch it to Select * from instead of delete for testing)
I have also used command1.Parameters.AddWithValue("@ID", id) in cases like this where you know the type coming in to the function is the correct type to match with the database type
also, are you missing a commit?
then in the catch use a rollback
"
var sqlQuery = "delete from table";
using (var connection = new SqlConnection(ConnectionString))
{
await connection.OpenAsync();
using (var tran = connection.BeginTransaction())
using (var command = new SqlCommand(sqlQuery, connection, tran))
{
try {
await command.ExecuteNonQueryAsync();
} catch {
tran.Rollback();
throw;
}
tran.Commit();
}
}
"this throws 500
so it is a sewers problem?
serwers*
that just means it couldn't connect to your sql server, which could be for a few reasons
like the connection string being wrong, some network issue, or the server is down
wait my bad i did not turn on my vpn
yeah i can connect
this code is valid
it goes through it and returns same error
that the user does no exist
what "user?"
sorry animal
XD
Also, change your sqlQuery... right now it will delete all records from the Animals table
i know dont worry its usless base
it also may be easier to test this in isolation instead of having to send a request to your controller to trigger the code
i tried
on my local database same error
now the table values are
are you checking your database to see what the query is actually doing?
because you're generating the 400 yourself, which is another layer that could be wrong which is why it would be better to test the code on its own instead of in your full application
what happens if you just run the query manually as the user your program is connecting as?
as number lemme show you wait a sec
but it does not do anything becouse the delete instruction will not work
i want to see the actual response from your SQL server when you send the query, not in C#
ah ok
god dang it wait a sec xd
A
and the state of database:
in your query in the IDE you have a table of animal, in your C# code you have animals... try changing it to animal
it won't be able to find an animals table since that isn't the name.
this one...
ah i deleted it but i can take it back wait a sec
and I would double check your connection string with the database. Does your program have other functions that read from the database? Do they successfully read?
yes
i have update and show all animals
everythings works just delete wants to be a pain in the ass
xd
just verifying that there isn't an error with the connection
i can add get put but cannot delete
does the user you are using to log into the database have privileges to delete?
yea i thot about the same
but i could delete manually
right
here
thanks for help guys anyway i have no idea too why this does not work
coz everything i did earlier work so i need to fuck somtheing up
no idea what
in your delete method, can you change it to an update to set one of the strings? (Update animals set name = 'some value') to see if that transaction will go through? Also make sure to copy the connection string you are using from the read method to this one to prevent any spelling errors (and we know that one works)
same
without seeing the whole database and the other functions, I am at a loss...
do all your sqlStrings start with '@?
yes
sure here you go
this is working update
var sqlString = "SELECT * FROM Animal WHERE Id = @MyId"; <-- this string doesn't start with the '@
your image for the delete one shows @"Delete...
can you try it without the '@?
same
thanks for trying to help
im goin off for now if u or anybody else have an idea share (:
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.So guys
this was so stupid mistake
XD i marked id to get it from route
but i did not pass it in http delete
that's why i said this
when you're debugging you want to remove as many factors as possible
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.