Srivastava
Srivastava

Reputation: 3588

Controls are not inherited from other class

I have a function like this defined in one class:

using System;
using System.Collections.Generic;

using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using System.Windows.Forms;
using System.Configuration;
using System.Diagnostics;
using MFDBAnalyser;

namespace MFDBAnalyser

{

    public class DataAccessMaster:MFDBAnalyser

    {


        //        /// <summary>
        //        /// This function gets the list of all the databases present in the local server.
        //        /// </summary>
        //        /// <returns></returns>


        public static DataSet GetAllDataBaseNames()

        {

            SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
            objConnectionString.DataSource = txtHost.Text;
            objConnectionString.UserID = txtUsername.Text;
            objConnectionString.Password = txtPassword.Text;

            SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString);

            //If connected then give this message to user
            lblMessage.Visible = true;
            lblMessage.Text = "You are connected to the SQL Server....";

            try
            {
                //To Open the connection.
                sConnection.Open();

                //Query to select the list of databases.
                string selectDatabaseNames = @"SELECT 
                                                    NAME 
                                                 FROM 
                                                    [MASTER]..[SYSDATABASES]";

                //Create the command object
                SqlCommand sCommand = new SqlCommand(selectDatabaseNames, sConnection);

                //Create the data set 
                DataSet sDataset = new DataSet("master..sysdatabases");

                //Create the dataadapter object
                SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectDatabaseNames, sConnection);
                sDataAdapter.TableMappings.Add("Table", "master..sysdatabases");

                //Fill the dataset
                sDataAdapter.Fill(sDataset);

                //Bind the database names in combobox
                DataViewManager dsv = sDataset.DefaultViewManager;

            }
            catch(Exception ex)
            {
                //All the exceptions are handled and written in the EventLog.
                EventLog logException = new EventLog("Application");
                logException.Source = "MFDBAnalyser";
                logException.WriteEntry(ex.Message);
                MessageBox.Show("Login Failed!!", "Error Occured");
            }
            finally
            {
                //If connection is not closed then close the connection
                if(sConnection.State != ConnectionState.Closed)
                {
                    sConnection.Close();
                }
            }
        }
    }
}

And then I called this function in another class like this:

public void BindDBDropDown()

        {

 DataSet dsTablesWithoutForeignKeys = default(DataSet);

            try
            {
                //The function GetAllForeignKeyTables() is called from the class PluginManager.
                dsTablesWithoutForeignKeys = DataAccessMaster.GetAllDataBaseNames();

                cmbDatabases.DisplayMember = "TABLE_NAME";
                cmbDatabases.ValueMember = "";
                cmbDatabases.DataSource = dsTablesWithoutForeignKeys.Tables["master..sysdatabases"];
            }
            catch(Exception ex)
            {
                //All the exceptions are handled and written in the EventLog.
                EventLog logException = new EventLog("Application");
                logException.Source = "MFDBAnalyser";
                logException.WriteEntry(ex.Message);
            }
        }

But there is showing a error like txtHost etx doesn't exist and when I change the protected modifier of designer.cs class to public then also it is showing error...

Can anybody tell me whats is happening??

Upvotes: 0

Views: 105

Answers (3)

msergeant
msergeant

Reputation: 4801

Assuming that your text boxes are defined in MFDBAnayser class, you still will not be able to access them in your GetAllDataBaseNames function. GetAllDataBaseNames is a static function and therefore it will not be able to access instance variables like the text boxes or other controls.

Upvotes: 1

kbvishnu
kbvishnu

Reputation: 15650

U cannot get the values for a form in another class. Try using the parameters. Or use properties {get ;set;} to acess the values. I think u can use some property and assign the value of textbox. If u are doing in winforms it is Ok that u can use static variables.But in the case of web u must pass arguments.

Upvotes: 0

willvv
willvv

Reputation: 8649

Verify that the modifier of txtHost is also protected or public.

Upvotes: 0

Related Questions