4 Replies - 167 Views - Last Post: 03 June 2019 - 11:02 AM Rate Topic: -----

#1 petercheung   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-June 19

C# Combobox in Other Class

Posted 03 June 2019 - 02:24 AM

I want to code all process code to one file, but I don't how to code Combobox to my code


process.cs
    public ConnectDB()
        {
            Initialize();
        }

        //Initialize values
        public void Initialize()
        {
            server = "localhost";
            database = "localdatabase";
            uid = "user";
            password = "password";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            connection = new MySqlConnection(connectionString);
        }


        //open connection to database
        private bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                //When handling errors, you can your application's response based on the error number.
                //The two most common error numbers when connecting are as follows:
                //0: Cannot connect to server.
                //1045: Invalid user name and/or password.
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                }
                return false;
            }
        }

        //Close connection
        private bool CloseConnection()
        {
            try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public void UserAuthCommd(string staffId, string pwd)
        {
            string query = "SELECT* FROM employee where employee_id = '" + staffId + "' AND PASSWORD = '" + pwd + "'; ";

            if (this.OpenConnection() == true)

            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
.
.

                while (dataReader.Read())
                {
                    count = count + 1;
                    IsL0User = dataReader["em_type"].ToString().Equals("S"); // L0 For System Administor
                  
                }

                if (count == 1 && IsL1User == true)
                {
                    MessageBox.Show("Level 0 Role");
					// for test 
                    Level0Main_frm Level0 = new Level0Main_frm();
                    Level0.ShowDialog();

                }

                else
                {
                    MessageBox.Show("Username and Password is Not Correct . . . Please try again", "Error Message!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
                connection.Close();
                this.CloseConnection();
            }

        }







i try to add following code into process.cs

   public void Req(ComboBox comboBox)
        {

            string query = "SELECT `em_id` FROM `employee`";

            if (this.OpenConnection() == true)

            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                OpenConnection();
                while (dataReader.Read())
                {
                    comboBox.Items.Add(dataReader[1]);

                }

                connection.Close();
                cmd.ExecuteNonQuery();
                this.CloseConnection();




and following code to a new form

  public partial class Req_frm : Form
    {
        private ConnectDB dbConnect;
   
        public Req_frm()


        {
            InitializeComponent();
           dbConnect = new ConnectDB();
        }

        private void comboBox1_Load(object sender, EventArgs e)
        {
            dbConnect.Req(comboBox1);
        }
    }
}




the Combobox did not display anything

Is This A Good Question/Topic? 0
  • +

Replies To: C# Combobox in Other Class

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: C# Combobox in Other Class

Posted 03 June 2019 - 04:28 AM

And what happens when you step through the code with the debugger instead of just letting it run? In particular, step through the Req() method?
Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,090
  • Joined: 12-January 10

Re: C# Combobox in Other Class

Posted 03 June 2019 - 08:23 AM

i use a class i made that fills all my dropdowns

then i call that class from another class

you should really break your code up so it is easier to fix down the road.

example:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

public class LoadDropDowns
{
    public void LoadDropDowns()
    {
    }


    public void LoadDropDowns(object your parameters)
    {
        Dataset _pDataset = new Dataset();
        try
        {
            cDataAccess pDataAccess = new cDataAccess();
            SqlCommand cmd = pDataAccess.GetSQLCommand("some Stored Procedure");
            DataSet pDataset;

            cmd.Parameters.Add(new SqlParameter("@", some));


            pDataset = pDataAccess.GetDataSet(cmd);

            cmd.CommandTimeout = 180;
            if (pDataset.Tables(0).Rows.Count > 0)   
                _pDataset = pDataset;
            else
            {
                DataSet _ds = new DataSet();
                _pDataset = _ds;
            }
            pDataAccess.CloseConnection();
            cmd.Dispose();
        }
        catch (Exception ex)
        {
        }

        return _pDataset;
    }
}




the other class

 protected void LoadSomeDroptDown()
{
    LoadDropDowns _someDropdownDS = new LoadDropDowns();

    ddlSomething.DataSource = _someDropdownDS.LoadDropDowns(parameters);
    ddlSomething.DataTextField = "some textfield";
    ddlSomething.DataValueField = "some id";
    ddlSomething.DataBind();
}


This post has been edited by DarenR: 03 June 2019 - 11:01 AM

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: C# Combobox in Other Class

Posted 03 June 2019 - 10:20 AM

DarenR: Please use C# in the C# forum.
Was This Post Helpful? 0
  • +
  • -

#5 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,090
  • Joined: 12-January 10

Re: C# Combobox in Other Class

Posted 03 June 2019 - 11:02 AM

fixed
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1