C
C#16mo ago
Kubiszon

❔ ✅ 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
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
mayby there is something what im don wrong. Or error is somwhere else?
Jimmacle
Jimmacle16mo ago
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?
Kubiszon
Kubiszon16mo ago
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
Jimmacle
Jimmacle16mo ago
i'd step through with a debugger and see what state the datareader is in before the call to .ReadAsync
Kubiszon
Kubiszon16mo ago
yea still same issue
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
might be something wrong with int given as a value ofa paramiter ?
Jimmacle
Jimmacle16mo ago
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 not
Wisch10
Wisch1016mo ago
SQL 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(); } }"
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
this throws 500
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
so it is a sewers problem? serwers*
Jimmacle
Jimmacle16mo ago
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
Kubiszon
Kubiszon16mo ago
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
Kubiszon
Kubiszon16mo ago
Jimmacle
Jimmacle16mo ago
what "user?"
Kubiszon
Kubiszon16mo ago
sorry animal XD
Wisch10
Wisch1016mo ago
Also, change your sqlQuery... right now it will delete all records from the Animals table
Kubiszon
Kubiszon16mo ago
i know dont worry its usless base
Jimmacle
Jimmacle16mo ago
it also may be easier to test this in isolation instead of having to send a request to your controller to trigger the code
Kubiszon
Kubiszon16mo ago
i tried on my local database same error now the table values are
Kubiszon
Kubiszon16mo ago
Jimmacle
Jimmacle16mo ago
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?
Kubiszon
Kubiszon16mo ago
as number lemme show you wait a sec but it does not do anything becouse the delete instruction will not work
Jimmacle
Jimmacle16mo ago
i want to see the actual response from your SQL server when you send the query, not in C#
Kubiszon
Kubiszon16mo ago
ah ok god dang it wait a sec xd
Kubiszon
Kubiszon16mo ago
A
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
and the state of database:
Kubiszon
Kubiszon16mo ago
Wisch10
Wisch1016mo ago
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.
Kubiszon
Kubiszon16mo ago
Wisch10
Wisch1016mo ago
this one...
Kubiszon
Kubiszon16mo ago
ah i deleted it but i can take it back wait a sec
Wisch10
Wisch1016mo ago
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?
Kubiszon
Kubiszon16mo ago
yes i have update and show all animals everythings works just delete wants to be a pain in the ass xd
Wisch10
Wisch1016mo ago
just verifying that there isn't an error with the connection
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
i can add get put but cannot delete
Wisch10
Wisch1016mo ago
does the user you are using to log into the database have privileges to delete?
Kubiszon
Kubiszon16mo ago
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
Wisch10
Wisch1016mo ago
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)
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
same
Kubiszon
Kubiszon16mo ago
Wisch10
Wisch1016mo ago
without seeing the whole database and the other functions, I am at a loss... do all your sqlStrings start with '@?
Kubiszon
Kubiszon16mo ago
yes sure here you go
Kubiszon
Kubiszon16mo ago
Kubiszon
Kubiszon16mo ago
this is working update
Wisch10
Wisch1016mo ago
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 '@?
Kubiszon
Kubiszon16mo ago
same thanks for trying to help im goin off for now if u or anybody else have an idea share (:
Accord
Accord16mo ago
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.
Kubiszon
Kubiszon16mo ago
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
Kubiszon
Kubiszon16mo ago
Jimmacle
Jimmacle16mo ago
that's why i said this when you're debugging you want to remove as many factors as possible
Accord
Accord16mo ago
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.