472,353 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Inserting data from web form not working?

2
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.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Mobile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;


namespace MobileSpeech
{
/// <summary>
/// Summary description for register.
/// </summary>
public class ruser : System.Web.UI.MobileControls.MobilePage
{

//Declaration
protected System.Web.UI.MobileControls.TextBox fnameTBox;
protected System.Web.UI.MobileControls.Label firstname;
protected System.Web.UI.MobileControls.TextBox snameTBox;
protected System.Web.UI.MobileControls.Label sname;
protected System.Web.UI.MobileControls.Label email;
protected System.Web.UI.MobileControls.TextBox emailTBox;
protected System.Web.UI.MobileControls.Label mobno;
protected System.Web.UI.MobileControls.TextBox mobnoTBox;
protected System.Web.UI.MobileControls.Label uname;
protected System.Web.UI.MobileControls.TextBox unameTBox;
protected System.Web.UI.MobileControls.Label pwd;
protected System.Web.UI.MobileControls.TextBox pwdTBox;
protected System.Web.UI.MobileControls.TextBox courseTBox;
protected System.Web.UI.MobileControls.Label coursen;
protected System.Web.UI.MobileControls.TextBox regidtb;
protected System.Web.UI.MobileControls.Command savebtn;
protected System.Web.UI.MobileControls.Command cancelbtn;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.MobileControls.Form Form1;
protected System.Web.UI.MobileControls.Form RegUForm;


// method to connect to database and update regid
private void getConnected()
{
try
{
string strConnString = "workstation id=PATRICIA;packet size=4096;integrated security=SSPI;data source=\"(l" +
"ocal)\";persist security info=False;initial catalog=MobileQuiz";
SqlConnection myConnection = new SqlConnection(strConnString);

String strConnString1 = "select regid from Regusers";

SqlCommand myCommand =new SqlCommand(strConnString1,myConnection);

//open database
myConnection.Open();

//Creating data reader variable
SqlDataReader r = myCommand.ExecuteReader();

int i=0;

while (r.Read())
{
i++ ;
}
i++ ;
regidtb.Text = i.ToString();


}

catch (SqlException sql)
{
//Error message
this.Response.Write(sql.Message);
}

}

private void Page_Load(object sender, System.EventArgs e)
{
//put the connection to the database on a Thread so the Form displays quickly..
ThreadStart tsgc = new ThreadStart(getConnected) ;
Thread tgc = new Thread(tsgc) ;
tgc.Start() ;

//calls getconnected method
getConnected();

//calls initializeComponent method
InitializeComponent();

//called when save but is clicked
this.savebtn.Click += new System.EventHandler(this.savebtn_Click);

//called when Cancel button is clicked
this.cancelbtn.Click += new System.EventHandler(this.cancelbtn_Click);


}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Regusers", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("password", "password"),
new System.Data.Common.DataColumnMapping("courseid", "courseid")})});
this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = @"INSERT INTO Regusers(firstname, 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.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@firstname", System.Data.SqlDbType.VarChar, 10, "firstname"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@surname", System.Data.SqlDbType.VarChar, 15, "surname"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@email", System.Data.SqlDbType.VarChar, 30, "email"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@mobno", System.Data.SqlDbType.NVarChar, 10, "mobno"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@login", System.Data.SqlDbType.VarChar, 10, "login"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@password", System.Data.SqlDbType.VarChar, 10, "password"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@courseid", System.Data.SqlDbType.Int, 4, "courseid"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=PATRICIA;packet size=4096;integrated security=SSPI;data source=loc" +
"alhost;persist security info=False;initial catalog=MobileQuiz";
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT regid, firstname, surname, email, mobno, login, password, courseid FROM Re" +
"gusers";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.Form1.Activate += new System.EventHandler(this.Form1_Activate);
this.Load += new System.EventHandler(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(object sender, System.EventArgs e)
{

try
{

if(regidtb.Text!=" "&&fnameTBox.Text!=" "&&snameTBox.Text!=" "&&emailTBox.Text!=" "&&mobnoTBox.Text!=" "&&unameTBox.Text!=" "&&pwdTBox.Text!=" "&&courseTBox.Text!=" ")
{

string strConnString = "workstation id=PATRICIA;packet size=4096;integrated security=SSPI;data source=\"(l" +
"ocal)\";persist security info=False;initial catalog=MobileQuiz";

SqlConnection myConnection = new SqlConnection(strConnString);

String strConnString1 = "SELECT regid from Regusers";

SqlCommand myCommand =new SqlCommand(strConnString1,myConnection);

//open database
myConnection.Open();

string strInsert = "INSERT INTO regusers(regid, firstname, surname, email, mobno, login, password, courseid) VALUES ("+regidtb.Text+", "+fnameTBox.Text+", "+snameTBox.Text+", "+emailTBox.Text+", "+mobnoTBox.Text+", "+unameTBox.Text+", "+pwdTBox.Text+", "+courseTBox.Text+")";

SqlCommand inst = new SqlCommand(strInsert, myConnection);

inst.ExecuteNonQuery();


myConnection.Close();
}

else
{
this.Response.Write("ERROR");
}

}

catch(SqlException sql)
{
//Error message
this.Response.Write(sql.Message);
}
}





protected void cancelbtn_Click(object sender, System.EventArgs e)
{

//reset all the textboxes
int i=int.Parse(regidtb.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.EventArgs e)
{

}
}
}
May 1 '06 #1
0 2899

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Ria Van Heerden | last post by:
How do I insert data from one form into two tables in a database? I am working with ASP pages and a MSSQL database Please let me know if one of...
5
by: aniket_sp | last post by:
i am using a data adapter and a dataset for filling and retrieving data into .mdb database. following is the code..... for the form load event...
3
by: hfk0 | last post by:
Hi, I'm using the following parameters in my web form: cmdTest.Parameters.Add(New SqlParameter("@FirstN", SqlDbType.NVarChar, 25))...
25
by: bseakgano | last post by:
I have developed a intranet . Using HTML , SQL and ASP . I have created a table with SQL is just fine . And design a form is just looks fine to me ....
2
by: Ravigandha | last post by:
Hello everybody, My question is how to insert special characters and symbols in Mysql5 database and how to retrieve them from database in php. Here...
6
by: ashes | last post by:
Hi, I am creating an ecommerce website using Microsoft Visual Studio, VB.Net and MS Access 2003. I am new to VB.Net When someone wants to...
18
by: boss1 | last post by:
Hi all, i m having a problem with inserting data in oracle db. When i use form action =(call self page) then data is inserting properly.But...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.