Howdy, Stranger!

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

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Strange Problem with ExecuteNonQuery

naveedanjumnaveedanjum Posts: 1Member
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

  • samcaleb05samcaleb05 Posts: 3Member
    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....

  • lenLasalalenLasala Posts: 5Member
    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.