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