Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem understanding properties

Status
Not open for further replies.

Valeriya

MIS
Jan 9, 2006
138
US
Hi, I have a problem fully understanding properties. Below is the code and my question will follow righ after it
Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace SelectIntoDataSet
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectToDb = "server= ENGSQL1;database=Northwind;Integrated Security=True";
            string selectStr = "Select Top 10 CustomerID, CompanyName, ContactName, Address " +
                                      "From Customers Order by CustomerID";

            ConnectionClass connect = new ConnectionClass(); //(connectToDb,selectStr);
            connect._ConnectionString = connectToDb;
            connect._SelectString = selectStr;
                       
            connect.ConnectToDB("Customers");

            string connectToDb1 = "server= EngSQL1;database = pubs;Integrated Security=True";
            string selectStr1 = "Select * From authors";

            ConnectionClass connect1 = new ConnectionClass(); //(connectToDb1, selectStr1);
            connect1._ConnectionString = connectToDb1;
            connect1._SelectString = selectStr1;
            
            
            connect.ConnectToDB("Authors");
            Console.Read();
        }

       
    }
}


using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SelectIntoDataSet
{   
    
    public class ConnectionClass
    {
        private string connectionString;
        private string selectString;


        public string _ConnectionString
        {
            get{return connectionString;}
            set{connectionString = value;}

        }

        public string _SelectString
        {
            get{return selectString;}
            set {selectString = value;}
        }
  

            public void ConnectToDB(string tableName)
            {
             SqlConnection mySqlConnection = new SqlConnection(_ConnectionString);
             SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
             mySqlCommand.CommandText = _SelectString;
             SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
             mySqlDataAdapter.SelectCommand = mySqlCommand;
             DataSet myDataSet = new DataSet();
             mySqlConnection.Open();
          	 mySqlDataAdapter.Fill(myDataSet, tableName);
             mySqlConnection.Close();
		 DataTable myDataTable = myDataSet.Tables[tableName];

             DisplayRows(myDataTable);

           }

            public void DisplayRows(DataTable table) 
            {
                 foreach (DataRow myDataRow in table.Rows)
                 {
                 
                    for(int j = 0; j < table.Columns.Count; j++)
                    {
                      Console.WriteLine(table.Columns[j] + " = " + myDataRow[ table.Columns[j] ]);
                    }
                
                 }
                Console.WriteLine(table.Rows.Count);
                Console.Read();
             }
        }
    }

Here is my problem. Basically, I want to be able to set connection string and select(sql statement) string through the Main( ), fill the data set and display the records in the console window. What I would like to do next, is to pass (set) a new connection string and select string to the ConnectionClass and display those records; however, _ConnectionString and _SelectString remember the old values and display the records from the first step. When I stepped through the code I noticed that I was able to set new values to the two properties, however, as soon as I came back to my program class to call “connect.ConnectToDB("Authors");” the values in my properies change back to the old ones. How do I overcome this problem?

Thanks!

Valeriya
 
yep but i agree... you should be reusing the same object so instead of
Code:
ConnectionClass connect1 = new ConnectionClass(); //(connectToDb1, selectStr1);

you should have
Code:
connect = null;
connect = new ConnectionClass();

now you cannot get the objects mixed up :)

Age is a consequence of experience
 
Blind me! :) I found my mistake too, just a bit ago. Thanks to both of you for telling me about reusing same object!!! Books don't talk about these issues much.

Again thank you very much for the valuable tips!

Valeriya



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top