For Programmers: Free Programming Magazines  


Home > Archive > C# > November 2004 > Newbie needs help: treeview, datareader, and a function









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Newbie needs help: treeview, datareader, and a function
techprot-google04

2004-11-27, 3:56 pm


Who's asking this question:
1. A newbie.
2. This is my second C# program.
3. I've done VB programming in the past.
4. I'm rusty on OOP - usually where I trip up.

Here is what I've got:
1. A form with treeview.
2. A database with a table.

Here is what I want to do:
1. Populate the treeview with each record in the table.

What you'll notice:
1. Why use a treeview? Future functionality. I plan to have "sub
projects".
2. But for now I want to have a node per record.

My problem:
1. If the fillTree function is a void then when I call in in my main(),
I get "an object reference is required for the

nonstatic field, method or property...".
2. However, if I make it static void fillTree(), then I get an error
one the line "projectTree.Nodes..." where it says -

"...projectTree' denotes a field where a class was expected".

Help!


My function:

I use the line fillTree(); in Main() to call the function below.

void fillTree()

// Binds the tree view to data
{
int nRows = 0;

string conStr = @"server=myserv;" +
"Integrated Security=true;" +
"database=GroupProjectTracker";

SqlConnection Conn = new SqlConnection(conStr);

try
{
Conn.Open();
string SQL = "SELECT * FROM tblProjects WHERE projectStatus =
'Open'";
SqlCommand sqlComm= new SqlCommand(SQL, Conn);
SqlDataReader sqlReader = sqlComm.ExecuteReader();

while (sqlReader.Read())
{

projectTree.Nodes.Add(sqlReader[1]);


}
}
catch (Exception ex)
{
MessageBox.Show ("Error connecting to database: " + ex.Message);
Conn.Close();
}
finally
{
Conn.Close();
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com