I tried to insert data in DB using 3 tire architecture in c# .NET but getting the following error.
Error:
Server Error in '/3tweb' Application.
Incorrect syntax near the keyword 'User'.
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 the keyword 'User'.
Source Error:
Line 25: objconn.Open();
Line 26: SqlCommand objcmd = new SqlCommand(sqlstring, objconn);
Line 27: objcmd.ExecuteNonQuery();
Line 28: }
Line 29: public DataSet LoadCustomerDB()
Source File: C:\ASP project\3tweb\DataLayer\Class1.cs Line: 27
My DataLayer file is given below.
Class1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace DataLayer
{
public class clsDalLayer
{
SqlConnection objconn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
private DataSet ExecuteSql(string sqlcmd)
{
DataSet ds = new DataSet();
objconn.Open();
SqlCommand objcmd = new SqlCommand(sqlcmd, objconn);
SqlDataAdapter objadp = new SqlDataAdapter(objcmd);
objadp.Fill(ds);
objconn.Close();
return ds;
}
private void InsertUpdateDeleteSQLString(string sqlstring)
{
objconn.Open();
SqlCommand objcmd = new SqlCommand(sqlstring, objconn);
objcmd.ExecuteNonQuery();
}
public DataSet LoadCustomerDB()
{
DataSet ds = new DataSet();
string sql = "SELECT id,Name,Username,Age from Person ";
sql +="order by id DESC ";
ds = ExecuteSql(sql);
return ds;
}
public void AddNewUser(string username, string userpass)
{
DataSet ds = new DataSet();
string sql = "INSERT into User (username,password) VALUES ('" + username + "','" + userpass + "')";
InsertUpdateDeleteSQLString(sql);
}
}
}
Here my story is i want to insert data into DB from view.AddNewUser
method is called from business layer with two value.When the sql query is executing i am getting this type of error.So please help me to resolve this error.