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?