0

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?

12
  • This is ASP.NET Web Forms, not MVC. Commented Jun 19, 2012 at 22:06
  • Have you looked at your schema in SQL Management Studio? There may be a mismatch. Commented Jun 19, 2012 at 22:11
  • Can you show the script that defines dbo.LeadProcedure - The parameters are probably not quite what you're expecting. Commented Jun 19, 2012 at 22:15
  • 1
    @JonEgerton the error message doesn't look like a SqlException. Its probably a build related problem. See forums.asp.net/post/1348461.aspx Commented Jun 19, 2012 at 22:18
  • 1
    Try the steps detailed in this answer: stackoverflow.com/questions/706603/… Commented Jun 19, 2012 at 22:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't have any .aspx.designer.cs file!!! As I know it should have created automatically. Could you please suggest how to get this?
Designer file will exist if you created web application project, but not in web site project. Do you have it for other pages?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.