user6870371
user6870371

Reputation:

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)

Upvotes: 1

Views: 1561

Answers (3)

Mostafiz
Mostafiz

Reputation: 7352

You need to set the colum name in insert statement

cmd.CommandText = "insert into homepageSelection (ColumnName) values('"+TextBox1.Text+"')";

also you missed click event

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

Upvotes: 0

Mohit S
Mohit S

Reputation: 14064

SqlConnection is for SQL Server. You need MySqlConnection - This is not part of the .NET Framework. So it is a better approach if you will also have to use the MySqlCommand object rather than the SqlCommand object.

This is MySql and not SQL. So you need to get connected to MySql. For this you need to download and installed the MySQL Connector/NET from the MySQL official website.

Then probably you can look at Connect C# to MySQL to see how to get connected with MySQL Database and run the different Insert, Update, Select, Delete commands using C#

Last but not the least you have to include OnClick="Button1_Click1" with your asp:Button

Upvotes: 1

Jaydip Jadhav
Jaydip Jadhav

Reputation: 12309

Missing button click event definition in aspx

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

Upvotes: 8

Related Questions