473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 2985

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 you have done this before and how this is...
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 Dim dc(0) As DataColumn Try If...
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)) cmdTest.Parameters.Add(New SqlParameter("@LastN",...
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 . But when I try to insert Data into the SQL I just...
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 i am inserting some data from a form,by post...
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 register on the website, they fill out a form and the...
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 problem with when using form...
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 for a band, I got these tables for their discography...
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 section where the Admin can INSERT new records in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.