davidxc davidxc - 1 month ago 7
ASP.NET (C#) Question

My web form is not saving to database successfully

My form doesn't save the text in the Texbox to the database. I've probably something wrong in my .cs CodeFile, but I can't work it out.

It could well be my connection string.

My web form:

<form id="form1" runat="server">
<div>
<table>
<tr>
<td>
Enter selection text:
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Submit" />
</td>
</tr>
</table>
</div>
</form>


Here is my code behind:

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

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=db65225900.db.1and1.com; Initial Catalog=db211255182; User ID=dbo652259000; Password=Password");

protected void Page_Load(object sender, EventArgs e)
{
}

protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into homepageSelection values('"+TextBox1.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
}
}


My MsSQL is setup like the following:

1 column: selectionText nvarchar(3000)

Answer

Missing button click event definition in aspx

 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click1" />
                                             ---------------^