Results 1 to 7 of 7

Thread: [RESOLVED] Update Statement

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Resolved [RESOLVED] Update Statement

    hi everyone. I am trying to execute an update statement. I have tried what seems like 20 different ways and nothing seems to be working. Can someone please tell me what i am doing wrong.

    int Employeenum = int.Parse(this.txtEmployeeNum.Text);
    string BranchName = this.cboBranch.Text;

    string sql = "update branch set num_employees = " + Employeenum + " where branch_name = '" + BranchName + "'";
    SqlCommand cmd = new SqlCommand(sql, conn);
    try
    {
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    cmd.Connection.Close();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }


    After that happens i get no update on the table and no errors.

    Last edited by Hack; Feb 14th, 2007 at 09:13 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: Update Statement

    Make sure that you even have a match in that WHERE statement in the table. It is possible you aren't hitting true on that conditional at all.
    Currently Using: VS 2005 Professional

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Update Statement

    ExecuteNonQuery is a function that returns an integer containing the number of records affected by the operation. What does ExecuteNonQuery return? If it's zero then there are no matching records. If it's greater than zero then the operation is succeeding and that number of records were updated.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Update Statement

    Thanks everyone,

    I have pulled the sql statement from my command and ran it directly against the database. It updates and completes successfully with no problems.

    I also tried the following

    int x = cmd.ExecuteNonQuery();

    I stepped through and noticed that one row was changed. However, the transaction was never committed to the database. I am confused right now.

    any help would be greatly appreciated

    thanks,

    notrosh

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Update Statement

    I also changed the statement to include wild card characters in the where clause and still no action.

    thanks,

    notrosh

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Update Statement

    How exactly are you determining that no data was changed?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    122

    Re: Update Statement

    i have actually fixed the issue. it was very weird. I changed the connection string to a general connection string. Where before i was pulling the connection string form the app.config file. Seems to work fine now. However, to answer the previous question. I knew the data wasn't changing because of two reasons. One i manually checked the database for changes and two when i restarted the application it showed no changes.

    thanks for all the help everyone

    notrosh


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width