I am trying to make a basic page in ASP.Net by inserting into a MSSQL database. When I run the page it does not insert anything and no error is produced :(
The connection string name is right as well as the table name and fields. This is my code:

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

public partial class _Default : System.Web.UI.Page
{
    SqlConnection connection;

    protected void Page_Load(object sender, EventArgs e)
    {
        connection = new SqlConnection(ConfigurationManager.ConnectionStrings["PaintingConnection"].ConnectionString);
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlCommand command = new SqlCommand("INSERT INTO PaintingTable(paintingAuthor, paintingPrice, paintingName) VALUES (@id_paintingAuthor, @id_paintingPrice, @id_paintingName)", connection);

        //insert author
        SqlParameter authorContent = new SqlParameter("@id_paintingAuthor", SqlDbType.NVarChar);
        authorContent.Value = txtAuthor.Text;
        command.Parameters.Add(authorContent);

        //insert price
        SqlParameter priceContent = new SqlParameter("@id_paintingPrice", SqlDbType.NVarChar);
        priceContent.Value = txtPrice.Text;
        command.Parameters.Add(priceContent);

        //insert painting name
        SqlParameter nameContent = new SqlParameter("@id_paintingName", SqlDbType.NVarChar);
        nameContent.Value = txtName.Text;
        command.Parameters.Add(nameContent);

        /*insert image
        SqlParameter imageContent = new SqlParameter("@id_paintingImage", SqlDbType.VarChar);
        imageContent.Value = imageUpload.
        command.Parameters.Add(imageContent);*/

        connection.Open();
        command.BeginExecuteNonQuery();
        connection.Close();

        Response.Redirect("Results.aspx");
    }
}

Thank You :D

Try chaning command.BeginExecuteNonQuery() to command.ExecuteNonQuery()

That seemed to work. Thank You!
What is the differnece between the two? Why did ExecuteNonQuery work but BeginExecuteNonQuery not work?

BeginExecuteNonQuery is for async operations and requires an EndExecuteNonQuery call for it to 'finish' the command. From the documentation:

The BeginExecuteNonQuery method starts the process of asynchronously executing a Transact-SQL statement or stored procedure that does not return rows, so that other tasks can run concurrently while the statement is executing. When the statement has completed, developers must call the EndExecuteNonQuery method to finish the operation. The BeginExecuteNonQuery method returns immediately (CommandTimeout has no effect on BeginExecuteNonQuery), but until the code executes the corresponding EndExecuteNonQuery method call, it must not execute any other calls that start a synchronous or asynchronous execution against the same SqlCommand object. Calling the EndExecuteNonQuery before the command's execution is completed causes the SqlCommand object to block until the execution is finished.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.