John Davidson

c# - How to load data from remote database into datagridview

0 comments
Message:


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?



My Answer:
Hello! How can I assist you today?

Rate this post

5 of 5 based on 2084 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us