1

I have a Windows application and need to send some data to a remote server. I send the data but it sends only one value, so I need to read a table from SQL Server and send all values in that table at once.

The code to send data:

 private void SaveUser()
 {
        CN.conn = CN.connection;

        CN.cmd = new SqlCommand("SELECT [FULLNAME]  FROM STUDENTS", CN.conn);

        CN.conn.Close();
        CN.conn.Open();

        CN.dr = CN.cmd.ExecuteReader();

        while (CN.dr.Read())
        {
            try
            {    
                 using (var wb = new WebClient())
                 {
                     var data = new NameValueCollection();

                     data["latitude"] = CN.dr[0].ToString();

                     var response = wb.UploadValues("https://urbanwaste.000webhostapp.com/user/pickup.php", "POST", data);
                     string responseInString = Encoding.UTF8.GetString(response);
                 }
            }
            catch (Exception ex)
            {                  
                MessageBox.Show(ex.Message);
            }

            MessageBox.Show("Successfully");
        }

        CN.conn.Close();
}

My database table:

Database table that I need to send

remote database server

Thank you

4
  • Have you tried with a JSON string? Commented Sep 29, 2018 at 10:35
  • No i'm not trying Commented Sep 29, 2018 at 10:38
  • Then you should for sure. Commented Sep 29, 2018 at 10:39
  • please tell some tutorials describing this way Commented Sep 29, 2018 at 10:41

1 Answer 1

2

Something like this - I did not build & run it so apologies if it isn't perfect but hopefully it will point you in the right direction

using Newtonsoft.Json;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;

private class DataToSendModel
{
    public string FullName { get; set; }
    public string OtherProperty1 { get; set; }
    public string OtherProperty2 { get; set; }
}

// async as I have used .NET Core and it is best practice, but you can remove async from everywhere if you want (I would suggest not)
public async Task SaveUser()
{
    // Always wrap in a `using` statement to endsure cleanup of connections and resources
    using (var conn = new SqlConnection(config.GetConnectionString("SomeConnectionStringConfigName")))
    using (var cmd = new SqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        // Consider a stored procedure for this
        cmd.CommandText = "SELECT FullName, OtherColumn1, OtherColumn2 FROM Students";

        conn.Open();

        using (var reader = await cmd.ExecuteReaderAsync())
            while (reader.Read())
            {
                var dataToSend = new DataToSendModel
                {
                    FullName = reader["FullName"].ToString(),
                    OtherProperty1 = reader["OtherColumn1"].ToString(),
                    OtherProperty2 = reader["OtherColumn2"].ToString()
                };

                // Don't use this, use HttpClient https://blog.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/
                //using (var wb = new WebClient())
                using (var httpClient = new HttpClient())
                {
                    var response = await httpClient.PostAsync("https://urbanwaste.000webhostapp.com/user/pickup.ph", new StringContent(JsonConvert.SerializeObject(dataToSend)));

                    response.EnsureSuccessStatusCode();

                    string content = await response.Content.ReadAsStringAsync();
                }
            }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I can't test it as I don't have access to your data or API, but the general gist is there and should help you get started. The key is to serialise an object and send that, which will mean your API will receive all properties
Thank you for your effort the problem is not this C# part, the problem is in the PHP part. Anyway Thank you @Rob king

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.