Home > Back-end >  How to resolve "'default_aspx' does not contain a definition for 'Button1_Click&
How to resolve "'default_aspx' does not contain a definition for 'Button1_Click&

Time:11-03

enter image description hereI have looked up other questions regarding but none of the solutions are applicable in this case. I have tried to delete the Button1_Click function and add it again, I have tried to rename it. On the right-clicking in the design and viewing code takes me to the same file therefore the files are connected properly.

The funny thing is that it was working before. At first I was receiving the error: 'default_aspx' does not contain a definition for 'TextBox1_TextChange'. I deleted OnTextChange=TextBox1_TextChange and then deleted the protected void TextBox1_TextChange(object sender, EventArgs e) {}

I recompiled and started to receive 'default_aspx' does not contain a definition for 'Button1_Click'

The code is as follows:

Default.aspx.cs*

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace APP_FullStack_C.Sharp_.Net_MSSQL
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=DESKTOP\SQLEXPRESS;Initial Catalog=StudentRecords;Integrated Security=True");
            con.Open();
            // Sql connection string
            SqlCommand comm = new SqlCommand("Insert into dbo.StudentInfo_Tab values('"   int.Parse(TextBox1.Text)   "','"   TextBox2.Text   "','"   DropDownList1.SelectedValue   "','"   double.Parse(TextBox3.Text)   "','"   TextBox4.Text   "')", con);
            comm.ExecuteNonQuery();
            con.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('Successfully Inserted');", true);
            LoadRecord();
        }

        // To show inserted values in the grid view 
        void LoadRecord()
        {
            SqlCommand comm = new SqlCommand("select * from StudentInfo_Tab", con);
            SqlDataAdapter d = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();
            d.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        
    }
}

Default.aspx*

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="APP_FullStack_C.Sharp_.Net_MSSQL._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <div>
        <div style="font-size:x-large; height: 51px;" align="center" > Student Info Manage Forms</div>
        <table class="nav-justified">
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">Student ID</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server" Font-Size="Medium"  ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">Student Name</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Font-Size="Medium" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">Address</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">Age</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Font-Size="Medium" ></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 435px; height: 20px"></td>
                <td class="modal-sm" style="width: 186px; height: 20px">Contact</td>
                <td style="height: 20px">
                    <asp:TextBox ID="TextBox4" runat="server" Font-Size="Medium"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 435px; height: 20px;"></td>
                <td class="modal-sm" style="width: 186px; height: 20px;"></td>
                <td style="height: 20px"></td>
            </tr>
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">&nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black" OnClick="Button1_Click" Text="Insert" Width="79px" />
                </td>
            </tr>
            <tr>
                <td style="width: 435px">&nbsp;</td>
                <td class="modal-sm" style="width: 186px">&nbsp;</td>
                <td>
                    <asp:GridView ID="GridView1" runat="server" Width="538px">
                    </asp:GridView>
                </td>
            </tr>
        </table>
        <br />
    </div>

</asp:Content>

I am extremely new to C# and .Net. Any help is appreciated. Thanks in advance!

Edit: I think the issue is because of the backslash. Added the picture of the error. Removing the backslash removes the error but of course removing it is not an option.

CodePudding user response:

I would just remove the button click markup in this:

 <asp:Button ID="Button1" runat="server"
   BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black" 
   OnClick="Button1_Click" Text="Insert" Width="79px" />

so, in above, remove the onclick event. eg;

 <asp:Button ID="Button1" runat="server"
   BackColor="#E1E1E8" Font-Bold="True" ForeColor="Black" 
   Text="Insert" Width="79px" />

At this point, the error probably will go away.

However, if not then while on the above page right click->view code. In the code, find the code stub for the button click event. Comment it out (don't delete).

Now try the page. It should work - but of course that button not going to work.

At this point compile error gone?

Then in designer - double click on that button. It will re-create the event code for you. You can now cut/paste/move into that code stub the code you commented out before. Humpty dumpty should now be ok, and all the Kings men looks like in this case that humpty dumpty can be fixed!

edit:=-======================================================

Oh, gee, look at error message again - it is the event stub for the text box 1 on the form. This one:

    <asp:TextBox ID="TextBox1" runat="server" Font-Size="Medium"  ></asp:TextBox>
           

In code behind, there is is a code stub for TextBox1 called:

TextBox1_TextChange

so the error message is in fact clear, and correct. Go into code behind and find the TextChange event. You can comment out. Now back in designer, display the property sheet for the text box.

eg this: enter image description here

Double click in that area where the arrow points. you jump to code editor. It should place your cursor in the correct code stub. If it created a new one, then copy code from the existing Text changed event in your code.

My bad, my sorry - did not read that question close, and I thought this was a button issue, but it clearly was a text box changed event issue.

That code stub will not fire unless the text box has a autopostback=true, so it not all that clear what was supposed to occur.

Anyway, look for a code behind text changed event for text box 1.

CodePudding user response:

Instead of single backslash use 2 backslashes. Changed to

Data Source=DESKTOP-KJ3D7HA\SQLEXPRESS

Source:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1009

A big thanks to all who helped!!

  • Related