0

I am getting this error for an event handler for a button when clicked. Please help me. Thanks in advance!

HTMLCODE

<asp:Button ID="RunButton" runat="server" Text="Run" onclick="Load"/>

SERVER CODE

protected void btn_Click(object sender, EventArgs e)
    {
    string strConnString = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Emp]"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    ResultPage.GridView1.DataSource = dt;
                    ResultPage.GridView1.DataBind();
                }
            }
        }
    }
}
Brandon
  • 87
  • 1
  • 1
  • 9
  • Possible duplicate of ["A namespace cannot directly contain members such as fields or methods" in Net.Reflector](http://stackoverflow.com/questions/21175781/a-namespace-cannot-directly-contain-members-such-as-fields-or-methods-in-net-r) – Nkosi Aug 01 '16 at 02:04
  • If you did a quick search on stack overflow or google for that matter you would see exactly what can cause that error. The code in your example has nothing to do with that issue. – Nkosi Aug 01 '16 at 02:06
  • @Nkosi I did read through that question, however, I am using this code that has worked before. The only thing I did was to change the ID and server-side event handler. I checked through the codes at the server-side. There was no extra } or misplaced ones. This error only happened when I added the event handler to my run button to run the sql. – Brandon Aug 01 '16 at 02:14
  • Check your source code and see if you have any stray variables outside your classes. – Nkosi Aug 01 '16 at 02:16
  • I found the error. Apparently, I accidentally deleted the } and somehow all had { }. Thanks for your help @Nkosi – Brandon Aug 01 '16 at 02:30
  • Based on the code you have shown in your example, I see nothing that will cause the error you stated. only discrepancy is between your button's `onclick="Load"` and the name of the handler `btn_Click` – Nkosi Aug 01 '16 at 02:31

0 Answers0