davidxc davidxc - 1 year ago 60
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">
Enter selection text:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<td colspan="2" align="center">
<asp:Button ID="Button1" runat="server" Text="Submit" />

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)
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into homepageSelection values('"+TextBox1.Text+"')";

My MsSQL is setup like the following:

1 column: selectionText nvarchar(3000)

Answer Source

Missing button click event definition in aspx

 <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click1" />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download