C
C#2y ago
Korallis

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
TheRanger
TheRanger2y ago
html query? commonly we send json as the data
Korallis
KorallisOP2y ago
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.
TheRanger
TheRanger2y ago
ah XML
Korallis
KorallisOP2y ago
can you point me in the right direction ?
TheRanger
TheRanger2y ago
how should your html query look like?
TheRanger
TheRanger2y ago
and u want to build this url based on each row u fetched from the database?
Korallis
KorallisOP2y ago
yes and store the resposne
TheRanger
TheRanger2y ago
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
Korallis
KorallisOP2y ago
im a complete beginner so whatever the easiest approach is and is there a guide i can follow
TheRanger
TheRanger2y ago
well lets start with string interpolation do you know what a foreach is?
Korallis
KorallisOP2y ago
yes i use it in SQL abit its as it sounds. for every x do this.
TheRanger
TheRanger2y ago
foreach(var row in rows)
{
string url = $"https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth={row.DateOfBirth.ToString("dd/MM/yyyy")}&surname={row.surname}&hasAgreedTermsAndConditions={row.agreed}&organisationName={row.organisation}&employeeSurname=Lintern&employeeForename={row.employeeforname}";
}
foreach(var row in rows)
{
string url = $"https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth={row.DateOfBirth.ToString("dd/MM/yyyy")}&surname={row.surname}&hasAgreedTermsAndConditions={row.agreed}&organisationName={row.organisation}&employeeSurname=Lintern&employeeForename={row.employeeforname}";
}
this is a string interpolation, is how you append data to the string
Korallis
KorallisOP2y ago
so basically just a very complicated concatenate
TheRanger
TheRanger2y ago
its not complicated at all its very easy
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
string name = "John";
Console.WriteLine($"Hello {name}");
string name = "John";
Console.WriteLine($"Hello {name}");
Console Output
Hello John
Hello John
Compile: 616.409ms | Execution: 87.553ms | React with ❌ to remove this embed.
Korallis
KorallisOP2y ago
ohhh. thats cool
TheRanger
TheRanger2y ago
note the $ dollar sign has to be inserted before the beginning of the string or the string wont detect whats inside { } as a variable
Korallis
KorallisOP2y ago
yeah
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
string name = "John";
Console.WriteLine($"Hello {name}");
Console.WriteLine("Hello {name}");
string name = "John";
Console.WriteLine($"Hello {name}");
Console.WriteLine("Hello {name}");
Console Output
Hello John
Hello {name}
Hello John
Hello {name}
Compile: 583.570ms | Execution: 71.802ms | React with ❌ to remove this embed.
Korallis
KorallisOP2y ago
i get it 🙂
TheRanger
TheRanger2y ago
2- u can use a string builder
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString()
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString()
Result: string
https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth=27/03/1981&surname=mbugua
https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth=27/03/1981&surname=mbugua
Compile: 585.578ms | Execution: 23.868ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
looks more readable, right? there is also a class especially made to build url query but its in asp.net core
Korallis
KorallisOP2y ago
yeah V2 looks cleaner too but to build that string those sb.Append would need to be variable.
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
var qb = new Microsoft.AspNetCore.Http.Extensions.QueryBuilder();
qb.Add("dateOfBirth","20/03/1981");
qb.Add("surname","mbugua");
qb.ToString()
var qb = new Microsoft.AspNetCore.Http.Extensions.QueryBuilder();
qb.Add("dateOfBirth","20/03/1981");
qb.Add("surname","mbugua");
qb.ToString()
Result: string
?dateOfBirth=20%2F03%2F1981&surname=mbugua
?dateOfBirth=20%2F03%2F1981&surname=mbugua
Compile: 565.791ms | Execution: 31.842ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
V3 might be better for you, its especially made for it appends ? at the beginning and & between each query
Korallis
KorallisOP2y ago
v3
TheRanger
TheRanger2y ago
well yes u can do sb.Append($"&surname={row.surname}"); for V2 or qb.Add("surname",row.surname); for V3
Korallis
KorallisOP2y ago
ahh ok let me test that i need to install some more libraries 🙂 thanks for your help
TheRanger
TheRanger2y ago
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
Korallis
KorallisOP2y ago
no its for windows im using the .net windows form app
TheRanger
TheRanger2y ago
i see, so u cant use V3 but it aint hard to make one or there might be a way
Korallis
KorallisOP2y ago
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
c#
amespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}




}
}
}
c#
amespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}




}
}
}
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
const string url = "https://secure.crbonline.gov.uk/crsc/api/status/001536522000";
var param = new Dictionary<string, string>();
param.Add("dateOfBirth","20/03/1981");
param.Add("surname","mbugua");

