I think I'm missing a fundamental concept

D

Dan

I'm using several forms to manipulate several database tables, with
child objects and relations.

I've created classes for my Company records with properties for each
field in the table (gets and sets), but I can't seem to figure out how
to load the class with the data. I can create a method in the Company
class, but then I have to create a variable from the class to add the
field values to, then how do I get that variable out to the form.
It's wring and I know it, but I can't find any help in the books I
have, becuase all the example I can find do it all in one class and
method (Main()).

I think I want the method to run against the class and not a variable
in the class, but how do I reference the DB with varaibles? Here is
the code I'm using now:

I don't currently have this in the Company class as I got errors
trying to reference the class in itself (using 'this')
I'm only posting 2 of the Company properties classes, otherwise it's
just too long

public static void FillCompany(string CompID)
{
SqlConnection myDB =
new SqlConnection("server=SQLDB.RL.INT;database=" +
"Subs_Dev;Integrated Security=SSPI;");

SqlCommand mySQLCommand = myDB.CreateCommand();

Company myCompany = new Company();

string selectString = "EXEC uspSearchID @ID=" + CompID
+ ", @Table=utblcompany";

mySQLCommand.CommandText = selectString;

SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

mySqlDataAdapter.SelectCommand = mySQLCommand;

DataSet myDataset = new DataSet();
try
{
myDB.Open();
}
catch
{

}
string dataTableName = "Company";
mySqlDataAdapter.Fill(myDataset, dataTableName);

DataTable myDataTable = myDataset.Tables[dataTableName];

DataRow myDataRow = myDataTable.Rows[0];

myCompany.ActiveSubFlag = myDataRow
["ActiveSubFlag"].ToString();
}

public class Company
{
private Int32 orgID;
private string activesubflag;

public Int32 OrgID
{
get
{
return orgID;
}
set
{
orgID = value;
}
}
public string ActiveSubFlag
{
get
{
return activesubflag;
}
set
{
activesubflag = value;
}
}
}
 
D

Dan

I'm using several forms to manipulate several database tables, with
child objects and relations.
I've created classes for my Company records with properties for each
field in the table (gets and sets), but I can't seem to figure out how
to load the class with the data.  [...]

I'm afraid I don't really understanding the question.  You already have
code to implement properties.  You already have code that retrieves data
  from your database and initializes those properties.  You could easily
move the code that's in "FillCompany()" into the Company class itself.
What's left?

What _specifically_ are you having trouble with?  Please formulate your
post as an actual, specific question.

Pete

Sorry about that Pete. I was able to figure it out. I needed to drop
the static part of the FillCompany class when it's inside the Company
class, then all the this references work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top