2011-10-27 77 views
0

以下是我在我的項目中創建帳戶的代碼。代碼工作完美,直到Response.Write(iduser)但UPDATE命令不起作用。沒有發現使用異常的錯誤,但MySQL的記錄沒有更新。奇怪! MySQL更新無法正常工作

try 
       { 

        string pet1 = "[email protected]"; 
        string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=newtest;" + "UID=root;" + "PASSWORD=**********;" + "OPTION=3"; 
        OdbcConnection MyConnection = new OdbcConnection(MyConString); 
        OdbcCommand cmd = new OdbcCommand("Select id_user from awm_accounts where email=?", MyConnection); 
        cmd.Parameters.Add("@val1", OdbcType.VarChar, 255).Value = pet1; 
        MyConnection.Open(); 
        OdbcDataReader dr = cmd.ExecuteReader(); 
        if (dr.HasRows == false) 
        { 
         throw new Exception(); 
        } 
        if (dr.Read()) 
        { 
         int iduser = Convert.ToInt32(dr[0].ToString()); 
         Account acct = new Account(); 
         acct.Email = username.Text + domain.Text; 
         acct.MailIncomingLogin = username.Text + domain.Text; 
         acct.MailIncomingHost = "imap." + DropDownList1.SelectedValue; 
         acct.MailIncomingPassword = password.Text; 
         acct.MailIncomingPort = 993; 
         acct.MailOutgoingHost = "smtp." + DropDownList1.SelectedValue; 
         acct.MailOutgoingPort = 465; 
         acct.MailIncomingProtocol = IncomingMailProtocol.Imap4; 
         acct.MailOutgoingAuthentication = true; 
         acct.DefaultAccount = false; 
         acct.IDUser = 1; 
         integr.CreateUserFromAccount(acct); 
         Response.Write(iduser); 
         if (!IsPostBack) 
         { 
          cmd = new OdbcCommand("UPDATE awm_accounts SET id_user=? WHERE email=? ", MyConnection); 
          cmd.Parameters.Add("@tb_nickname", OdbcType.Int, 11).Value = iduser; 
          cmd.Parameters.Add("@tb_fullname", OdbcType.VarChar, 255).Value = username.Text + domain.Text; 
cmd.ExecuteNonQuery(); 
         } 
        } 
        MyConnection.Close(); 
       } 
      catch (Exception exp) 
      { 
       Response.Write(exp); 
      } 
+0

你是否在使用UpdatePanel?如果是這樣,請刪除它,直到您修復了錯誤。 –

+0

不。我在這段代碼中沒有使用UpdatePanel。 –

回答

2

我不知道有關Response.Write一部分,但我敢肯定,你創建一個SQLCommand,但從來沒有真正執行。

你忘了把cmd.executeNonScalar()放在那裏嗎?

+0

是啊!你寫了。剛纔我發現我缺少'cmd.executenonquery'我添加了它,代碼工作正常。謝謝。 –