0

Hello Every One I am new To C# and SQL can any one tell me where am I wrong. It is not inserting . And not giving any errors.

 SqlConnection myConnection = new SqlConnection("server=SHASHAK\\SQLEXPRESS;" +
                             "Trusted_Connection=yes;" +
                             "database=Abhishek; " +
                             "connection timeout=30");

private void btnShow_Click(object sender, EventArgs e)
{

    try
    {
        //myConnection.Open();
       //// SqlCommand cmd = new SqlCommand("select player_id, player_name , score from player", myConnection);
        SqlCommand cmd = new SqlCommand("INSERT INTO Abhishek (" + "  player_id, player_name " + ") VALUES (" + " @textBox1.Text, @textBox2.Text", myConnection);
       SqlDataAdapter adapter = new SqlDataAdapter(cmd);
       MessageBox.Show("Ok");

        //DataTable dt = new DataTable();
        //adapter.Fill(dt);
        //screen.DataSource = dt;



    }
    catch (SqlException ex)
    {
        MessageBox.Show("You failed!" + ex.Message);
    }
}
6
  • 5
    you aren't executing the command, and you aren't setting your parameter values. You commented out where you open the connection, which is pretty important Commented Apr 17, 2014 at 18:13
  • 1
    And when you do execute the command you are missing a ) however you really really should be using Parametrized queries Commented Apr 17, 2014 at 18:15
  • ExecuteNonQuery Commented Apr 17, 2014 at 18:15
  • Why VS not showing any error? Commented Apr 17, 2014 at 18:17
  • You are never executing the query, hence no error... Commented Apr 17, 2014 at 18:19

2 Answers 2

4

Try This:

myConnection.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Abhishek (player_id, player_name)     
             VALUES(@playerid, @playername)", myConnection);
cmd.Parameters.AddWithValue("@playerid",textBox1.Text);
cmd.Parameters.AddWithValue("@playername",textBox2.Text);
int commandStatus = cmd.ExecuteNonQuery();

if(commandStatus > 0)
      MessageBox.Show("Row inserted Successfully!");
else
      MessageBox.Show("Row Insertion Failed!");
Sign up to request clarification or add additional context in comments.

5 Comments

its giving error "You failed " syntax error @player"
@AbhishekSharma: sorry i missed to specify the exact parameter names , please check my edit now
giving error " must declare scaler variable @playerid"
again an error near "int commandStatus = cmd.ExecuteNonQuery();"
SQl unhandled "Incorrect syntax near '@playername'.
3

Few issues with your code.

  • You are trying to parameterized query but you are not passing any parameter to command
  • You need to use using statement with your connection and command object.
  • catch System.Exception after SqlException to catch any other exception.
  • You are not executing your command.

Your code could be on the following lines:

SqlConnection myConnection = new SqlConnection("server=SHASHAK\\SQLEXPRESS;" +
                   "Trusted_Connection=yes;" +
                   "database=Abhishek; " +
                   "connection timeout=30");
private void btnShow_Click(object sender, EventArgs e)
{
    try
    {
        using (myConnection)
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO player (player_id, player_name) VALUES (@playerid, @playername)", myConnection))
            {
                cmd.Parameters.AddWithValue("@playerid", textBox1.Text);
                cmd.Parameters.AddWithValue("@playername", textBox2.Text);
                myConnection.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }
    catch (SqlException ex)
    {
        MessageBox.Show("You failed!" + ex.Message);
    }
    catch (Exception ex)
    {
        //Show message / log
    }
}

Consider using names which reflect their values. e.g. txtPlayerID and txtPlayerName for textBox1 and textBox2 respectively.

9 Comments

int.Parse is not strictly nessesary, Sql will attempt to convert it for you when it performs the insert. Just be aware that it might not use your indexes in some special conditions depending on which variable (yours or the column you are doing something to) gets automatically cast.
@Habib Its giving me an error .. Invalid Object Abhishek
@AbhishekSharma, what is your table name ? is it same as your Database name ?
@AbhishekSharma, modified my code, your code in question had INSERT INTO Abhishek, it should be INSERT INTO PLAYER. Try the edited code in answer.
myConnection.Open(); SqlCommand cmd = new SqlCommand("select player_id, player_name , score from player", myConnection); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); adapter.Fill(dt); screen.DataSource = dt;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.