var newUrl = url + "?" + String.Join("&", param.Select(x => x.Key + "=" + x.Value));
Console.WriteLine(newUrl.ToString());
const string url = "https://secure.crbonline.gov.uk/crsc/api/status/001536522000";
var param = new Dictionary<string, string>();
param.Add("dateOfBirth","20/03/1981");
param.Add("surname","mbugua");

var newUrl = url + "?" + String.Join("&", param.Select(x => x.Key + "=" + x.Value));
Console.WriteLine(newUrl.ToString());
Console Output
https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth=20/03/1981&surname=mbugua
https://secure.crbonline.gov.uk/crsc/api/status/001536522000?dateOfBirth=20/03/1981&surname=mbugua
Compile: 717.824ms | Execution: 113.063ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
are u using dapper to get data from database?
Korallis
KorallisOP2y ago
yeah shouldn't i be?
TheRanger
TheRanger2y ago
its fine but Entity framework is a thing you dont have to write a sql query there just linq methods
Korallis
KorallisOP2y ago
what the heck is a linq method 🙂 google time
TheRanger
TheRanger2y ago
Language Integrated Query for example .Select is one of the linq methods
Korallis
KorallisOP2y ago
so my code is very wrong lol
TheRanger
TheRanger2y ago
string join is well, combines data into a single string seperated by the character u specify
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
string[] array = {"a", "b", "c"};
string.Join(",", array)
string[] array = {"a", "b", "c"};
string.Join(",", array)
Result: string
a,b,c
a,b,c
Compile: 600.844ms | Execution: 33.817ms | React with ❌ to remove this embed.
Korallis
KorallisOP2y ago
i get that but how the hell do i combine that with SQL to pull from DB?
TheRanger
TheRanger2y ago
u dont have to do it now, u can learn entity framework later
Korallis
KorallisOP2y ago
i can see what you mean tho it would be alot cleaner.
Thinker
Thinker2y ago
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.
TheRanger
TheRanger2y ago
ud basically use the same code but just replace 20/03/1981and mbugua with row.DateOfBirth and row.surname
Korallis
KorallisOP2y ago
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.
Thinker
Thinker2y ago
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.
Korallis
KorallisOP2y ago
thats quite clever.
Thinker
Thinker2y ago
Almost completely removes the need for writing SQL manually
Korallis
KorallisOP2y ago
and also prevents injection issues. without having to use using all the time
Thinker
Thinker2y ago
yep Most people would recommend using it over Dapper or ADO.NET or whatever
TheRanger
TheRanger2y ago
well sql injection can be avoided by parameterized queries anyway
Korallis
KorallisOP2y ago
ok ive added the following
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString();
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString();
and it tells me unreachable code..
TheRanger
TheRanger2y ago
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 variable
Korallis
KorallisOP2y ago
namespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString();
}



}
}
}
namespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append("dateOfBirth=27/03/1981");
sb.Append("&surname=mbugua");
sb.ToString();
}



}
}
}
TheRanger
TheRanger2y ago
well yeah ur method ends its execution at return output;
Korallis
KorallisOP2y ago
i commented that out.
TheRanger
TheRanger2y ago
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
Korallis
KorallisOP2y ago
because i was following a guide on how to build an app that pulls data from a db and inserts it into a Datagrid
TheRanger
TheRanger2y ago
the GetPeople() method should be as it was you then build the query from what it returns
var people = GetPeople();

foreach(var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it
}
var people = GetPeople();

