While trying to add value into Database, using C# code in .net framework; getting Error: The name 'name' does not exist in the current context

I have an asp.net page where clients can insert inputs in text fields:

<table>
    <tr>
        <td style="width: 56px"><asp:Label ID="Label1" runat="server" Text="Name"></asp:Label></td>
        <td style="width: 148px"><asp:TextBox ID="name" runat="server" Width="272px"></asp:TextBox></td>
    </tr>

    <tr>
        <td style="width: 56px"><asp:Label ID="Label2" runat="server" Text="Email"></asp:Label></td>
        <td style="width: 148px"><asp:TextBox ID="email" runat="server" Width="272px"></asp:TextBox></td>
    </tr>

    <tr>
        <td style="width: 56px"><asp:Label ID="Label3" runat="server" Text="Subject"></asp:Label></td>
        <td style="width: 148px"><asp:TextBox ID="sub" runat="server" Width="272px"></asp:TextBox></td>
    </tr>

    <tr>
        <td style="width: 56px"><asp:Label ID="Label4" runat="server" Text="Message"></asp:Label></td>
        <td style="width: 148px">
            <asp:TextBox ID="message" runat="server" Width="272px"></asp:TextBox>
            <!--<textarea id="message"; cols="1"; rows="1"; style="width: 272px; height: 152px;"></textarea>-->
        </td>
    </tr>
    <tr></tr>
    <tr>
        <td style="width: 56px; height: 36px;"></td>
        <td style="width: 148px; height: 36px;"><asp:Button ID="Button1" runat="server" Text="Submit" Style="float:left" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Reset" />
            <asp:button id="btnTest" runat="server" onclick="btnTest_Click" text="Test Database Connection" />
        </td>
    </tr>
</table>      "

I have a method that tries to add my values into the database:

protected void Button1_Click(object sender, EventArgs e)
{
    using (SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
    {
        //Check if the same request is already submitted.

        SqlCommand cmd1 = new SqlCommand();
        cmd1.CommandType = System.Data.CommandType.StoredProcedure;
        cmd1.CommandText = "dbo.Procedure";

        cmd1.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = name.Text;
        cmd1.Parameters.Add("@email", System.Data.SqlDbType.VarChar).Value = email.Text;
        cmd1.Parameters.Add("@sub", System.Data.SqlDbType.VarChar).Value = sub.Text;
        cmd1.Parameters.Add("@message", System.Data.SqlDbType.VarChar).Value = message.Text;

        cmd1.Connection = conn1;
        conn1.Open();
        cmd1.ExecuteNonQuery();
        conn1.Close();
    }                 
    Response.Redirect("~/Default.aspx");     
}

But now I am getting error like "The name 'name'/'email'/'sub'/'message' does not exist in the current context". I am a new one into .net framework and C#. Please help.

My start page is Default.aspx and submit button is on Contact.aspx page. But when I press the Submit button in the design mode, then the method created in the code behind of Default.aspx.cs. Could it be an issue?

Answers


To me it looks like code behind cannot locate TextBoxes with ids (email, name...). If you are using web application project, check your .aspx.designer.cs file to make sure it is initialised properly. Also your code Behind partial class name and namespace must match your Inherits="xxx" attribute in <%@ Page ... %> directive in your aspx page. However, you might also have this issue because your table is inside some databound or dynamic control, in this case you would have to find your control references in code.


Need Your Help

Youtube embed videos not working in webview. What's wrong with this code?

android video youtube embed android-webview

I am new to android. I am facing two problems right now 1. I parsed xml file and showed it in webview which contains videos and text . When i click on videos it will not play . In xml videos are