C
C#13mo ago
jaix

❔ SQL Statement only returning one row

Only chest press gets returned, where as I need both
No description
No description
No description
76 Replies
jaix
jaixOP13mo ago
No description
arion
arion13mo ago
SELECT * FROM CustomExercises
SELECT * FROM CustomExercises
if you want only the exercise
SELECT Exercise FROM CustomExercises
SELECT Exercise FROM CustomExercises
TheRanger
TheRanger13mo ago
what is DatabaseUtils.ReadData ? never seen it around before we usually use Entity framework or Dapper and its not recommended to use List<object>
arion
arion13mo ago
Its also not recommended to use raw strings in a query like that
arion
arion13mo ago
Nick Chapsas
YouTube
"Your Code Has a SQL Injection!" | Code Cop #007
Use code GRPC20 and get 20% off the brand new "gRPC in .NET" course on Dometrain: https://dometrain.com/course/from-zero-to-hero-grpc-in-dotnet Become a Patreon and get special perks: https://www.patreon.com/nickchapsas Hello everybody, I'm Nick, and in this video, I'll show you what SQL Injection actually is and explain why people on LinkedIn...
Tvde1
Tvde113mo ago
that's quite a bad video to link it does not explain SQL injection
Jimmacle
Jimmacle13mo ago
oh no it's AskSQL there's a cursed function like that in all the work code i inherited anyway, you didn't share how you're adding this data to the database your current query looks for an exact username match, so if you accidentally added a space or something to one it won't return it if not that then something in your ReadData function is wrong
arion
arion13mo ago
explain "quite a bad" its pretty much 1:1 whats happening here (timestamp) and he explains it
Tvde1
Tvde113mo ago
the video is aimed toward people generically saying "this code is bad! sql injection!" when those people don't know the full code of the user, and don't explain SQL injection if username is not a user-inputted string, it's not SQL injection there are much better ways to explain what SQLi is, than showing this video
arion
arion13mo ago
It shows what its not, it shows what it is, it shows an example, it even says that if its not controllable its not injectable. It explains 2/3 of the explanation of:
SQL injection (SQLi) is a web security vulnerability that allows an attacker to interfere with the queries that an application makes to its database. This can allow an attacker to view data that they are not normally able to retrieve. This might include data that belongs to other users, or any other data that the application can access. In many cases, an attacker can modify or delete this data, causing persistent changes to the application's content or behavior. In some situations, an attacker can escalate a SQL injection attack to compromise the underlying server or other back-end infrastructure. It can also enable them to perform denial-of-service attacks.
It doesn't explain that last paragraph or demonstrate it He explained all of that (except the last paragraph), how is that a "quite a bad"? He says word for word "... if the user from an API post request or query string parameter is controlling this I they can pass down anything they want so what I can say for example if I wanted to attack this query is okay what if I say that where ID is one and ..." then proceeds to demonstrate injection, i'd say that does explain SQL injection, no?
jaix
jaixOP13mo ago
um so how do i fix my problem thats what it says but only returns one row
JakenVeina
JakenVeina13mo ago
what IS your problem?
SELECT Exercise
FROM CustomExercises
SELECT Exercise
FROM CustomExercises
only returns one row? That's because there's only one row in the CustomExercises table
Jimmacle
Jimmacle13mo ago
you didn't include all the code between making your query and getting your response DatabaseUtils.ReadData is a black box that none of us know anything about
jaix
jaixOP13mo ago
No description
jaix
jaixOP13mo ago
Sorry theres 2
JakenVeina
JakenVeina13mo ago
then that's how many rows that query is returning
Jimmacle
Jimmacle13mo ago
well, it's clear why you only get one row your ReadData method only ever reads the first row of the response reader.Read() advances the reader to the next row in the result, you aren't calling it in a loop in addition, you're pivoting the first row into your list which doesn't make sense
jaix
jaixOP13mo ago
ohhhhhhhhh okay thank you
jaix
jaixOP13mo ago
does this look any better 😭
No description
Jimmacle
Jimmacle13mo ago
the if shouldn't be there as it is now you're skipping the first record by calling Read twice before accessing the fields
jaix
jaixOP13mo ago
ahh okay thank you
TheRanger
TheRanger13mo ago
try to define a class instead of using List<object>
Jimmacle
Jimmacle13mo ago
also that you could end up with a method like List<T> ReadData<T>(string query, Func<DbDataReader, T> rowReader) if you want to keep it reusable or if you don't care to reinvent the wheel, use dapper or another ORM which does this kind of mapping for you
jaix
jaixOP13mo ago
No description
jaix
jaixOP13mo ago
im taking each row and inserting it into essentially a 3d list i think or thats what im trynna do
Jimmacle
Jimmacle13mo ago
a 2d list, but yes R is saying that's not a good idea, because it's safer to map your DB results to strongly typed models i can tell you copied and pasted that line from the example on the link, do you know why it's not working?
jaix
jaixOP13mo ago
oh wait cuz its getting the first and second item and their different data types?
Jimmacle
Jimmacle13mo ago
that line assumes your row matches that format as in the first column has an int and the second column has a string
jaix
jaixOP13mo ago
ohhh whats the best way about doing this, im fairly new to c# and just followed a guide
Jimmacle
Jimmacle13mo ago
the one that still had an if was basically it if you just want a List<List<object>>
jaix
jaixOP13mo ago
System.Collections.Generic.List`1[System.Object] how do i access the data in this
Jimmacle
Jimmacle13mo ago
it's a list
jaix
jaixOP13mo ago
No description
jaix
jaixOP13mo ago
o
Jimmacle
Jimmacle13mo ago
if you want to print the elements you have to loop over them or use string.Join or something but your elements are lists, so you'll have to loop over each of those lists too data_final should be List<List<object>> you really should never use object ever tbh if i was writing this i'd do something super generic like this extension method
static List<T> Query<T>(this IDbConnection db, string query, Func<IDataReader, T> rowMapper)
{
using var cmd = db.CreateCommand();
cmd.CommandText = query;
using var reader = cmd.ExecuteReader();
var result = new List<T>();
while (reader.Read())
result.Add(rowMapper(reader));
return result;
}
static List<T> Query<T>(this IDbConnection db, string query, Func<IDataReader, T> rowMapper)
{
using var cmd = db.CreateCommand();
cmd.CommandText = query;
using var reader = cmd.ExecuteReader();
var result = new List<T>();
while (reader.Read())
result.Add(rowMapper(reader));
return result;
}
but this has flaws too, like it doesn't support parameterized queries which you 100% need to use for anything involving user input
jaix
jaixOP13mo ago
i see ill try get it workin w that knowledge thanks i got the problem solved but i was wondering if you can help me w smthin else @Jimmacle i wanna make it so when a button is pressed in one form, it will create another button in another form
jaix
jaixOP13mo ago
When a custom exercise is created, it would add and isplay it on the add exercise list
No description
No description
TheRanger
TheRanger13mo ago
is that winforms or wpf?
jaix
jaixOP13mo ago
winforms
TheRanger
TheRanger13mo ago
first make the button you want to click fire a method
jaix
jaixOP13mo ago
No description
jaix
jaixOP13mo ago
where would i go from here 😭
TheRanger
TheRanger13mo ago
have a reference to the other opened form u want to access
jaix
jaixOP13mo ago
how do you create a refernce thats the part i cant rlly figure out
TheRanger
TheRanger13mo ago
all of the opened forms can be accessed in Application.Forms what is the class name of your other form?
jaix
jaixOP13mo ago
addexercise
TheRanger
TheRanger13mo ago
give it a name using the Property Name eg in its constructor Name = "foo"; then you can get its reference using Application.Forms["foo"];
var otherForm = (addexercise)Application.Forms["foo"];
var otherForm = (addexercise)Application.Forms["foo"];
or
var otherForm = Application.Forms.OfType<addexercise>().First();
var otherForm = Application.Forms.OfType<addexercise>().First();
can work too
jaix
jaixOP13mo ago
what does this do
TheRanger
TheRanger13mo ago
it gets the reference of ur form u want to open
jaix
jaixOP13mo ago
the forms already open i just want to add a button in the addexercise form when the create button in the customform is fired
TheRanger
TheRanger13mo ago
yes i know, but you dont have its reference
jaix
jaixOP13mo ago
oh
jaix
jaixOP13mo ago
No description
TheRanger
TheRanger13mo ago
hmm do u have a custom class called Application in your project or something?
jaix
jaixOP13mo ago
nah i dont
TheRanger
TheRanger13mo ago
press Ctrl + left click on Application
jaix
jaixOP13mo ago
No description
TheRanger
TheRanger13mo ago
oh its OpenForms not Forms
jaix
jaixOP13mo ago
oh yh there we go okay reference is made where do i go from here
TheRanger
TheRanger13mo ago
well define a method in that class and call it from the reference eg
otherForm.CreateButtonOnExerciseList();
otherForm.CreateButtonOnExerciseList();
then let that method programatically add the button to the list
jaix
jaixOP13mo ago
so create a method inside of the customexercise right
TheRanger
TheRanger13mo ago
u mean addexercise
jaix
jaixOP13mo ago
and then use the method in customexc
TheRanger
TheRanger13mo ago
if you dont know how to programatically add a button, look at your form's file name that ends with designer.cs it will give you some idea
jaix
jaixOP13mo ago
wouldnt it be better to use invoke like this
jaix
jaixOP13mo ago
No description
jcotton42
jcotton4213mo ago
do you know what Invoke does? oh wait that's an event not a control
jaix
jaixOP13mo ago
i used invoke to send data from one form to another surely that can work w what im trynna achieve, no?
TheRanger
TheRanger13mo ago
?
jcotton42
jcotton4213mo ago
I think jaix is referring to using events you could have one form subscribe to an event on the other form but you're not really adding much there
jaix
jaixOP13mo ago
yeah so i used invoke so that when a button is clicked on one form, it creates a panel on another form surely i can do that with what im trynna do but im so confused
jcotton42
jcotton4213mo ago
personally I don't think using events adds anything here also Invoke isn't special here, it's just a way to raise an event usually seen in the form of SomeEvent?.Invoke(), to not raise the event if it has no subscribers whereas SomeEvent() would throw an exception if it had no subscribers
JakenVeina
JakenVeina13mo ago
delegate() and delegate.Invoke() are semantically identical only difference is personal preference unless you want to do ?. like cotton said
Accord
Accord13mo 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