Steven Matthews
Steven Matthews

Reputation: 11285

Calling a method from within a class - not working

So I've got a class, commenter, and two methods within that class, SaveBtn_Click - created primarily not by me, and then also PeerReview, primarily created by me.

Anyway, the code starts off like this (after a variety of using statements):

public partial class commenter : System.Web.UI.Page

    {
        string employee_reviewed;
        PeerReview pr = new PeerReview();
        public void SaveBtn_Click(object sender, EventArgs e)
        {
           //all the information for the SaveBtn_Click method. 
        }

After that, I have PeerReview:

 public void PeerReview(System.Web.UI.WebControls.ListBox listbox)
    {
        MySqlConnection con = new MySqlConnection("server=localhost;database=hourtracking;uid=username;password=password");
        MySqlCommand cmd = new MySqlCommand("select first_name from employee where active_status=1", con);
        con.Open();
        MySqlDataReader r = cmd.ExecuteReader();

        Console.WriteLine("Another test!");
        Console.WriteLine(r);
        Console.WriteLine("Hi, this is a test!");
        while (r.Read())
        {
            listbox.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));

        }
        con.Close();
    }

I'm connecting this with ASP.NET, and I can get the listbox to show up, but not the individual items in the listbox. I'm testing it with a console.writeline command, to see if that outputs anything - but nothing is being put out on the ASP page.

I'm not certain how I should reference these particular sections (new to C#, asking like 3 dozen questions about this).

ASP code looks like this:

<asp:ListBox ID="listBox1" runat="server">

Upvotes: 0

Views: 263

Answers (3)

sjramsay
sjramsay

Reputation: 555

quick view here is you are adding items to listbox instead of listBox1

change:

 listbox.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));

to:

 listBox1.Items.Add(new ListItem(Convert.ToString(r["first_name"]), Convert.ToString(r["first_name"])));

Upvotes: 0

Darren Wainwright
Darren Wainwright

Reputation: 30727

You're trying to add items to listbox though your listBox has an id of listBox1

Rather than looping through your data and adding items why not bind the datasource to your listbox and then set the DataTextField and DataValueField on your listbox.

So for example (typos may exist..sorry.. been a while since i wrote C#)

MySqlConnection con = new MySqlConnection("server=localhost;database=hourtracking;uid=username;password=password");
    MySqlCommand cmd = new MySqlCommand("select first_name from employee where active_status=1", con);
    con.Open();
    MySqlDataReader r = cmd.ExecuteReader();

    listBox1.DataSource = r;
    listBox1.DataBind();
    con.Close();

If you can't bind to the reader (can't remember..) then dump your results into a datatable first, then bind to the listBox1

    DataTable dTable = New DataTable();
    dTable.Load(reader);
    listBox1.DataSource = dTable;
    listBox1.DataBind();

in your asp, set the listBox fields like:

<asp:ListBox ID="listBox1" runat="server" DataTextField="first_name" DataValueField="first_name">

Upvotes: 0

David W
David W

Reputation: 10184

You have some confused declarations.

You declare a method called PeerReview, but you also have an attempt to create an instance of PeerReview as though it were a type. I think you really just want to call the PeerReview method from your button click event, eg

public void SaveBtn_Click(object sender, EventArgs e)
        {
           PeerReview();
        }

And then eliminate the "PeerReview pr = new PeerReview();" line. Also, as this is on a page, you have an implicit reference within the partial class to the listbox by its ID, so you don't need to pass it as a parameter. And the Console.WriteLines are not useful in a web application - you might try Response.Write if you're wanting to add that to the output for debug purposes.

Edits based on OP response

You should call PeerReview in the Page_Load event handler:

public void Page_Load(object sender, EventArgs e)
{
    // You need to determine if you should call PeerReview every time the page 
    // loads, or only on the initial call of the page, thus determining whether
    // you need the IsPostBack() test. My instinct is that you *do* want to constrain
    // it to the first pass, but only you can make that determination for
    // certain based on your requirements.

    if (!Page.IsPostBack)  //Do you need this check?
    {
        PeerReview();
    }
}

Upvotes: 2

Related Questions