Home>

I am creating a form app in C #.

When I call the form, I want to display the information acquired from the DB in a table in the form.
Access to the DB is described in the Load process, but when I try to debug it, it seems that the Load process is not called.

DB uses SQL Server.

I would appreciate if you could tell me how to fix it.

Thank you.

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;
using System.Data.SqlClient;
using System.Configuration;
namespace hiyariHat
{
    public partial class User List: Form
    {
        public user list ()
        {
            InitializeComponent ();
        }
        string constr = @ "Data Source = DESKTOP-S22NJTB \ SQLEXPRESS;Initial Catalog = hiyarihatto;Integrated Security = True";
        int rowcount;
        private void user list_Load (object sender, EventArgs e)
        {
            try
            {
                DataTable dataTable = new DataTable ();
                string sql = "SELECT * FROM RESIDENTS";
                using (SqlConnection conn = new SqlConnection (constr))
                {
                    using (SqlCommand cmd = new SqlCommand (sql, conn))
                    {
                        using (SqlDataAdapter adapter = new SqlDataAdapter (cmd))
                        {
                            adapter.Fill (dataTable);
                            rowcount = dataTable.Rows.Count;
                        }
                    }
                }

                   //dataGridView1.Rows.Add(dataTable);
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString ());
            }

        }
        private void dataGridView1_CellContentClick (object sender, DataGridViewCellEventArgs e)
        {
        }
        private void button1_Click (object sender, EventArgs e)
        {
            New user registration frm = new New user registration ();
            frm.Show ();
        }

    }
}