Hi!
Can anybody tell me what I'm missing here?
I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:
"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."
//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalH ost;database=Us ers;uid=geir;pw d=geir");
//Open database connection
con.Open();
//Create variables to hold values from textboxes
string strUn = txtUsername.Tex t;
string strPw = txtPassword.Tex t;
//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;
try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}
Can anybody tell me what I'm missing here?
I'm trying to insert a new record into a sql-database from code at
runtime (not stored procedure),
but get the following error message:
"The 'strUn' is not permitted in this context.
Only constants, expressions or variables allowed here.
Column names are not permitted."
//Create sql connection
SqlConnection con = new SqlConnection
("server=LocalH ost;database=Us ers;uid=geir;pw d=geir");
//Open database connection
con.Open();
//Create variables to hold values from textboxes
string strUn = txtUsername.Tex t;
string strPw = txtPassword.Tex t;
//Create a sqlCommand to insert textbox values into sql-database
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandT ext = "INSERT INTO tblUsers(Userna me,Password)
VALUES(strUn, strPw)";
sqlcmd.Connecti on = con;
try
{
sqlcmd.ExecuteN onQuery();
}
catch(SqlExcept ion ex)
{
lblInfo.Text = "ExecuteNonQuer y failed because: \n" +
"\n" +
ex.Message;
}
finally
{
con.Close();
}
Comment