0

I'm trying to create an application that can access a database, load the data and if the user adds any new fields, to save the data back into the database. As of right now, I am able to load the data into the application, but I am still unable to save any changes due to the error given above.

This is the save method

    private void Save_Click(object sender, RoutedEventArgs e)
    {
        dataSource.updateDataSet(dataSet);
    }

And this is the SimeDataSource file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using System.Data;

class SimpleDataSource : IDisposable
{
    MySqlConnection conn;
    MySqlDataAdapter adapter = new MySqlDataAdapter();

public SimpleDataSource(string server, string database, int port,
    string user, string password)
{
    Connect(server, database, port, user, password); 
}

public void Connect(string server, string database, int port,
    string user, string password)
{

    string connectionString = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password;
    try
    {
        conn = new MySqlConnection(connectionString);
        conn.Open();
    }
    catch (MySqlException e)
    {
        Console.WriteLine("Error: {0}", e.ToString());
    }
    finally
    {
        if (conn == null)
        {
            conn.Close();
        }
    }
}

public MySqlDataReader Query(string queryString)
{
    // TODO: Declares MySqlDataReader and MySqlCommand objects.
    // When the MySqlCommand object is executed with the query
    // string, the return value will be assigned to the MySqlDataReader
    // object. This object is then returned with the "return" keyword.
    try
    {
        MySqlCommand cmd = new MySqlCommand(queryString, conn);
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        Console.WriteLine("Error: {0}", e.ToString());
    }
    return null;
}

public void Update(string updateString)
{
    try
    {
        MySqlCommand cmd = new MySqlCommand(updateString, conn);
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        Console.WriteLine("Error: {0}", e.ToString());
    }
}

public void Dispose()
{
    if (conn != null)
        conn.Dispose();
}

public void QueryPreparedStatement(string queryString, Dictionary<string, string> values)
{
    try
    {
        MySqlCommand cmd = new MySqlCommand(queryString, conn);
        foreach (string key in values.Keys)
        {
            cmd.Parameters.AddWithValue("@" + key, values[key]);
        }
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException ex)
    {
        Console.WriteLine("Error: {0}", ex.ToString());
    }

}

public DataTable DataTableQuery(string sqlQuery)
{
    DataTable dt = new DataTable();
    MySqlCommand cmd = new MySqlCommand(sqlQuery, conn);
    MySqlDataAdapter adapter = new MySqlDataAdapter(cmd);
    adapter.SelectCommand = cmd;
    try
    {
        adapter.Fill(dt);
        return dt;
    }
    catch (MySqlException ex)
    {
        Console.WriteLine("Error: {0}", ex.ToString());
        return null;
    }

}

public static void updateDataSet(DataSet dataSet)
{
    try
    {
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        adapter.Update(dataSet);
    }
    catch (MySqlException ex)
    {
        Console.WriteLine("Error: {0}", ex.ToString());
    }
}

}

Edit:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Application_Development_Project
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private SimpleDataSource dataSource;
        private DataSet dataSet;

        public MainWindow()
        {
            InitializeComponent();
            dataSource = new SimpleDataSource
            ("*****","*****",*****,"*****","*****");
        }

        public void SimpleDataSource(string server, string database, int port, string user, string password)
        {
            string connectionString = "server=" + server + ";database=" + database + ";port=" + port + ";user=" + user + ";password=" + password;

        }

        private void Load_Click(object sender, RoutedEventArgs e)
        {
            DataTable dt = dataSource.DataTableQuery("SELECT * FROM Company");
            dataGrid.ItemsSource = dt.DefaultView;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            SimpleDataSource.updateDataSet(dataSet);
        }

    }
}
Ben Parry
  • 133
  • 1
  • 4
  • 19

1 Answers1

3

You've declared updateDataSet as public static void updateDataSet(DataSet dataSet).

Static means you call it on the Type, not an instance of that type (like the error says).

Not this: dataSource.updateDataSet(dataSet);

But this: SimpleDataSource.updateDataSet(dataSet);

Edit: You should also rename the SimpleDataSource method in your MainWindow to something else or the compiler will think you mean that method instead of the class.

Mark
  • 1,360
  • 1
  • 8
  • 14
  • Hi, thanks for the reply. I tried that earlier on and it states that the SimpleDataSource is a method which is not valid in the given context. – Ben Parry Feb 16 '16 at 13:35
  • 1
    `SimpleDataSource` has no accessor on it, so I think that it defaults to `internal`. Are the 2 classes in scope? – David Pilkington Feb 16 '16 at 13:37
  • @BenParry Can you post the whole code of the class that has the `Save_Click` method? – Mark Feb 16 '16 at 13:40
  • I think it's to do with my updateDataSet method, but I'm not sure what to do, since it's my first time implementing a database into an application – Ben Parry Feb 16 '16 at 13:46
  • You have a method named `SimpleDataSource` in your form that has the same name as the class `SimpleDataSource`. So when using `SimpleDataSource.updateDataSet` the compiler thinks you mean the _method_ and thus gives that error. You should rename `public void SimpleDataSource(string server, string database, int port, string user, string password)` in `MainWindow` to something else. – Mark Feb 16 '16 at 13:50