简体   繁体   中英

method is not being called by application

My method is not being called by my application. I've used breakpoints and it's never initiated in the code. I'm building a C# Windows Forms application using an Azure Database, but the DataGridView is never being filled neither is the code being called at all... I have noooo clue whatsoever why..

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 MyWinFormsProj
{
   public partial class CompanyForm : Form
{
    public CompanyForm()
    {
        InitializeComponent();
    }
    //Connection String
    string cs = ConfigurationManager.ConnectionStrings
    ["MyConnetion"].ConnectionString;

    // Load all employees
    private void dataEmployees_Load()
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand
            (
                "Select fname,ename FROM dbo.Users", con
            );
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataEmployees.DataSource = dt;
        }
    }

    // Crate company
    private void createCompany_Click_1(object sender, EventArgs e)
    {
        if (textBoxCompanyName.Text == "")
        {
            MessageBox.Show("Fill out information");
            return;
        }
        using (SqlConnection con = new SqlConnection(cs))
        {
            //Create SqlConnection
            con.Open();
            SqlCommand cmd = new SqlCommand(
            "insert into dbo.Company  (companyName) 
             values(@companyName)", con);
             cmd.Parameters.AddWithValue(
            "@companyName", textBoxCompanyName.Text);
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            MessageBox.Show("Grattis! Du har skapat ett företag");

           }
        }
      }
    }

The second method is working and is doing what it is supposed to do, but the first one is never called..

you need to set an event handler on the gridView onLoad and pass this method to the handler

public void GridView_OnLoad(object sender, EventArgs e)
{
    dataEmployees_Load();
}

You need to fix your method signature to look like this:

private void dataEmployees_Load(object sender, EventArgs e)

Then, in your GirdView, you need to set this function as handler for event "onload":

OnLoad="dataEmployees_Load"

Thank you guys for your answers it helped my solve the problem. Like you were saying the problem was in the method not being called. So I called it directly on initiliazeComponent like this.

    public partial class CompanyForm : Form
{
    public CompanyForm()
    {
        InitializeComponent();
        Load += new EventHandler(dataEmployees_Load); //Added this code
    }
    // Load all employees
    private void dataEmployees_Load(object sender, EventArgs e)
    {
        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand
            (
                "Select fname,ename FROM dbo.Users", con
            );
            SqlDataReader dr = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(dr);
            dataEmployees.DataSource = dt;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM