Calling the functionfrom one class to another in a default datatable

I have a function like this in one of my classes and then I need to call it in another class and get the value in a default datatable.

public DataTable GetPrimaryKeyTables(string localServer, string userName, string password, string selectedDatabase)
{
        // Create the datatable 
        DataTable dtListOfPrimaryKeyTables = new DataTable("tableNames");

        SqlConnectionStringBuilder objConnectionString = new SqlConnectionStringBuilder();
        objConnectionString.DataSource = localServer; ;
        objConnectionString.UserID = userName;
        objConnectionString.Password = password;
        objConnectionString.InitialCatalog = selectedDatabase;

        // Query to select primary key tables.
        string selectPrimaryKeyTables = @"SELECT 
                                               TABLE_NAME
                                              AS
                                               TABLES
                                            FROM 
                                               INFORMATION_SCHEMA.TABLE_CONSTRAINTS
                                           WHERE 
                                               CONSTRAINT_TYPE = 'PRIMARY KEY'
                                        ORDER BY
                                               TABLE_NAME";

        // put your SqlConnection and SqlCommand into using blocks! 
        using(SqlConnection sConnection = new SqlConnection(objConnectionString.ConnectionString))
        using(SqlCommand sCommand = new SqlCommand(selectPrimaryKeyTables, sConnection))
        {
            try
            {
                // Create the dataadapter object 
                SqlDataAdapter sDataAdapter = new SqlDataAdapter(selectPrimaryKeyTables, sConnection);

                // Fill the datatable - no need to open the connection, the SqlDataAdapter will do that all by itself  
                // (and also close it again after it is done) 
                sDataAdapter.Fill(dtListOfPrimaryKeyTables);
                dgResultView.DataSource = dtListOfPrimaryKeyTables;
            }
            catch(Exception ex)
            {
                //All the exceptions are handled and written in the EventLog. 
                EventLog log = new EventLog("Application");
                log.Source = "MFDBAnalyser";
                log.WriteEntry(ex.Message);
            }
        }

        // return the data table to the caller 
        return dtListOfPrimaryKeyTables;
    }

Can anyone help me in this, every time I try, the controls are not inherited from one class to another.

Answers


I am not sure what you mean by 'controls not inherited from one class to another'.

you will create an object of this class in your another class and call the method on it.

something like this

class class1
{
  public DataTable GetPrimaryKeyTables(string localServer, string userName, string password, string selectedDatabase)


  .......
   ........
  return dtListOfPrimaryKeyTables;


}
class Class2
{
    protected void BindControl(....)
    {
      DataTable dt = new class1().GetPrimaryKeyTables(......);
      dgResultView.DataSource = dt;
      dgResultView.DataBind();

     }

}

Either you pass 'dgResultView' as a parameter to the method or you use the above code snippet. Controls are defined as 'Protected', hence they won't be accessible in the other class. dgResultView.DataSource = dtListOfPrimaryKeyTables; used in the function is not gonna work.

It is a good idea to have your connection string and other information in a config file and is accessed from there.


Need Your Help

Node bin file not being recognized on linux or osx

node.js command-line-interface

I've been developing a node cli app using PyCharm from Windows and it works only on Windows, but always returns : No such file or directory when being run from Linux. I've been able to reproduce the

Splitting a string on to several different lines in Java

java string console char

I have a very long string consisting of words punctuation and spaces. I am trying to modify the string to add a new line (\n) after every x characters but while preserving whole words. For example:...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.