Having issue inserting data into my database title "Uni-Hub.mdf" currently pulling my hair out!
The error message:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Incorrect syntax near ')'.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.
Source Error:
Line 114: dbCommand.Connection = conn;
Line 115: conn.Open();
Line 116: dbCommand.ExecuteNonQuery(); Line 117: //lblTest.Text = sql1;
Line 118: }
My code is as follows:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
public string strConn;
public void Page_Load(object sender, EventArgs e)
{
//lblHeader.Text = "Please enter your details:";
protected void Submit_Click(object sender, EventArgs e)
{
string errFlag = "allClear";
if (TextBoxFN.Text == "")
{
lblErrFname.Text = "Please enter your first name.";
errFlag = "error";
}
if (TextBoxSN.Text == "")
{
lblErrLname.Text = "Please enter your last name.";
errFlag = "error";
}
if (TextBoxUC.Text == "")
{
lblErrUC.Text = "Please enter your University.";
errFlag = "error";
}
if (TextBoxM.Text == "")
{
lblErrMobile.Text = "Please enter your Mobile.";
errFlag = "error";
}
if (TextBoxEmail.Text == "")
{
lblErrEmail.Text = "Please enter your email address.";
errFlag = "error";
}
// Regex using regular expressions - notice the need for namespace - System.Text.RegularExpressions;
Regex theEmailValidator = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3} \.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)");
Match emailMatch = theEmailValidator.Match(TextBoxEmail.Text);
if (!emailMatch.Success)
{
lblErrEmail.Text += " This is not a valid email address";
errFlag = "error";
}
// End of regex email check
// call method to insert into tCustomer if no errors
if (errFlag == "allClear")
{
insertRecord();
// Proceed to capturing confirming order details and pay
Response.Redirect("Uni-Hub.aspx");
}
// if errFlag is "error" then processing remains on the customer page with error messages
}
protected void insertRecord()
{
//Build connection object //
SqlConnection conn;
conn = new SqlConnection();
strConn += "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|UniHub1.mdf;User Instance=true";
conn.ConnectionString = strConn;
pdavis0000
Member
3 Points
7 Posts
General Insert issue
May 08, 2012 06:01 PM|LINK
Hi guys,
Having issue inserting data into my database title "Uni-Hub.mdf" currently pulling my hair out!
The error message:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Incorrect syntax near ')'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ')'.
Source Error:
Line 114: dbCommand.Connection = conn; Line 115: conn.Open(); Line 116: dbCommand.ExecuteNonQuery(); Line 117: //lblTest.Text = sql1; Line 118: }My code is as follows:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
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.Text.RegularExpressions;
public partial class _Default : System.Web.UI.Page
{
public string strConn;
public void Page_Load(object sender, EventArgs e)
{
//lblHeader.Text = "Please enter your details:";
if (IsPostBack)
{
lblErrFname.Text = null;
lblErrLname.Text = null;
lblErrUC.Text = null;
lblErrMobile.Text = null;
lblErrMusic.Text = null;
lblErrInterst.Text = null;
lblErrEmail.Text = null;
lblErrMailings.Text = null;
}
}
protected void Submit_Click(object sender, EventArgs e)
{
string errFlag = "allClear";
if (TextBoxFN.Text == "")
{
lblErrFname.Text = "Please enter your first name.";
errFlag = "error";
}
if (TextBoxSN.Text == "")
{
lblErrLname.Text = "Please enter your last name.";
errFlag = "error";
}
if (TextBoxUC.Text == "")
{
lblErrUC.Text = "Please enter your University.";
errFlag = "error";
}
if (TextBoxM.Text == "")
{
lblErrMobile.Text = "Please enter your Mobile.";
errFlag = "error";
}
if (TextBoxEmail.Text == "")
{
lblErrEmail.Text = "Please enter your email address.";
errFlag = "error";
}
// Regex using regular expressions - notice the need for namespace - System.Text.RegularExpressions;
Regex theEmailValidator = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3} \.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)");
Match emailMatch = theEmailValidator.Match(TextBoxEmail.Text);
if (!emailMatch.Success)
{
lblErrEmail.Text += " This is not a valid email address";
errFlag = "error";
}
// End of regex email check
// call method to insert into tCustomer if no errors
if (errFlag == "allClear")
{
insertRecord();
// Proceed to capturing confirming order details and pay
Response.Redirect("Uni-Hub.aspx");
}
// if errFlag is "error" then processing remains on the customer page with error messages
}
protected void insertRecord()
{
//Build connection object //
SqlConnection conn;
conn = new SqlConnection();
strConn += "data source=.\\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|UniHub1.mdf;User Instance=true";
conn.ConnectionString = strConn;
string sql1;
sql1 = "insert into tMember (First, Last, Email, Uni, Mobile, Music, MailingList, ";
sql1 += ")";
sql1 += " values(";
sql1 += "'" + TextBoxFN.Text + "',";
sql1 += "'" + TextBoxSN.Text + "',";
sql1 += "'" + TextBoxEmail.Text + "',";
sql1 += "'" + TextBoxUC.Text + "',";
sql1 += "'" + TextBoxM.Text + "',";
sql1 += "'" + DDLMusicType.SelectedItem.Value + "',";
sql1 += "'" + DDLInterest.SelectedItem.Value + "',";
sql1 += MailingList.SelectedItem.Value;
sql1 += ")";
SqlCommand dbCommand = new SqlCommand();
dbCommand.CommandText = sql1;
dbCommand.Connection = conn;
conn.Open();
dbCommand.ExecuteNonQuery();
//lblTest.Text = sql1;
}
}
Please please help! :)
ignatandrei
All-Star
135172 Points
21681 Posts
Moderator
MVP
Re: General Insert issue
May 08, 2012 09:12 PM|LINK
delete the
,
after
MailingList
pdavis0000
Member
3 Points
7 Posts
Re: General Insert issue
May 09, 2012 06:34 PM|LINK
Thank you, all sorted now.