1

I'm new to C# and I'm trying to learn (I've been asking a few questions lately on it - my only programming knowledge is in Python, PHP and Javascript). Anyway, I found some examples on the web for how to generate a picklist in C#, and I'm trying to use/modify them to fit my existing code. The results are coming up with an expected class error - but I'm creating a class! Here's my code:

private void PeerReview()
{

            MySqlConnection connection;
            string connStringName = "server=localhost;database=hourtracking;uid=username;password=password";
            connection = new MySqlConnection(connStringName);

            cmd.CommandText = "select name from peer_review_info where active_status=1";
            cmd.Connection = connection;
            connection.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    ListItem item = new ListItem();
                    item.Text = sdr["peerrevid"].ToString();
                    item.Value = sdr["peerrevid"].ToString();
                    item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                    chkPeerRev.Items.Add(item);
                }
            }
           connection.Close();
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.PeerReview();
    }
}
3
  • Wait, or am I just using a method of a class that does not exist? Oct 19, 2012 at 13:20
  • 2
    Where do you get this error? (Where are the coloured squiggles?)
    – Rawling
    Oct 19, 2012 at 13:20
  • 1
    Can you show the whole code, including class? Oct 19, 2012 at 13:23

1 Answer 1

2

You have too many closing braces. Should be:

private void PeerReview()
{    
    MySqlConnection connection;
    string connStringName =
        "server=localhost;database=hourtracking;uid=username;password=password";
    connection = new MySqlConnection(connStringName);

    cmd.CommandText = "select name from peer_review_info where active_status=1";
    cmd.Connection = connection;
    connection.Open();
    using (SqlDataReader sdr = cmd.ExecuteReader())
    {
        while (sdr.Read())
        {
            ListItem item = new ListItem();
            item.Text = sdr["peerrevid"].ToString();
            item.Value = sdr["peerrevid"].ToString();
            item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
            chkPeerRev.Items.Add(item);
        }
   }
   connection.Close();
}

Well, actually it needs more changes than that; the connection needs a using, and it is very unclear where cmd is defined, but that also needs a using and should not be shared (it should be scoped to this method). I would rewrite as:

private void PeerReview()
{    
    string connStringName = SomethingExternal.GetConnectionString();

    using(var connection = new MySqlConnection(connStringName))
    using(var cmd = connection.CreateCommand())
    {
        cmd.CommandText =
            "select name from peer_review_info where active_status=1";
        connection.Open();
        using (var sdr = cmd.ExecuteReader())
        {
            while (sdr.Read())
            {
                ListItem item = new ListItem();
                item.Text = sdr["peerrevid"].ToString();
                item.Value = sdr["peerrevid"].ToString();
                item.Selected = Convert.ToBoolean(sdr["IsSelected"]);
                chkPeerRev.Items.Add(item);
            }
       }
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.