Error : Object cannot be cast from DBNull to other types. in monodevelop

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật