Strange Problem with ExecuteNonQuery

I have made a database in SQL Server 2005 Express (Default with VS 2005). And I am making a Desktop Application Program in C#.
[code]
private void btnEnroll_Click(object sender, EventArgs e)
{

SqlConnection mySqlConnection = new SqlConnection(Properties.Settings.Default.TestDBConnectionString);
mySqlConnection.Open();
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText = "INSERT INTO enroll(id,testString) VALUES( @id,@testString)";

mySqlCommand.Parameters.Add("@id", SqlDbType.NChar, 10);
mySqlCommand.Parameters.Add("@testString", SqlDbType.VarChar, 50);
mySqlCommand.Parameters["@id"].Value = 1;
mySqlCommand.Parameters["@testString"].Value = "J4 Company";
mySqlCommand.ExecuteNonQuery();
MessageBox.Show("Successfully added row to Customers table");
mySqlConnection.Close();
}

[/code]

But it doesnot insert it into DB. It doesn't give any exception also.



Comments

  • Try this ...

    private void btnEnroll_Click(object sender, EventArgs e)
    {

    SqlConnection mySqlConnection = new SqlConnection(Properties.Settings.Default.TestDBConnectionString);
    mySqlConnection.Open();
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
    mySqlCommand.CommandText = "INSERT INTO enroll(id,testString) VALUES( @id,@testString)";

    //mySqlCommand.Parameters.Add("@id", SqlDbType.NChar, 10);
    //mySqlCommand.Parameters.Add("@testString",SqlDbType.VarChar, 50);
    // mySqlCommand.Parameters["@id"].Value = 1;
    //mySqlCommand.Parameters["@testString"].Value = "J4 Company";

    mySqlCommand.Parameters.AddWith("@id","1");
    mySqlCommand.Parameters.AddWith("@testString","j4 Company");
    mySqlCommand.ExecuteNonQuery();
    MessageBox.Show("Successfully added row to Customers table");
    mySqlConnection.Close();
    }

    Tks in Adv ...

    G.Sam....

  • If it interests you, I have a set of database controls in C# to do all the dirty work for you see http://www.c-sharplizards.com

    These controls will generate the sql statements you need for insert, update and delete based on the text, combo boxes you have on your form.

    You assign a group name to the controls which make a db record and the control agent on the form takes care of the rest.

    connecting to databases is simplified using a sever and sql connection component, there is even a login component which discovers all sql servers on your system or network

    regards
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories