I am fairly new to ASP.NET and I am trying to save the data of my web form into the database. I have tested the database table connection with the Aspx file and it loads the data of the table(manually entered data) into the table correctly in the browser.
However, when I fill up the form and press Save, I get this error:Screenshot of error message
This is my code of aspx.cs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
Response.Write("Database connection successful");
conn.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("Database connection successful");
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table (Name, Address, Phone, Email, Source, Message) values (@name, @address, @phone, @email, @source, @message");
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("@name", Name.Text);
com.Parameters.AddWithValue("@address", Address.Text);
com.Parameters.AddWithValue("@phone", Phone.Text);
com.Parameters.AddWithValue("@email", Email.Text);
com.Parameters.AddWithValue("@source", Source.SelectedItem.ToString());
com.Parameters.AddWithValue("@message", "text");
com.ExecuteNonQuery();
Response.Write("Data is saved");
conn.Close();
}
catch (Exception ex)
{
Response.Write("Error:" + ex.ToString());
}
}
}
My error says this:
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource
1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at _Default.Button1_Click(Object sender, EventArgs e) in D:\documents\visual studio 2015\WebSites\Enquiry Tracking Sys\Default.aspx.cs:line 45 ClientConnectionId:6c142f6c-40d7-4977-b75d-8fb0d774b0c6 Error Number:156,State:1,Class:15