Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
why doesnt this work?
C#
 private void button1_Click(object sender, EventArgs e)
        
{
                string ConnectionString = @"Data Source=PHIL-PC\SQLEXPRESS;Initial Catalog=Sudoku;Integrated Security=True";
                
                SqlConnection conn = new SqlConnection(ConnectionString);
                conn.Open();
                string namesub = nameTextBox.Text;
                string emailsub= emailTextBox.Text;
                string SQL1 = "insert into player(Player_name,Player_email) values (" + "'" + namesub + "'" + "," + emailsub + " )";
                SqlCommand cmd1 = new SqlCommand(SQL1, conn);             
                cmd1.ExecuteNonQuery();
                this.player1TableAdapter.Fill(this.sudokuDataSet.player1);
                conn.Close();


sorry im not very knowledgeable in programming, but ill try explain things better. the thing i want done is that the values put in by the user from the nameTextBox and emailTextbox (in the visual studio form) to be inserted into the "player" table in the sql database. i am using this template from a previous example. the error that comes up is associated with cmd1.ExecuteNonQuery(); line it says "The name "asdfafs" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted. "
Posted
Updated 12-Jun-12 22:01pm
v5
Comments
Sebastian T Xavier 13-Jun-12 3:47am    
post your error message

There are quite a few reasons why this may not work: firstly, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. This may cure your problem without additional work.
string ConnectionString = @"Data Source=PHIL-PC\SQLEXPRESS;Initial Catalog=Sudoku;Integrated Security=True";

C#
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
string namesub = nameTextBox.Text;
string emailsub= nameTextBox.Text;
string SQL1 = "insert into player(Player_name,Player_email) values (@NM, @EM)";
SqlCommand cmd1 = new SqlCommand(SQL1, conn); 
cmd1.Parameters.AddWithValue("@NM", namesub);
cmd1.Parameters.AddWithValue("@EM", emailsub);
cmd1.ExecuteNonQuery();

But it would be worth checking if you really wanted the name in both the name and email fields...
 
Share this answer
 
v2
I think you have made a mistake in
"insert into player(Player_name,Player_email) values (" + "'" + namesub + "'" + "," + emailsub + " )"
statement.

if you see this line carefully, then you seee, the comma is missing from the "emailsub".

put like
C#
string SQL1 = "insert into player(Player_name,Player_email) values (" + "'" + namesub + "'" + ",'" + emailsub + "' )";



Thanks,
Nilesh
 
Share this answer
 
Comments
stib_markc 13-Jun-12 4:28am    
5!
Vani Kulkarni 13-Jun-12 4:55am    
Good one!
What does the following code?
C#
this.player1TableAdapter.Fill(this.sudokuDataSet.player1);

Whats the error message you are getting? (without that we can't help you)
If you are not receiving any error message, please use proper exception handling.
I suspect the issue is with connection string. Have you missed the password?

Best Regards
Sebastian
 
Share this answer
 
v3
Try to do a step by step Debug,

First try to put user and pass in connection string, just in case.
then if you have SQL management studio installed try to copy the query manually.
if thats ok to then check if all objects are instantiated

And maybe after inserting do a dataset refresh just in case
 
Share this answer
 
forgot to insert a value into a third text box which waas the primary key..and the syntax in the insert part.
thanks for help everyone..i can submit my assignment on time now
correct result
string ConnectionString = @"Data Source=PHIL-PC\SQLEXPRESS;Initial Catalog=Sudoku;Integrated Security=True";
SqlConnection conn = new SqlConnection(ConnectionString);
conn.Open();
string namesub = nameTextBox.Text;
string emailsub= emailTextBox.Text;
string idsub = player_idTextBox.Text;
string SQL1 = "insert into player(player_id,Player_name,Player_email) values (" + "'" + idsub + "'" + "," + "'" + namesub + "'" + "," + "'" + emailsub + "'" + ")";
SqlCommand cmd1 = new SqlCommand(SQL1, conn);
cmd1.ExecuteNonQuery();
this.player1TableAdapter.Fill(this.sudokuDataSet.player1);
conn.Close();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900