foreach(var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it
}
Korallis
KorallisOP2y ago
if i want to add that to field in the person output. called DBSLink how would i do that?
TheRanger
TheRanger2y ago
add what to field
Korallis
KorallisOP2y ago
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
TheRanger
TheRanger2y ago
seperate sql Queries?
Korallis
KorallisOP2y ago
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.
public class Person
{
public int id { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string DateOfBirth { get; set; }
public string CertificateNumber { get; set; }

public string CheckDate { get; set; }

public string CheckResult { get; set; }

public string DBSLink { get; set; }
public class Person
{
public int id { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string DateOfBirth { get; set; }
public string CertificateNumber { get; set; }

public string CheckDate { get; set; }

public string CheckResult { get; set; }

public string DBSLink { get; set; }
so the DBSLink is the output of the generated url
TheRanger
TheRanger2y ago
i see, thats easy
Korallis
KorallisOP2y ago
maybe for you i only just started learning this lol
TheRanger
TheRanger2y ago
edited code above u only need to add this person.DBSLink = url;
Korallis
KorallisOP2y ago
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
TheRanger
TheRanger2y ago
where is that?
Korallis
KorallisOP2y ago
thats your "DD/MM"YYYY" which sholdnt be needed
TheRanger
TheRanger2y ago
ah yeah ur DateOfBirth property isnt a type of DateTime i edited code so i removed it
Korallis
KorallisOP2y ago
i was about to type exactly what you put so i am learning lol
TheRanger
TheRanger2y ago
its a good practice to have the type of dates as DateTime, not string
Korallis
KorallisOP2y ago
thats good to know i know youre code is good but i keep getting unreachable code. which means im doing it wrong
TheRanger
TheRanger2y ago
where are u getting it?
Korallis
KorallisOP2y ago
at the very start of your code.
namespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}

var people = GetPeople();

foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it

}
}
}
}
namespace TestApp
{
public class DataAccess
{
public List<Person> GetPeople()
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(Helper.CnnVal("EclipseDB")))
{
var output = connection.Query<Person>($"Select DISTINCT(c.id), p.forename, p.surname, FORMAT (p.date_of_birth, 'dd/MM/yyyy ') as [DateofBirth], cc.reference [CertificateNumber] from can_candidate c JOIN gen_person p on p.id = c.person_id JOIN gen_static_codebook st on st.id = c.status_id JOIN can_compliance cc on cc.candidate_id = c.id and cc.deleted is null Where c.deleted is null and st.id = 585 and cc.name like 'Update Service' and cc.approved = 1 and not cc.reference is null").ToList();
return output;
}

var people = GetPeople();

foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it

}
}
}
}
TheRanger
TheRanger2y ago
this code
var people = GetPeople();

foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it

var people = GetPeople();

foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it

should not even be in this method
Korallis
KorallisOP2y ago
i need to make a new one
TheRanger
TheRanger2y ago
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 crashes
Korallis
KorallisOP2y ago
so if i create a new class this resolves that issue
TheRanger
TheRanger2y ago
what do u need a new class for?
Korallis
KorallisOP2y ago
im not saying i do i was asking a question. as ive learned quite alot so far lol
TheRanger
TheRanger2y ago
well it didnt look like a question to me
Korallis
KorallisOP2y ago
so if i create something like Public List <DBS> and then run that code thats what i was trying to ask
TheRanger
TheRanger2y ago
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
public static void GenerateDBSLinks(List<Person> people)
{
foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it
}
}
public static void GenerateDBSLinks(List<Person> people)
{
foreach (var person in people)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/001536522000?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}");
string url = sb.ToString();
person.DBSLink = url;
//use this url where u need it
}
}
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
int Sum(int a, int b)
{
int sum = a + b;
return sum;
Console.WriteLine("Print me");
}

int foo = Sum(3, 10);
Console.WriteLine(foo);
int Sum(int a, int b)
{
int sum = a + b;
return sum;
Console.WriteLine("Print me");
}

