i’m trying to connect my gtk#2.0 app to a database. so theres a button, when the button clicked, it should fetching my database, but then an error appearing.error image. i’m using mariadb Ver 15.1 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
, monodevelop version 7.8.4 build 2
, mono Mono JIT compiler version 6.12.0.200 (tarball Tue Jul 11 21:32:10 UTC 2023)
heres my MainWindow.cs
<code>using System;
using Gtk;
using MySql.Data.MySqlClient;
public partial class MainWindow : Window
{
private Button btnFetchData;
private TextView textView;
public MainWindow() : base("GTK# MariaDB Fetch Example")
{
// Set window properties
SetDefaultSize(400, 300);
SetPosition(WindowPosition.Center);
// Create a button to fetch data
btnFetchData = new Button("Fetch Data");
btnFetchData.Clicked += OnButtonClicked;
// Create a TextView to display results
textView = new TextView();
// Use a vertical box to layout the button and textview
VBox vbox = new VBox();
vbox.PackStart(btnFetchData, false, false, 0);
vbox.PackStart(textView, true, true, 0);
Add(vbox);
// Show all UI elements
ShowAll();
}
private void OnButtonClicked(object sender, EventArgs e)
{
// Connect to MariaDB and fetch data
string connStr = "server=localhost;" + "port=3306;" + "user id=root; " + "password=halonamasayafauzy;" + "database=MonoPeople;" + "SslMode=none";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
string sql = "SELECT * FROM test";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
// Clear existing text
textView.Buffer.Clear();
// Display results in TextView
while (rdr.Read())
{
// Pastikan hanya membaca kolom yang ada, dan lakukan pengecekan NULL
string id = rdr.IsDBNull(rdr.GetOrdinal("id")) ? "NULL" : rdr["id"].ToString();
string name = rdr.IsDBNull(rdr.GetOrdinal("name")) ? "NULL" : rdr["name"].ToString();
string row = $"ID: {id}, Name: {name}n";
textView.Buffer.Text += row;
}
rdr.Close();
}
catch (Exception ex)
{
textView.Buffer.Text = "Error: " + ex.Message;
}
finally
{
conn.Close();
}
}
public static void Maincoy()
{
Application.Init();
new MainWindow();
Application.Run();
}
}
</code>
<code>using System;
using Gtk;
using MySql.Data.MySqlClient;
public partial class MainWindow : Window
{
private Button btnFetchData;
private TextView textView;
public MainWindow() : base("GTK# MariaDB Fetch Example")
{
// Set window properties
SetDefaultSize(400, 300);
SetPosition(WindowPosition.Center);
// Create a button to fetch data
btnFetchData = new Button("Fetch Data");
btnFetchData.Clicked += OnButtonClicked;
// Create a TextView to display results
textView = new TextView();
// Use a vertical box to layout the button and textview
VBox vbox = new VBox();
vbox.PackStart(btnFetchData, false, false, 0);
vbox.PackStart(textView, true, true, 0);
Add(vbox);
// Show all UI elements
ShowAll();
}
private void OnButtonClicked(object sender, EventArgs e)
{
// Connect to MariaDB and fetch data
string connStr = "server=localhost;" + "port=3306;" + "user id=root; " + "password=halonamasayafauzy;" + "database=MonoPeople;" + "SslMode=none";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
string sql = "SELECT * FROM test";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
// Clear existing text
textView.Buffer.Clear();
// Display results in TextView
while (rdr.Read())
{
// Pastikan hanya membaca kolom yang ada, dan lakukan pengecekan NULL
string id = rdr.IsDBNull(rdr.GetOrdinal("id")) ? "NULL" : rdr["id"].ToString();
string name = rdr.IsDBNull(rdr.GetOrdinal("name")) ? "NULL" : rdr["name"].ToString();
string row = $"ID: {id}, Name: {name}n";
textView.Buffer.Text += row;
}
rdr.Close();
}
catch (Exception ex)
{
textView.Buffer.Text = "Error: " + ex.Message;
}
finally
{
conn.Close();
}
}
public static void Maincoy()
{
Application.Init();
new MainWindow();
Application.Run();
}
}
</code>
using System;
using Gtk;
using MySql.Data.MySqlClient;
public partial class MainWindow : Window
{
private Button btnFetchData;
private TextView textView;
public MainWindow() : base("GTK# MariaDB Fetch Example")
{
// Set window properties
SetDefaultSize(400, 300);
SetPosition(WindowPosition.Center);
// Create a button to fetch data
btnFetchData = new Button("Fetch Data");
btnFetchData.Clicked += OnButtonClicked;
// Create a TextView to display results
textView = new TextView();
// Use a vertical box to layout the button and textview
VBox vbox = new VBox();
vbox.PackStart(btnFetchData, false, false, 0);
vbox.PackStart(textView, true, true, 0);
Add(vbox);
// Show all UI elements
ShowAll();
}
private void OnButtonClicked(object sender, EventArgs e)
{
// Connect to MariaDB and fetch data
string connStr = "server=localhost;" + "port=3306;" + "user id=root; " + "password=halonamasayafauzy;" + "database=MonoPeople;" + "SslMode=none";
MySqlConnection conn = new MySqlConnection(connStr);
try
{
conn.Open();
string sql = "SELECT * FROM test";
MySqlCommand cmd = new MySqlCommand(sql, conn);
MySqlDataReader rdr = cmd.ExecuteReader();
// Clear existing text
textView.Buffer.Clear();
// Display results in TextView
while (rdr.Read())
{
// Pastikan hanya membaca kolom yang ada, dan lakukan pengecekan NULL
string id = rdr.IsDBNull(rdr.GetOrdinal("id")) ? "NULL" : rdr["id"].ToString();
string name = rdr.IsDBNull(rdr.GetOrdinal("name")) ? "NULL" : rdr["name"].ToString();
string row = $"ID: {id}, Name: {name}n";
textView.Buffer.Text += row;
}
rdr.Close();
}
catch (Exception ex)
{
textView.Buffer.Text = "Error: " + ex.Message;
}
finally
{
conn.Close();
}
}
public static void Maincoy()
{
Application.Init();
new MainWindow();
Application.Run();
}
}
i’m just trying to connect my gtk#2.0 app to a database in with mariadb-server. i’m using debian 12. anything will be appreciated. thanks!