0

So for a school project i'm working on a C# application that can put information inside the Database so we can output it in out website.. And I wondering if it was possible to do use a online database for it, I just used this website for it: http://www.freesqldatabase.com/

Error: "Reading from the stream has failed" on adapter.fill(table)

This is the MySqlConnection string:

MySqlConnection connection = new MySqlConnection("datasource=sql11.freesqldatabase.com;port=3306;Initial Catalog='sql11174958';username=Username;password=Password");
            MySqlCommand command;

This is my c# code I used:

using MySql.Data.MySqlClient;
using MySql.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Database
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        MySqlConnection connection = new MySqlConnection("datasource=sql11.freesqldatabase.com;port=3306;Initial Catalog='sql11174958';username=Username;password=Password");
        MySqlCommand command;

        private void Form1_Load(object sender, EventArgs e)
        {
            populateDGV();
        }

        public void populateDGV()
        {
            // populate the datagridview
            string selectQuery = "SELECT * FROM park";
            DataTable table = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter(selectQuery, connection);
            adapter.Fill(table);
            dataGridView_USERS.DataSource = table;
        }

        private void dataGridView_USERS_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                txbParkID.Text = dataGridView_USERS.CurrentRow.Cells[0].Value.ToString();
                txbParkNaam.Text = dataGridView_USERS.CurrentRow.Cells[1].Value.ToString();
                txbLocatie.Text = dataGridView_USERS.CurrentRow.Cells[2].Value.ToString();
                txbOpeningsDagen.Text = dataGridView_USERS.CurrentRow.Cells[3].Value.ToString();
                txbPrijzen.Text = dataGridView_USERS.CurrentRow.Cells[4].Value.ToString();
                txbLeeftijden.Text = dataGridView_USERS.CurrentRow.Cells[5].Value.ToString();
                txbTags.Text = dataGridView_USERS.CurrentRow.Cells[6].Value.ToString();
                rtbBeschrijving.Text = dataGridView_USERS.CurrentRow.Cells[7].Value.ToString();
            }
            catch
            {
                MessageBox.Show("No cell selected.");
            }

        }

        public void openConnection()
        {
            if(connection.State == ConnectionState.Closed)
            {
                connection.Open();
            }
        }

        public void closeConnection()
        {
            if(connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }

        public void executeMyQuery(string query)
        {
            try
            {
                openConnection();
                command = new MySqlCommand(query,connection);

                if(command.ExecuteNonQuery() == 1)
                {
                    MessageBox.Show("Query Executed");
                }

                else
                {
                    MessageBox.Show("Query Not Executed");
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }finally
            {
                closeConnection();
            }
        }

        private void BTN_INSERT_Click(object sender, EventArgs e)
        {
            string insertQuery = "INSERT INTO park(ParkNaam, ParkLocatie, ParkOpeningsDagen, ParkOpeningsTijden, ParkPrijzen, ParkLeeftijden, ParkTags, ParkBeschrijving) VALUES('" +txbParkNaam.Text+ "','" +txbLocatie.Text+ "','" +txbOpeningsDagen.Text + "','" +txbOpeningsTijden.Text+ "','" +txbPrijzen.Text+ "','" +txbLeeftijden.Text+ "','" +txbTags.Text+ "','" +rtbBeschrijving.Text+ "')";
            executeMyQuery(insertQuery);
            populateDGV();
        }

        private void BTN_UPDATE_Click(object sender, EventArgs e)
        {
            string updateQuery = "UPDATE park SET ParkNaam='" +txbParkNaam.Text+ "',ParkLocatie='" +txbLocatie.Text+ "',ParkOpeningsDagen='" +txbOpeningsDagen.Text+ "',ParkOpeningsTijden='" +txbOpeningsTijden.Text+ "',ParkPrijzen='" +txbPrijzen.Text+ "',Parkleeftijden='" +txbLeeftijden.Text+ "',ParkTags='" +txbTags.Text+ "',ParkBeschrijving='" +rtbBeschrijving.Text+ "' WHERE ParkID =" + int.Parse(txbParkID.Text);
            executeMyQuery(updateQuery);
            populateDGV();
        }

        private void BTN_DELETE_Click(object sender, EventArgs e)
        {
            string deleteQuery = "DELETE FROM park WHERE ParkID = " + int.Parse(txbParkID.Text);
            executeMyQuery(deleteQuery);
            populateDGV();
        } 
    }
}
derloopkat
  • 6,232
  • 16
  • 38
  • 45
Ruitjes
  • 25
  • 2
  • 10

1 Answers1

0

I got the problem, you are opening the connection, but I don't see where you are closing them. Once you have opened a connection to MySQL, you should close them after use.

In this case connections are open and not closed in the remote server. As though the remote server is free, number of connections allowed is limited. You should close opened connections by contacting the MySQL service providers, then only the error will be removed and please close connections after use otherwise problem will occur again.

Biby Augustine
  • 425
  • 1
  • 3
  • 16
  • Got the same error message but somewhere else: https://i.gyazo.com/3221432becebd4264f078223e0450515.png – Ruitjes May 17 '17 at 11:11