I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error "
The name 'Peter' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
Where Peter is the value entered in the textbox for firstname (fnameTBox)
I'm sure the problem is something obvious but I can't seem to resolve and it is really frustrating. I would really appreciate help. I know that there is a connection to database as the regid is updated on form.
Thanks
This is the code I'm using
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Data.Sql Client;
using System.Drawing;
using System.Web;
using System.Web.Mobi le;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.M obileControls;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Threadin g;
namespace MobileSpeech
{
/// <summary>
/// Summary description for register.
/// </summary>
public class ruser : System.Web.UI.M obileControls.M obilePage
{
//Declaration
protected System.Web.UI.M obileControls.T extBox fnameTBox;
protected System.Web.UI.M obileControls.L abel firstname;
protected System.Web.UI.M obileControls.T extBox snameTBox;
protected System.Web.UI.M obileControls.L abel sname;
protected System.Web.UI.M obileControls.L abel email;
protected System.Web.UI.M obileControls.T extBox emailTBox;
protected System.Web.UI.M obileControls.L abel mobno;
protected System.Web.UI.M obileControls.T extBox mobnoTBox;
protected System.Web.UI.M obileControls.L abel uname;
protected System.Web.UI.M obileControls.T extBox unameTBox;
protected System.Web.UI.M obileControls.L abel pwd;
protected System.Web.UI.M obileControls.T extBox pwdTBox;
protected System.Web.UI.M obileControls.T extBox courseTBox;
protected System.Web.UI.M obileControls.L abel coursen;
protected System.Web.UI.M obileControls.T extBox regidtb;
protected System.Web.UI.M obileControls.C ommand savebtn;
protected System.Web.UI.M obileControls.C ommand cancelbtn;
protected System.Data.Sql Client.SqlDataA dapter sqlDataAdapter1 ;
protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
protected System.Data.Sql Client.SqlConne ction sqlConnection1;
protected System.Web.UI.M obileControls.F orm Form1;
protected System.Web.UI.M obileControls.F orm RegUForm;
// method to connect to database and update regid
private void getConnected()
{
try
{
string strConnString = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=\"(l" +
"ocal)\";persis t security info=False;init ial catalog=MobileQ uiz";
SqlConnection myConnection = new SqlConnection(s trConnString);
String strConnString1 = "select regid from Regusers";
SqlCommand myCommand =new SqlCommand(strC onnString1,myCo nnection);
//open database
myConnection.Op en();
//Creating data reader variable
SqlDataReader r = myCommand.Execu teReader();
int i=0;
while (r.Read())
{
i++ ;
}
i++ ;
regidtb.Text = i.ToString();
}
catch (SqlException sql)
{
//Error message
this.Response.W rite(sql.Messag e);
}
}
private void Page_Load(objec t sender, System.EventArg s e)
{
//put the connection to the database on a Thread so the Form displays quickly..
ThreadStart tsgc = new ThreadStart(get Connected) ;
Thread tgc = new Thread(tsgc) ;
tgc.Start() ;
//calls getconnected method
getConnected();
//calls initializeCompo nent method
InitializeCompo nent();
//called when save but is clicked
this.savebtn.Cl ick += new System.EventHan dler(this.saveb tn_Click);
//called when Cancel button is clicked
this.cancelbtn. Click += new System.EventHan dler(this.cance lbtn_Click);
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.sqlDataAda pter1 = new System.Data.Sql Client.SqlDataA dapter();
this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
this.sqlConnect ion1 = new System.Data.Sql Client.SqlConne ction();
this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
//
// sqlDataAdapter1
//
this.sqlDataAda pter1.InsertCom mand = this.sqlInsertC ommand1;
this.sqlDataAda pter1.SelectCom mand = this.sqlSelectC ommand1;
this.sqlDataAda pter1.TableMapp ings.AddRange(n ew System.Data.Com mon.DataTableMa pping[] {
new System.Data.Com mon.DataTableMa pping("Table", "Regusers", new System.Data.Com mon.DataColumnM apping[] {
new System.Data.Com mon.DataColumnM apping("passwor d", "password") ,
new System.Data.Com mon.DataColumnM apping("coursei d", "courseid")})}) ;
this.sqlDataAda pter1.UpdateCom mand = this.sqlUpdateC ommand1;
//
// sqlInsertComman d1
//
this.sqlInsertC ommand1.Command Text = @"INSERT INTO Regusers(firstn ame, surname, email, mobno, login, password, courseid) VALUES (@firstname, @surname, @email, @mobno, @login, @password, @courseid); SELECT regid, firstname, surname, email, mobno, login, password, courseid FROM Regusers WHERE (regid = @@IDENTITY)";
this.sqlInsertC ommand1.Connect ion = this.sqlConnect ion1;
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@firstnam e", System.Data.Sql DbType.VarChar, 10, "firstname" ));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@surname" , System.Data.Sql DbType.VarChar, 15, "surname")) ;
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@email", System.Data.Sql DbType.VarChar, 30, "email"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@mobno", System.Data.Sql DbType.NVarChar , 10, "mobno"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@login", System.Data.Sql DbType.VarChar, 10, "login"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@password ", System.Data.Sql DbType.VarChar, 10, "password") );
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@courseid ", System.Data.Sql DbType.Int, 4, "courseid") );
//
// sqlConnection1
//
this.sqlConnect ion1.Connection String = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=loc" +
"alhost;per sist security info=False;init ial catalog=MobileQ uiz";
//
// sqlSelectComman d1
//
this.sqlSelectC ommand1.Command Text = "SELECT regid, firstname, surname, email, mobno, login, password, courseid FROM Re" +
"gusers";
this.sqlSelectC ommand1.Connect ion = this.sqlConnect ion1;
this.Form1.Acti vate += new System.EventHan dler(this.Form1 _Activate);
this.Load += new System.EventHan dler(this.Page_ Load);
}
//This method is called when the "Save" Button is Clicked.
//It checks if Data is entered into all the feilds, if 'yes' then it procedes
//by opening an connection to the database and inserting the new data in it.
protected void savebtn_Click(o bject sender, System.EventArg s e)
{
try
{
if(regidtb.Text !=" "&&fnameTBox.Te xt!=" "&&snameTBox.Te xt!=" "&&emailTBox.Te xt!=" "&&mobnoTBox.Te xt!=" "&&unameTBox.Te xt!=" "&&pwdTBox.Text !=" "&&courseTBox.T ext!=" ")
{
string strConnString = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=\"(l" +
"ocal)\";persis t security info=False;init ial catalog=MobileQ uiz";
SqlConnection myConnection = new SqlConnection(s trConnString);
String strConnString1 = "SELECT regid from Regusers";
SqlCommand myCommand =new SqlCommand(strC onnString1,myCo nnection);
//open database
myConnection.Op en();
string strInsert = "INSERT INTO regusers(regid, firstname, surname, email, mobno, login, password, courseid) VALUES ("+regidtb.Text +", "+fnameTBox.Tex t+", "+snameTBox.Tex t+", "+emailTBox.Tex t+", "+mobnoTBox.Tex t+", "+unameTBox.Tex t+", "+pwdTBox.Text+ ", "+courseTBox.Te xt+")";
SqlCommand inst = new SqlCommand(strI nsert, myConnection);
inst.ExecuteNon Query();
myConnection.Cl ose();
}
else
{
this.Response.W rite("ERROR");
}
}
catch(SqlExcept ion sql)
{
//Error message
this.Response.W rite(sql.Messag e);
}
}
protected void cancelbtn_Click (object sender, System.EventArg s e)
{
//reset all the textboxes
int i=int.Parse(reg idtb.Text);
i--;
regidtb.Text=i. ToString() ;
fnameTBox.Text= "" ;
snameTBox.Text= "" ;
emailTBox.Text= "" ;
mobnoTBox.Text= "" ;
unameTBox.Text= "" ;
pwdTBox.Text="" ;
courseTBox.Text ="" ;
}
private void Form1_Activate( object sender, System.EventArg s e)
{
}
}
}
The name 'Peter' is not permitted in this context. Only constants, expressions, or variables allowed here. Column names are not permitted.
Where Peter is the value entered in the textbox for firstname (fnameTBox)
I'm sure the problem is something obvious but I can't seem to resolve and it is really frustrating. I would really appreciate help. I know that there is a connection to database as the regid is updated on form.
Thanks
This is the code I'm using
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Data.Sql Client;
using System.Drawing;
using System.Web;
using System.Web.Mobi le;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.M obileControls;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Threadin g;
namespace MobileSpeech
{
/// <summary>
/// Summary description for register.
/// </summary>
public class ruser : System.Web.UI.M obileControls.M obilePage
{
//Declaration
protected System.Web.UI.M obileControls.T extBox fnameTBox;
protected System.Web.UI.M obileControls.L abel firstname;
protected System.Web.UI.M obileControls.T extBox snameTBox;
protected System.Web.UI.M obileControls.L abel sname;
protected System.Web.UI.M obileControls.L abel email;
protected System.Web.UI.M obileControls.T extBox emailTBox;
protected System.Web.UI.M obileControls.L abel mobno;
protected System.Web.UI.M obileControls.T extBox mobnoTBox;
protected System.Web.UI.M obileControls.L abel uname;
protected System.Web.UI.M obileControls.T extBox unameTBox;
protected System.Web.UI.M obileControls.L abel pwd;
protected System.Web.UI.M obileControls.T extBox pwdTBox;
protected System.Web.UI.M obileControls.T extBox courseTBox;
protected System.Web.UI.M obileControls.L abel coursen;
protected System.Web.UI.M obileControls.T extBox regidtb;
protected System.Web.UI.M obileControls.C ommand savebtn;
protected System.Web.UI.M obileControls.C ommand cancelbtn;
protected System.Data.Sql Client.SqlDataA dapter sqlDataAdapter1 ;
protected System.Data.Sql Client.SqlComma nd sqlSelectComman d1;
protected System.Data.Sql Client.SqlComma nd sqlInsertComman d1;
protected System.Data.Sql Client.SqlComma nd sqlUpdateComman d1;
protected System.Data.Sql Client.SqlConne ction sqlConnection1;
protected System.Web.UI.M obileControls.F orm Form1;
protected System.Web.UI.M obileControls.F orm RegUForm;
// method to connect to database and update regid
private void getConnected()
{
try
{
string strConnString = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=\"(l" +
"ocal)\";persis t security info=False;init ial catalog=MobileQ uiz";
SqlConnection myConnection = new SqlConnection(s trConnString);
String strConnString1 = "select regid from Regusers";
SqlCommand myCommand =new SqlCommand(strC onnString1,myCo nnection);
//open database
myConnection.Op en();
//Creating data reader variable
SqlDataReader r = myCommand.Execu teReader();
int i=0;
while (r.Read())
{
i++ ;
}
i++ ;
regidtb.Text = i.ToString();
}
catch (SqlException sql)
{
//Error message
this.Response.W rite(sql.Messag e);
}
}
private void Page_Load(objec t sender, System.EventArg s e)
{
//put the connection to the database on a Thread so the Form displays quickly..
ThreadStart tsgc = new ThreadStart(get Connected) ;
Thread tgc = new Thread(tsgc) ;
tgc.Start() ;
//calls getconnected method
getConnected();
//calls initializeCompo nent method
InitializeCompo nent();
//called when save but is clicked
this.savebtn.Cl ick += new System.EventHan dler(this.saveb tn_Click);
//called when Cancel button is clicked
this.cancelbtn. Click += new System.EventHan dler(this.cance lbtn_Click);
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}
private void InitializeCompo nent()
{
this.sqlDataAda pter1 = new System.Data.Sql Client.SqlDataA dapter();
this.sqlInsertC ommand1 = new System.Data.Sql Client.SqlComma nd();
this.sqlConnect ion1 = new System.Data.Sql Client.SqlConne ction();
this.sqlSelectC ommand1 = new System.Data.Sql Client.SqlComma nd();
this.sqlUpdateC ommand1 = new System.Data.Sql Client.SqlComma nd();
//
// sqlDataAdapter1
//
this.sqlDataAda pter1.InsertCom mand = this.sqlInsertC ommand1;
this.sqlDataAda pter1.SelectCom mand = this.sqlSelectC ommand1;
this.sqlDataAda pter1.TableMapp ings.AddRange(n ew System.Data.Com mon.DataTableMa pping[] {
new System.Data.Com mon.DataTableMa pping("Table", "Regusers", new System.Data.Com mon.DataColumnM apping[] {
new System.Data.Com mon.DataColumnM apping("passwor d", "password") ,
new System.Data.Com mon.DataColumnM apping("coursei d", "courseid")})}) ;
this.sqlDataAda pter1.UpdateCom mand = this.sqlUpdateC ommand1;
//
// sqlInsertComman d1
//
this.sqlInsertC ommand1.Command Text = @"INSERT INTO Regusers(firstn ame, surname, email, mobno, login, password, courseid) VALUES (@firstname, @surname, @email, @mobno, @login, @password, @courseid); SELECT regid, firstname, surname, email, mobno, login, password, courseid FROM Regusers WHERE (regid = @@IDENTITY)";
this.sqlInsertC ommand1.Connect ion = this.sqlConnect ion1;
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@firstnam e", System.Data.Sql DbType.VarChar, 10, "firstname" ));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@surname" , System.Data.Sql DbType.VarChar, 15, "surname")) ;
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@email", System.Data.Sql DbType.VarChar, 30, "email"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@mobno", System.Data.Sql DbType.NVarChar , 10, "mobno"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@login", System.Data.Sql DbType.VarChar, 10, "login"));
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@password ", System.Data.Sql DbType.VarChar, 10, "password") );
this.sqlInsertC ommand1.Paramet ers.Add(new System.Data.Sql Client.SqlParam eter("@courseid ", System.Data.Sql DbType.Int, 4, "courseid") );
//
// sqlConnection1
//
this.sqlConnect ion1.Connection String = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=loc" +
"alhost;per sist security info=False;init ial catalog=MobileQ uiz";
//
// sqlSelectComman d1
//
this.sqlSelectC ommand1.Command Text = "SELECT regid, firstname, surname, email, mobno, login, password, courseid FROM Re" +
"gusers";
this.sqlSelectC ommand1.Connect ion = this.sqlConnect ion1;
this.Form1.Acti vate += new System.EventHan dler(this.Form1 _Activate);
this.Load += new System.EventHan dler(this.Page_ Load);
}
//This method is called when the "Save" Button is Clicked.
//It checks if Data is entered into all the feilds, if 'yes' then it procedes
//by opening an connection to the database and inserting the new data in it.
protected void savebtn_Click(o bject sender, System.EventArg s e)
{
try
{
if(regidtb.Text !=" "&&fnameTBox.Te xt!=" "&&snameTBox.Te xt!=" "&&emailTBox.Te xt!=" "&&mobnoTBox.Te xt!=" "&&unameTBox.Te xt!=" "&&pwdTBox.Text !=" "&&courseTBox.T ext!=" ")
{
string strConnString = "workstatio n id=PATRICIA;pac ket size=4096;integ rated security=SSPI;d ata source=\"(l" +
"ocal)\";persis t security info=False;init ial catalog=MobileQ uiz";
SqlConnection myConnection = new SqlConnection(s trConnString);
String strConnString1 = "SELECT regid from Regusers";
SqlCommand myCommand =new SqlCommand(strC onnString1,myCo nnection);
//open database
myConnection.Op en();
string strInsert = "INSERT INTO regusers(regid, firstname, surname, email, mobno, login, password, courseid) VALUES ("+regidtb.Text +", "+fnameTBox.Tex t+", "+snameTBox.Tex t+", "+emailTBox.Tex t+", "+mobnoTBox.Tex t+", "+unameTBox.Tex t+", "+pwdTBox.Text+ ", "+courseTBox.Te xt+")";
SqlCommand inst = new SqlCommand(strI nsert, myConnection);
inst.ExecuteNon Query();
myConnection.Cl ose();
}
else
{
this.Response.W rite("ERROR");
}
}
catch(SqlExcept ion sql)
{
//Error message
this.Response.W rite(sql.Messag e);
}
}
protected void cancelbtn_Click (object sender, System.EventArg s e)
{
//reset all the textboxes
int i=int.Parse(reg idtb.Text);
i--;
regidtb.Text=i. ToString() ;
fnameTBox.Text= "" ;
snameTBox.Text= "" ;
emailTBox.Text= "" ;
mobnoTBox.Text= "" ;
unameTBox.Text= "" ;
pwdTBox.Text="" ;
courseTBox.Text ="" ;
}
private void Form1_Activate( object sender, System.EventArg s e)
{
}
}
}