0

In C# I have 2 user controls, usercontrol1 contains a bunch of picturebox with flags representing each country and usercontrol2 has a datagridview.

What I want to do is when I click on a picturebox in usercontrol1 call the usercontrol2 with the data corresponding to the selected country loaded into the datagridview.

This is what I have so far for the connection to the Database:

        string url = "https:urltosomesite/filewithdata.php";

        var json = new WebClient().DownloadString(url);

        var m = JsonConvert.DeserializeObject<List<IPUS>>(json);

        dataGridView1.DataSource = m;

If I put this on the load of the usercontrol2 it connects to the db and shows the data correctly into the datagridview. However obvioulsy it loads all IP's. Here is the PHP code.

<?php

include "db_conn.php";

$query =  "SELECT ip, region, city, zip, provider FROM ip ORDER by region";
$result = mysqli_query($conn,$query);

$rows = array();
while($row = mysqli_fetch_array($result)) {
    $ip=$row['ip'];
    $region=$row['region'];
    $city=$row['city'];
    $zip=$row['zip'];
    $provider=$row['provider'];
    
    $rows[] = array('ip'=> $ip, 'region'=> $region, 'city'=> $city, 'zip'=> $zip,
                        'provider'=> $provider);

}
echo json_encode($rows);

Is worth mentioning that the database contains a row called "country" and in that row each country is called by its international code, for example United States is "US"

So basically what I want to do is if I click a picturebox with the American flag it should load the "US" data into the datagridview in usercontrol2. If I select the picturebox with the Italy flag it should only load the "IT" data and so on and so on.

How do I achieve that?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • There are a few bits to wire this up, and it's not clear precisely where you're stuck. Firstly you need to change the SQL so it has a `WHERE` clause so you can select a country. Then make it so it accepts the country as a parameter from the PHP, and make PHP retrieve that parameter from the querystring of the request (using $_GET). Then make the WebClient put the country ID parameter into the URL of the request. And lastly make C# listen to clicks on the pictureboxes and make a new request to the PHP with the relevant country ID in the request. – ADyson Nov 11 '20 at 21:21
  • Could you please give an example with code? Im still learning and Im kinda confused. – Dragonsan's Gameplays Nov 12 '20 at 14:23
  • Confused about which bit exactly though? What part(s) don't make sense to you? Break the problem down into the steps I've described. Each part is much easier to research on its own. It's essentially though quite a straightforward interaction using a HTTP request - client sends some data, server uses that data to influence the response it then sends back. I would start with the PHP side (because then you can test that functionality independently of the C# code (e.g. using an API testing tool such as Postman) and ensure it returns the JSON you expect, when you feed it a country code. – ADyson Nov 12 '20 at 14:31
  • For example, I modified the PHP code as you suggested to: SELECT ip, region, city, zip, provider FROM ipworld WHERE country = 'AE' ORDER by region and it returns the result correctly. Now in C#: Usercontrol1 (thats where the flags are) has in the AE flag (picturebox): `string url = "https://somesite/filewithdata.php"; var json = new WebClient().DownloadString(url); var m = JsonConvert.DeserializeObject>(json);` Im missing the part to call the other control (usercontrol2) and put the content of the var that has the json string in to the daragridview there. – Dragonsan's Gameplays Nov 12 '20 at 14:53
  • Well that's a good start. Now you need to replace the hard-coded 'AE' with a parameter which takes its value from a variable on the querystring - e.g. `$_GET["country"]`. Then, sending the request with a URL such as `https://titanvpn.network/data/ipworld.php?country=AE` would enable the client to control which country data is returned. – ADyson Nov 12 '20 at 14:59
  • Ty, will do that, but my main issue is still how to call the usercontrol and show the data on the other control into th datagrid view. How do I do that? – Dragonsan's Gameplays Nov 12 '20 at 15:17
  • Not sure what you mean by "call the user control". As I explained at the start what you need is to detect the event when the user clicks on a specific picture box – ADyson Nov 12 '20 at 15:55
  • I did this `string url = "somesite/filewithdata.php?country=AE"; var json = new WebClient().DownloadString(url); var m = JsonConvert.DeserializeObject>(json); usercontrol2 ip = new usercontrol2(); ip.dgv.DataSource = m;` and I got this error: System.NullReferenceException: Object reference not set to an instance of an object. I know I need to initialize the object but I dont know how. – Dragonsan's Gameplays Nov 12 '20 at 15:57
  • `new usercontrol2();` makes a new user control. You need to get a reference to the existing user control in the form, not make a new one (or just get a direct reference to the data grid. It's a bit hard to give specific advice btw when we can't see anything about your forms or controls in the question). – ADyson Nov 12 '20 at 15:59

0 Answers0