int foo = Sum(3, 10);
Console.WriteLine(foo);
Console Output
13
13
Compile: 641.892ms | Execution: 87.975ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
can you tell me whats wrong with this code
Korallis
KorallisOP2y ago
its doing the same thing nd only the bottom got done
TheRanger
TheRanger2y ago
what same thing
Korallis
KorallisOP2y ago
sorry im watching youtube tutorials too
TheRanger
TheRanger2y ago
what are u talking about forget tutorials on youtube the best place to learn the basics from is in links below $helloworld
Shinigami
Shinigami2y ago
Just a lurker, but I've learnt a lot from this discussion. Thanks guys 🙂
Korallis
KorallisOP2y ago
TheRanger should be a tutor. ive learned so much i have no errors but my Resultsgrid is not updaing with the URL..
TheRanger
TheRanger2y ago
$details
MODiX
MODiX2y ago
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)
Korallis
KorallisOP2y ago
$code
MODiX
MODiX2y ago
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/
Korallis
KorallisOP2y ago
code base here
Korallis
KorallisOP2y ago
BlazeBin - fghyqqvbuqsr
A tool for sharing your source code with the world!
Korallis
KorallisOP2y ago
it should simply build a URL and add it to DBSLink. and show on the Gridview. but the url does not show.
TheRanger
TheRanger2y ago
well u never called the GenerateDBSLinks and u forgot the & in sb.Append($"CertificateNumber={person.CertificateNumber}");
Korallis
KorallisOP2y ago
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
public static void API(List<Person> Candidate)
{ foreach (var person in Candidate)
{
var Client = new HttpClient();
var endpoint = new Uri(person.DBSLink);
var result = Client.GetAsync(endpoint).Result.ToString();
person.CheckResult = result;
public static void API(List<Person> Candidate)
{ foreach (var person in Candidate)
{
var Client = new HttpClient();
var endpoint = new Uri(person.DBSLink);
var result = Client.GetAsync(endpoint).Result.ToString();
person.CheckResult = result;
gives error 500 have no clue why if i copy and paste the url into chrome it returns an xml response?
TheRanger
TheRanger2y ago
what is?
Korallis
KorallisOP2y ago
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
TheRanger
TheRanger2y ago
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 open
TheRanger
TheRanger2y ago
you'll see something like
TheRanger
TheRanger2y ago
TheRanger
TheRanger2y ago
the browser sends Request Headers to the site did you need to login when you access the site?
Korallis
KorallisOP2y ago
No I latterly just copy and paste the url
TheRanger
TheRanger2y ago
ok so check the request headers, see what the browser sends to the site
Korallis
KorallisOP2y ago
Will do thanks again
public static void GDL(List<Person> Candidate)
{
foreach (var person in Candidate)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
}
}

//DBS Checking

static readonly HttpClient client = new HttpClient();
static async Task DbsChecking(string Disurl)

{
using HttpResponseMessage response = await client.GetAsync(Disurl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();

}
public static void GDL(List<Person> Candidate)
{
foreach (var person in Candidate)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
}
}

//DBS Checking

static readonly HttpClient client = new HttpClient();
static async Task DbsChecking(string Disurl)

{
using HttpResponseMessage response = await client.GetAsync(Disurl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();

}
how do i make responseBody fill out person.CheckResult ?
TheRanger
TheRanger2y ago
let ur DbsChecking method return the value of responseBody first
Korallis
KorallisOP2y ago
so create a new method to update CheckResult?
TheRanger
TheRanger2y ago
i didnt say create a new method you want to extract the value of responseBody from the method DbsChecking right?
Korallis
KorallisOP2y ago
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
TheRanger
TheRanger2y ago
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 method
Korallis
KorallisOP2y ago
yeah that makes sense but i still cant update person.CheckResult
TheRanger
TheRanger2y ago
just call the DbsChecking method to update person.CheckResult just like how you called await response.Content.ReadAsStringAsync(); to update responseBody
Korallis
KorallisOP2y ago
it doesnt see person.CheckResult thats the issue i thought i could just do responseBody = person.CheckResult but it doesnt allow it
TheRanger
TheRanger2y ago
how so? show a screenshot or something
Korallis
KorallisOP2y ago
TheRanger
TheRanger2y ago
do u know what unreachable code means? say you have something like this
Korallis
KorallisOP2y ago
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.
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
int Sum(int a, int b)
{
int total = a +b;
return total;
Console.WriteLine("PRINT MEEE");
}

int number = Sum(2, 3);
Console.WriteLine(number);
int Sum(int a, int b)
{
int total = a +b;
return total;
Console.WriteLine("PRINT MEEE");
}

int number = Sum(2, 3);
Console.WriteLine(number);
Console Output
5
5
Compile: 619.331ms | Execution: 78.484ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
tell me why PRINT MEE didnt get printed
Korallis
KorallisOP2y ago
cus its inside {} im guessing
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
int Sum(int a, int b)
{
int total = a +b;
Console.WriteLine("PRINT MEEE");
return total;
}

int number = Sum(2, 3);
Console.WriteLine(number);
int Sum(int a, int b)
{
int total = a +b;
Console.WriteLine("PRINT MEEE");
return total;
}

int number = Sum(2, 3);
Console.WriteLine(number);
Console Output
PRINT MEEE
5
PRINT MEEE
5
Compile: 634.659ms | Execution: 81.935ms | React with ❌ to remove this embed.
TheRanger
TheRanger2y ago
but then it got printed here, why?
Korallis
KorallisOP2y ago
honestly i dont know
TheRanger
TheRanger2y ago
do u know what int total = a +b; does?
Korallis
KorallisOP2y ago
there eactly the same accept the return is 1 line above returns the number value of A +b
TheRanger
TheRanger2y ago
but what is a and b?
int Sum(int a, int b) // a is 2, b is 3
{
int total = a + b; // 2 + 3 is 5, total is now 5
return total; // 5 returned to the method, thus method finished its execution, so it doesnt need to execute the PRINT MEE below
Console.WriteLine("PRINT MEEE");
}

// program starts here
int number = Sum(2, 3); // calls the Sum Method, 2 + 3 is 5
Console.WriteLine(number); // prints 5
int Sum(int a, int b) // a is 2, b is 3
{
int total = a + b; // 2 + 3 is 5, total is now 5
return total; // 5 returned to the method, thus method finished its execution, so it doesnt need to execute the PRINT MEE below
Console.WriteLine("PRINT MEEE");
}

// program starts here
int number = Sum(2, 3); // calls the Sum Method, 2 + 3 is 5
Console.WriteLine(number); // prints 5
Korallis
KorallisOP2y ago
but where did you declare that a -2 and b is 3? at the bottom
TheRanger
TheRanger2y ago
u cant see it?
TheRanger
TheRanger2y ago
Korallis
KorallisOP2y ago
Yeah I clocked it
TheRanger
TheRanger2y ago
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 using
Korallis
KorallisOP2y ago
The 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
TheRanger
TheRanger2y ago
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
Korallis
KorallisOP2y ago
i get what your saying Return ends the method. i get that
TheRanger
TheRanger2y ago
and returns the value too just like how Sum returned 5
Korallis
KorallisOP2y ago
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'
TheRanger
TheRanger2y ago
why do that when u can use the method itself to get the value
Korallis
KorallisOP2y ago
oh. i didnt think of that. can a gridview have multiple datasources?
TheRanger
TheRanger2y ago
person.CheckResult = await DbsChecking(Disurl);
Korallis
KorallisOP2y ago
doing that. you get person does not exist in current context
TheRanger
TheRanger2y ago
first, the variable ur looking for is person, not Person where did u put it
Korallis
KorallisOP2y ago
i put in the DBS checking method at first then moved it to the GDL method.
TheRanger
TheRanger2y ago
?
TheRanger
TheRanger2y ago
Korallis
KorallisOP2y ago
//Create the URL for DBS Check
public static void GDL(List<Person> Candidate)
{
foreach (var person in Candidate)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
person.CheckResult = await DbsChecking(Disurl);

}
}

//DBS Checking

static readonly HttpClient client = new HttpClient();
static async Task<string> DbsChecking(string Disurl)

{
using HttpResponseMessage response = await client.GetAsync(Disurl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();


// somehow add responseBody = person.CheckResult i Think i need a foreach statement but i cant quite get it right.
}

}
}
//Create the URL for DBS Check
public static void GDL(List<Person> Candidate)
{
foreach (var person in Candidate)
{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
person.CheckResult = await DbsChecking(Disurl);

}
}

//DBS Checking

static readonly HttpClient client = new HttpClient();
static async Task<string> DbsChecking(string Disurl)

{
using HttpResponseMessage response = await client.GetAsync(Disurl);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();


// somehow add responseBody = person.CheckResult i Think i need a foreach statement but i cant quite get it right.
}

}
}
TheRanger
TheRanger2y ago
look at the image
Korallis
KorallisOP2y ago
hense why i put it under person.DBSLink
TheRanger
TheRanger2y ago
yeah correct it is only defined in the foreach, and only the body of the foreach can see it
Korallis
KorallisOP2y ago
yeah but Await can only be used in Async
TheRanger
TheRanger2y ago
make ur method async then
Korallis
KorallisOP2y ago
i cant change the Static void to an async wihtout breaking the url builder.
TheRanger
TheRanger2y ago
what do u mean?
Korallis
KorallisOP2y ago
i cant simply change Public Static Void to static Async
TheRanger
TheRanger2y ago
u can public static async Task GDL(List<Person> Candidate)
Korallis
KorallisOP2y ago
i had task in lower case lol can i ask why person.CheckResult = await DbsChecking(Disurl); and not
person.CheckResult = await DbsChecking(responseBody);
person.CheckResult = await DbsChecking(responseBody);
TheRanger
TheRanger2y ago
because responseBody is only defined in the DbsChecking method and only visible there
Korallis
KorallisOP2y ago
ahh thatnks Damn.. you really are good how do i add an if error set as ""?
TheRanger
TheRanger2y ago
what do u mean
Korallis
KorallisOP2y ago
say the api errors and doesnt give a response.
TheRanger
TheRanger2y ago
$details
MODiX
MODiX2y ago
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)
TheRanger
TheRanger2y ago
what kind of error does it give you i believe i told you something yesterday but it seems you ignored it
Korallis
KorallisOP2y ago
no i fixed that it seemed to go down the list of candidates and give me the resposne.
TheRanger
TheRanger2y ago
the request headers?
Korallis
KorallisOP2y ago
but then one candidate has a invalid name and it seems to jsut simply stop
TheRanger
TheRanger2y ago
i see so u want to skip that candidate?
Korallis
KorallisOP2y ago
yeah
TheRanger
TheRanger2y ago
then ud need to learn the try catch pattern
foreach (var person in Candidate)
{
try{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
person.CheckResult = await DbsChecking(Disurl);
}catch(Exception e)
{
person.CheckResult = "I errored";
}
}
foreach (var person in Candidate)
{
try{
var sb = new StringBuilder("https://secure.crbonline.gov.uk/crsc/api/status/");
sb.Append($"{person.CertificateNumber}?");
sb.Append($"dateOfBirth={person.DateOfBirth}");
sb.Append($"&surname={person.Surname}&");
sb.Append("hasAgreedTermsAndConditions=true&");
sb.Append("organisationName=iCare24Group&");
sb.Append("employeeSurname=Lintern&");
sb.Append("employeeForename=Scott");
string Disurl = sb.ToString();
person.DBSLink = Disurl;
person.CheckResult = await DbsChecking(Disurl);
}catch(Exception e)
{
person.CheckResult = "I errored";
}
}
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
Korallis
KorallisOP2y ago
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
TheRanger
TheRanger2y ago
u mean refresh the datagrid?
Korallis
KorallisOP2y ago
yeah
TheRanger
TheRanger2y ago
re assign the datasource
Korallis
KorallisOP2y ago
can you time it to refresh every x secconds?
TheRanger
TheRanger2y ago
yea
TheRanger
TheRanger2y ago
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.
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