One query updates, the other doesn't

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbrewerton
    New Member
    • Nov 2009
    • 115

    One query updates, the other doesn't

    Ok, I'm using code-behind with a MySQL DB to update two tables. It updates the addressbook table fine but the device table won't update. I know its probably some stupid syntax error but I am not seeing it. To be sure I have the right variables set, I am using labels for the IDs. The ID numbers are correct. Can someone help? Thanks.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.OleDb;
    using System.Data.Odbc;
    
    public partial class editassign : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["aspnet"] = Request.QueryString[0];
    
            int counting = GridView1.Rows.Count;
            if (counting == 0)
            {
                GridView1.Visible = false;
                Wizard1.Visible = true;
            }
            else
            {
                GridView1.Visible = true;
                Wizard1.Visible = false;
            }
        }
    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["contact_id"] = this.DropDownList1.SelectedItem.Value;
        }
    
        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["device_id"] = this.DropDownList2.SelectedItem.Value;
            Label1.Text = Convert.ToString(Session["aspnet"]);
            Label2.Text = Convert.ToString(Session["contact_id"]);
            Label3.Text = Convert.ToString(Session["device_id"]);
        }
    
        protected void Wizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            OdbcConnection con = null;
            OdbcCommand cmd = null;
    
            try
            {
                con = new OdbcConnection();
                con.ConnectionString = "Driver={MySQL ODBC 5.1 Driver};server=MyDBServer;database=MyDB;uid=MyUser;pwd=MyPass;";
                cmd = new OdbcCommand();
                cmd.Connection = con;
                cmd.Parameters.Add("?aspnet_id", OdbcType.Int).Value = (Session["aspnet"] != null) ? Convert.ToInt32(Session["aspnet"]) : 1;
                cmd.Parameters.Add("?contact_id", OdbcType.Int).Value = (Session["contact_id"] != null) ? Convert.ToInt32(Session["contact_id"]) : 0;
                con.Open();
                // This one works fine
                cmd.CommandText = "update egw_addressbook set aspnet_id = ? where contact_id = ?;";
                cmd.ExecuteNonQuery();
    
                // These two don't. Even tried combining, no better.
                cmd.Parameters.Add("?aspnet_id", OdbcType.Int).Value = (Session["aspnet"] != null) ? Convert.ToInt32(Session["aspnet"]) : 1;
                cmd.Parameters.Add("?device_id", OdbcType.Int).Value = (Session["device_id"] != null) ? Convert.ToInt32(Session["device_id"]) : 0;
                cmd.CommandText = "update ewise_device set aspnet_id = ? where device_id = ?;";
                cmd.ExecuteNonQuery();
    
                cmd.Parameters.Add("?contact_id", OdbcType.Int).Value = (Session["contact_id"] != null) ? Convert.ToInt32(Session["contact_id"]) : 1;
                cmd.Parameters.Add("?device_id", OdbcType.Int).Value = (Session["device_id"] != null) ? Convert.ToInt32(Session["device_id"]) : 0;
                cmd.CommandText = "update ewise_device set contact_id = ? where device_id = ?;";
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                con.Close();
            }
        }
    }
    Last edited by tlhintoq; Nov 30 '09, 07:38 PM. Reason: [CODE] ...your code here... [/CODE] tags added
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • dbrewerton
      New Member
      • Nov 2009
      • 115

      #3
      Never mind...I got it

      I was overloading wizard finish by trying to execute all three queries.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm glad you solved your problem.
        Next time it would be helpful if you posted the error messages that you are getting. Sometimes it's easier for the experts here to work with error messages rather than having to comb through code to find the problem.

        Cheers!

        -Frinny

        Comment

        • dbrewerton
          New Member
          • Nov 2009
          • 115

          #5
          Agreed, sometimes that slips my mind LOL

          Comment

          Working...