How do i create a html string that can add paramaters send that html request and store the response
i have a small app that pulls data from our SQL Database.
now what i need to do is build a html query that pulls data from each row and then sends that query and stores the response and loop for every row found
can anyone point me in the right direction as i only started a few days ago.
187 Replies
html query? commonly we send json as the data
maybe im mis understanding something then
https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/758977/Multiple_Status_Checking_Guide_V2.0_23112018.pdf
im trying to build an app that queries our database and then runs a query against that.. and stores the returned value.
i have the database query part working fine.
ah XML
can you point me in the right direction ?
how should your html query look like?
and u want to build this url based on each row u fetched from the database?
yes
and store the resposne
1- u can just concatenate the data with string interpolation
2- there is a query builder for Uri that you can use to construct your uri from a row
im a complete beginner
so whatever the easiest approach is and is there a guide i can follow
well lets start with string interpolation
do you know what a foreach is?
yes
i use it in SQL abit
its as it sounds. for every x do this.
this is a string interpolation, is how you append data to the string
so basically just a very complicated concatenate
its not complicated at all
its very easy
TheRanger#3357
REPL Result: Success
Console Output
Compile: 616.409ms | Execution: 87.553ms | React with ❌ to remove this embed.
ohhh.
thats cool
note the $ dollar sign has to be inserted before the beginning of the string
or the string wont detect whats inside { } as a variable
yeah
TheRanger#3357
REPL Result: Success
Console Output
Compile: 583.570ms | Execution: 71.802ms | React with ❌ to remove this embed.
i get it 🙂
2- u can use a string builder
TheRanger#3357
REPL Result: Success
Result: string
Compile: 585.578ms | Execution: 23.868ms | React with ❌ to remove this embed.
looks more readable, right?
there is also a class especially made to build url query but its in asp.net core
yeah V2 looks cleaner too
but to build that string those sb.Append would need to be variable.
TheRanger#3357
REPL Result: Success
Result: string
Compile: 565.791ms | Execution: 31.842ms | React with ❌ to remove this embed.
V3 might be better for you, its especially made for it
appends
?
at the beginning and &
between each queryv3
well yes u can do
sb.Append($"&surname={row.surname}");
for V2
or
qb.Add("surname",row.surname);
for V3ahh ok
let me test that
i need to install some more libraries 🙂
thanks for your help
what kind of project are u coding on, Console ?
V1 and V2 are built in
V3 well, needs asp net core, a Microsoft framework that helps you create websites
no its for windows im using the .net windows form app
i see, so u cant use V3
but it aint hard to make one
or there might be a way
i need that link to be appended to the grid view too
so V2 best option?
This C# is both hard and seriously enjoyable at the same time
TheRanger#3357
REPL Result: Success
Console Output
Compile: 717.824ms | Execution: 113.063ms | React with ❌ to remove this embed.
are u using dapper to get data from database?
yeah
shouldn't i be?
its fine but Entity framework is a thing
you dont have to write a sql query there
just linq methods
what the heck is a linq method 🙂
google time
Language Integrated Query
for example .Select is one of the linq methods
so my code is very wrong lol
string join is well, combines data into a single string seperated by the character u specify
TheRanger#3357
REPL Result: Success
Result: string
Compile: 600.844ms | Execution: 33.817ms | React with ❌ to remove this embed.
i get that but how the hell do i combine that with SQL to pull from DB?
u dont have to do it now, u can learn entity framework later
i can see what you mean tho it would be alot cleaner.
Essentially a set of methods which get translated by Entity Framework to SQL to allow you to write what looks like C# which works with a database.
ud basically use the same code but just replace
20/03/1981
and mbugua
with row.DateOfBirth
and row.surname
so its like a translation layer. but wouldnt i need to add the SQL database as a source and select the tables i wanted etc.
You can write like
dbContext.Items.Select(x => x.Name).ToArray()
and that becomes a select name from Items
and which then is turned into a C# array.thats quite clever.
Almost completely removes the need for writing SQL manually
and also prevents injection issues.
without having to use using all the time
yep
Most people would recommend using it over Dapper or ADO.NET or whatever
well sql injection can be avoided by parameterized queries anyway
ok ive added the following
and it tells me unreachable code..
show more code
and which line number that throws that
sb.ToString();
just returns a string and discards it, since u never assigned the value that it returned into a variablewell yeah
ur method ends its execution at
return output;
i commented that out.
so the code below it will never be reached
and?
why are u trying to build that string in that method anyway
build it where u want to use it
because i was following a guide on how to build an app that pulls data from a db and inserts it into a Datagrid
the GetPeople() method should be as it was
you then build the query from what it returns
if i want to add that to field in the person output. called DBSLink how would i do that?
add what to field
it doesn't matter i dont think i can do this the way i wanted. i need to scrap it all and start again.
i think i need to seperate the SQL queries and save each one as a string
then create a table thats a collection of those
seperate sql Queries?
so far i have a list output thats shown in a list
this list is then shown when you click a button and it runs the sql code.
i need to do that code above so it builds a url and also show that in my gridview.
so the DBSLink is the output of the generated url
i see, thats easy
maybe for you
i only just started learning this
lol
edited code above
u only need to add this
person.DBSLink = url;
Severity Code Description Project File Line Suppression State
Error CS1503 Argument 1: cannot convert from 'string' to 'System.IFormatProvider' TestApp C:\Users\leeba\source\repos\TestApp\TestApp\DataAccess.cs 31 Active
where is that?
thats your "DD/MM"YYYY"
which sholdnt be needed
ah yeah ur DateOfBirth property isnt a type of DateTime
i edited code so i removed it
i was about to type exactly what you put so i am learning lol
its a good practice to have the type of dates as
DateTime
, not string
thats good to know
i know youre code is good
but i keep getting unreachable code.
which means im doing it wrong
where are u getting it?
at the very start of your code.
this code
should not even be in this method
i need to make a new one
the reason ur getting unreachable code like i said earlier is
ur method ends its execution at
return output
even if you didnt get unreachable code
GetPeople method will keep calling itself, in infinite recursion
until ur program crashesso if i create a new class
this resolves that issue
what do u need a new class for?
im not saying i do
i was asking a question.
as ive learned quite alot so far lol
well it didnt look like a question to me
so if i create something like Public List <DBS>
and then run that code
thats what i was trying to ask
why? ur class Person also has a property called DBSLink
u can create a method
call it
AppendUrlToDBS
or something
that will take an argument of your list
TheRanger#3357
REPL Result: Success
Console Output
Compile: 641.892ms | Execution: 87.975ms | React with ❌ to remove this embed.
can you tell me whats wrong with this code
its doing the same thing
nd only the bottom got done
what same thing
sorry im watching youtube tutorials too
what are u talking about
forget tutorials on youtube the best place to learn the basics from is in links below $helloworld
Written interactive course https://learn.microsoft.com/en-us/users/dotnet/collections/yz26f8y64n7k07
Videos https://dotnet.microsoft.com/learn/videos
Just a lurker, but I've learnt a lot from this discussion. Thanks guys 🙂
TheRanger should be a tutor.
ive learned so much
i have no errors but my Resultsgrid is not updaing with the URL..
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/code base here
BlazeBin - fghyqqvbuqsr
A tool for sharing your source code with the world!
it should simply build a URL and add it to DBSLink. and show on the Gridview. but the url does not show.
well u never called the
GenerateDBSLinks
and u forgot the &
in sb.Append($"CertificateNumber={person.CertificateNumber}");
i think this is abit above my capability of learning.
i cant figure out how to get that url into my Gridview.
but thanks for the lessons so far.
actually it worked lol
i actually fixed it
@TheRanger Thank you so much
gives error 500
have no clue why if i copy and paste the url into chrome it returns an xml response?
what is?
I sorted ot mate only issue I have is my result is giving me error 500 no clue why
As copying Utley into browser gives Mr results
its not the same
the browser passes Headers along the way
you'd need to manually put the headers
try right clicking anywhere in the page in ur browser and click
inspect window
then click the network
window
and load the page while that window is openyou'll see something like
the browser sends Request Headers to the site
did you need to login when you access the site?
No
I latterly just copy and paste the url
ok so check the request headers, see what the browser sends to the site
Will do thanks again
how do i make responseBody fill out person.CheckResult ?
let ur DbsChecking method return the value of responseBody first
so create a new method to update CheckResult?
i didnt say create a new method
you want to extract the value of responseBody from the method DbsChecking right?
then i dont understand what your saying you say wait for the method to return responseBody. i thought thats what await Response.Content did
yes and update person.CheckResult
then return responseBody
await response.Content.ReadAsStringAsync();
yes returned the value and stored it in responseBody
now return the value that is stored in responseBody
to the methodyeah that makes sense
but i still cant update person.CheckResult
just call the DbsChecking method to update person.CheckResult
just like how you called
await response.Content.ReadAsStringAsync();
to update responseBody
it doesnt see person.CheckResult thats the issue
i thought i could just do responseBody = person.CheckResult but it doesnt allow it
how so?
show a screenshot or something
do u know what unreachable code means?
say you have something like this
the unreachable code means it cannot get to that specific bit of code. usually cus you have a mis placed {} lol thats my understanding of it.
TheRanger#3357
REPL Result: Success
Console Output
Compile: 619.331ms | Execution: 78.484ms | React with ❌ to remove this embed.
tell me why PRINT MEE didnt get printed
cus its inside {}
im guessing
TheRanger#3357
REPL Result: Success
Console Output
Compile: 634.659ms | Execution: 81.935ms | React with ❌ to remove this embed.
but then it got printed here, why?
honestly i dont know
do u know what
int total = a +b;
does?there eactly the same accept the return is 1 line above
returns the number value of A +b
but what is a and b?
but where did you declare that a -2 and b is 3?
at the bottom
u cant see it?
Yeah I clocked it
be advised
a
and b
are local variables
meaing only the method Sum has its own a and b
any other method that also has a and b, they are not the same variables
just like how everyone has their own definition of this is my computer
when i say my computer
, im refering to the computer im using, when u say my computer
, u are refering to the computer ur usingThe thing is tho in my code the thing I need to update is in a class not a method so I'd need to reference that list I. Order to update that variable
anyway u see what return does
the method ends execution, thus any code below it wont get executed
and this is why it says unreachable code detected
so fix ur thing
i get what your saying
Return ends the method. i get that
and returns the value too
just like how Sum returned 5
i get that but if i replace return with responseBody = Person.CheckResult; it still errors because it cant find the reference
An object reference is required for the non-static field, method, or property 'Person.CheckResult'
why do that when u can use the method itself to get the value
oh.
i didnt think of that.
can a gridview have multiple datasources?
person.CheckResult = await DbsChecking(Disurl);
doing that. you get person does not exist in current context
first, the variable ur looking for is
person
, not Person
where did u put iti put in the DBS checking method at first then moved it to the GDL method.
?
look at the image
hense why i put it under person.DBSLink
yeah correct
it is only defined in the foreach, and only the body of the foreach can see it
yeah but Await can only be used in Async
make ur method async then
i cant change the Static void to an async wihtout breaking the url builder.
what do u mean?
i cant simply change Public Static Void to static Async
u can
public static async Task GDL(List<Person> Candidate)
i had task in lower case lol
can i ask why person.CheckResult = await DbsChecking(Disurl);
and not
because
responseBody
is only defined in the DbsChecking method and only visible thereahh thatnks
Damn..
you really are good
how do i add an if error set as ""?
what do u mean
say the api errors and doesnt give a response.
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
what kind of error does it give you
i believe i told you something yesterday but it seems you ignored it
no i fixed that
it seemed to go down the list of candidates and give me the resposne.
the request headers?
but then one candidate has a invalid name and it seems to jsut simply stop
i see
so u want to skip that candidate?
yeah
then ud need to learn the try catch pattern
if any code in the try scope threw an error
the try will catch it and go to the catch scope immediatly
execute whatever logic u want to do there, then exits at the end of the scope
and then the program will continue looping to the next person
oh thats a usefull little thing
is there a way to force your app to "Refresh its screen"
as i keep having to click in things to see what happening
u mean refresh the datagrid?
yeah
re assign the datasource
can you time it to refresh every x secconds?
yea
u can use this class https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.timer?view=windowsdesktop-7.0
Timer Class (System.Windows.Forms)
Implements a timer that raises an event at user-defined intervals. This timer is optimized for use in Windows Forms applications and must be used in a window.
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.