Home > other >  Program.cs file wont accept name of form that I've created
Program.cs file wont accept name of form that I've created

Time:04-13

I've been creating an application for university coursework, using Visual Studio Forms, and I was trying to launch the form I've made to check it's connecting to the database behind it, but it won't accept the name of the form as legitimate. I have no idea why, as it is appearing in the Solution Explorer.

This is the code for the program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FoxhillCustomer
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmViewEditStaff());
        }
    }
}

This is the code for the frmViewEditStaff.cs:

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;

namespace FoxhillViewEditStaff
{
    public partial class frmViewEditStaff : Form
    {
        SqlDataAdapter daViewEditStaff;
        DataSet dsFoxhillDentistryFinal = new DataSet();
        SqlCommandBuilder cmdBViewEditStaff;
        DataRow drViewEditStaff;
        String connStr, sqlViewEditStaff;
        int selectedTab = 0;
        bool staffSelected = false;
        int staffNoSelected = 0;

        public frmViewEditStaff()
        {
            InitializeComponent();
        }

        private void frmViewEditStaff_Load(object sender, EventArgs e)
        {
            connStr = @"Data Source = .\sqlexpress; Initial Catalog = InTheDogHouse; Integrated Security = true";

            sqlViewEditStaff = @"select * from Staff";
            daViewEditStaff = new SqlDataAdapter(sqlViewEditStaff, connStr);
            cmdBViewEditStaff = new SqlCommandBuilder(daViewEditStaff);
            daViewEditStaff.FillSchema(dsFoxhillDentistryFinal, SchemaType.Source, "ViewEditStaff");
            daViewEditStaff.Fill(dsFoxhillDentistryFinal, "ViewEditStaff");


            dgvStaff.DataSource = dsFoxhillDentistryFinal.Tables["Staff"];
        dgvStaff.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);

        tabStaff.SelectedIndex = 1;
        tabStaff.SelectedIndex = 0;

    }
}

}

Visual Studio Screen

CodePudding user response:

Your form is in a namespace of FoxhillViewEditStaff. Your Program class is in a namespace of FoxhillCustomer, and you don't have a using directive for the FoxhillViewEditStaff namespace.

I'd strongly advise you to:

  • Use a single namespace for all the classes in your project
  • Rename all your forms to follow normal .NET naming conventions, to make the code clearer

You could just add a using directive, but it would be more sensible to just put the classes into the same namespace, IMO.

  • Related