我在此代码中遇到类型为'System.NullReferenceException'的未处理异常:
Try
conn.Open()
Dim Query As String
If txtNewTeleNo.Text.ToString.Equals("") And txtNewUsername.Text.ToString.Equals("") And txtNewPassword.Text.ToString.Equals("") Then
MessageBox.Show("If you wish to make changes, please enter a new value in the textbox of the data you want to edit")
End If
If txtUsername3.Text.ToString.Equals("") And txtPassword3.Text.ToString.Equals("") Then
MessageBox.Show("You Have Not Entered an Account's Username or Password")
ElseIf txtNewEmail.Text.ToString.Equals("") Then
Query = "UPDATE tblLogin SET EmailAddress=@newEmailAddress WHERE Username=@username AND Password=@password"
cmd.Parameters.Add(New MySqlParameter("@emailAddress", DBNull.Value)) *error usually happens here
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET Username=@newUsername WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newUsername", txtNewUsername.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET Password=@newPassword WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newPassword", txtNewPassword.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET TelephoneNo=@newTelephoneNo WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newTelephoneNo", txtNewTeleNo.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
MessageBox.Show("Changes Have Been Made")
Me.Hide()
LogIn.Show()
Else
Query = "UPDATE tblLogin SET EmailAddress=@newEmailAddress WHERE Username=@username AND Password=@password"
cmd.Parameters.Add(New MySqlParameter("@emailAddress", txtNewEmail.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET Username=@newUsername WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newUsername", txtNewUsername.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET Password=@newPassword WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newPassword", txtNewPassword.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
Query = "UPDATE tbllogin SET TelephoneNo=@newTelephoneNo WHERE Username=@username AND Password=@password"
cmd = New MySqlCommand(Query, conn)
cmd.Parameters.Add(New MySqlParameter("@newTelephoneNo", txtNewTeleNo.Text))
cmd.Parameters.Add(New MySqlParameter("@username", txtUsername3.Text))
cmd.Parameters.Add(New MySqlParameter("@password", txtPassword3.Text))
cmd.ExecuteNonQuery()
MessageBox.Show("Changes Have Been Made")
Me.Hide()
LogIn.Show()
End If
Catch ex As MySqlException
MessageBox.Show(ex.GetBaseException.ToString)
Finally
conn.Dispose()
End Try
conn.Close()
txtPassword3.Clear()
txtUsername3.Clear()
txtNewPassword.Clear()
txtNewUsername.Clear()
txtNewTeleNo.Clear()
txtNewEmail.Clear()
这是我在visual studio上的当前代码。我尝试过其他方法,但都没有。一种方法略有作用,但当我尝试更改其中一个细节时,MySQL中的用户名和密码保持不变,但其他数据已被删除。
答案 0 :(得分:0)
' System.NullReferenceException' 是由尚未实例化(活动)的对象引起的。当您开始尝试添加参数时,尚未实例化您的对象 cmd 。你需要移动这一行:
cmd = New MySqlCommand(Query, conn)
在引用 cmd 的部分的顶部,然后您就可以添加参数了。我建议在这些方面之后把它放好:
Query = "..."