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?

MSIslam
  • 4,587
  • 6
  • 25
  • 28
  • This is ASP.NET Web Forms, not MVC. – Matthew Flaschen Jun 19 '12 at 22:06
  • Have you looked at your schema in SQL Management Studio? There may be a mismatch. – Codeman Jun 19 '12 at 22:11
  • Can you show the script that defines `dbo.LeadProcedure` - The parameters are probably not quite what you're expecting. – Jon Egerton Jun 19 '12 at 22:15
  • 1
    @JonEgerton the error message doesn't look like a SqlException. Its probably a build related problem. See http://forums.asp.net/post/1348461.aspx – Conrad Frix Jun 19 '12 at 22:18
  • 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 – fenix2222 Jun 19 '12 at 22:27
  • 1
    Try the steps detailed in this answer: http://stackoverflow.com/questions/706603/the-name-controlname-does-not-exist-in-the-current-context/1981228#1981228 – Jon Egerton Jun 19 '12 at 22:45
  • To expand on @Conrad Frix's comment, can you please paste the error message in it's entirety? Your 'like' is too vague. – Craig Jun 19 '12 at 22:45
  • name'/'email'/'sub'/'message text boxes are not referenced in the .designer.cs file, hope fully it will solve the problem. – MMK Jun 19 '12 at 22:52
  • Thanks all for the kind comments. I am new, so couldn't explain few things correctly. I will follow your suggestions. Checked the stored procedure in sqlserver and it's working ok. Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0103: The name 'name' does not exist in the current context Source Error: Line 37: cmd1.Parameters.Add("@name", System.Data.SqlDbType.VarChar).Value = name.Text; – MSIslam Jun 20 '12 at 15:42
  • 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? – MSIslam Jun 20 '12 at 15:46
  • I tried the options, but no progress! ('convert to the application' doesn't come up). 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? – MSIslam Jun 20 '12 at 16:50
  • I kinda solved it. I just changed my codefile from Default.aspx.cs to Contact.aspx.cs and created all the methods there and now it's working perfectly!!!! It seems it was referring to something else or calling the values from Memory. Thanks All. – MSIslam Jul 05 '12 at 19:37

1 Answers1

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.

fenix2222
  • 4,602
  • 4
  • 33
  • 56
  • 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? – MSIslam Jun 20 '12 at 15:45
  • Designer file will exist if you created web application project, but not in web site project. Do you have it for other pages? – fenix2222 Jun 20 '12 at 22:32