VB.NET'空引用未处理'并且在分配之前已使用Variable

时间:2015-11-29 11:26:08

标签: mysql vb.net

我收到一个空引用错误和一条警告,告诉我在分配之前已经使用了一个变量'Query'。代码可以工作,但在消息框'请选择大学'后点击确定错误发生。我试图做的是当用户管理员登录系统时,他可以免于选择大学,但当其他用户要登录时,他们需要选择他们来自哪所大学。

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click, ComboBox1.DropDownStyleChanged
    mydbcon = New MySqlConnection
    mydbcon.ConnectionString = "server=localhost;userid=jared;password=jared;database=database"
    Dim reader As MySqlDataReader

    Try
        mydbcon.Open()
        Dim Query As String
        If TextBox1UN.Text = "admin" Then
            Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim())
        ElseIf (String.IsNullOrEmpty(ComboBox1.SelectedItem)) Then
            MessageBox.Show("Please Select a college")
        ElseIf Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}' and College = '{2}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim, Me.ComboBox1.SelectedItem.Trim()) Then


            COMMAND = New MySqlCommand(Query, mydbcon)
        End If
        reader = COMMAND.ExecuteReader

        Dim count As Integer = 0
        While reader.Read       < --- Null reference occur
            count = count + 1

        End While

        If TextBox1UN.Text = "admin" And count = 1 Then
            Form2.Show()
            TextBox1UN.Clear()
            TextBox2Pass.Clear()
            ComboBox1.ResetText()

            Me.Hide()
        ElseIf count = 1 Then
            Form2.Show()
            Form2.Button4.Hide()
            TextBox1UN.Clear()
            TextBox2Pass.Clear()
            ComboBox1.ResetText()
            Me.Hide()

        Else
            MessageBox.Show("Incorrect! Please Try Again.")
        End If
        mydbcon.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        mydbcon.Dispose()
    End Try

End Sub
End Class

2 个答案:

答案 0 :(得分:0)

您声明了一个变量:

Dim Query As String

然后,在为任何值分配之前,您尝试在比较中使用它:

ElseIf Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}' and College = '{2}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim, Me.ComboBox1.SelectedItem.Trim()) Then

显然,尚未分配的变量不会等于该值。也许你只是想成为一个Else块?:

Else
  Query = String.Format("SELECT * FROM logininfo WHERE Username = '{0}' AND Password = '{1}' and College = '{2}'", Me.TextBox1UN.Text.Trim(), Me.TextBox2Pass.Text.Trim, Me.ComboBox1.SelectedItem.Trim())

此外,由于这发生在条件块中:

COMMAND = New MySqlCommand(Query, mydbcon)

然后任何时候不满足条件(如上所示,永远不会满足),那么就不会执行任何实际的数据库命令。所以没有有效的数据阅读器可供使用。

基本上,看起来If/Else结构有点搞砸了。现在还不完全清楚应该如何在你的逻辑中构建,但你绝对不想比较未分配的变量,也不想有条件地执行你的查询然后假设它总是被执行

答案 1 :(得分:0)

  1. 确保您的数据库查询有效。
  2. 当“admin”但“大学”=“”然后
  3.   

    myApp.controller('PhoneListCtrl', ['$rootScope', '$http', '$scope', function ($scope, $http, $rootScope) {
    
        if(navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function(position){
                    $scope.$apply(function(){
                    $scope.position = position;
              $rootScope.position = position;
            });
            console.log(position);
            $rootScope.latitude = position.coords.latitude;
            $rootScope.longititude = position.coords.longititude;
            console.log(position.coords.latitude);
            console.log(position.coords.longititude);
            });
    
        }
    
    }]);
    myApp.directive("myMaps", ['$rootScope', function($scope, $rootScope){
      console.log( $rootScope.position.coords.latitude + " rootscope ");
      return{
        restrict:'E',
        template:'<div></div>',
        replace: true,
        controller: function($scope, $element,$rootScope) {
        },
        link: function(scope, element, attrs){
          console.log('{{position.coords.latitude}}');
          var myLatLng = new google.maps.LatLng(56, -75.732333);
          var myLatLng2 = new google.maps.LatLng(56.2843691, -70.732333);
          var mapOptions = {
            center: myLatLng,
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          };
          var map = new google.maps.Map(document.getElementById(attrs.id), mapOptions);
          var marker = new google.maps.Marker({
            position: myLatLng, 
            map: map, 
            title: "hello world!"
          });
          var marker2 = new google.maps.Marker({
            position: myLatLng2, 
            map: map, 
            title: "hello"
          });
          marker.setMap(map);
          marker2.setMap(map);
        }
      };
    } ]);
    

    所以当“college =”“”然后出现消息框,当按OK确保查询运行良好时,将“ElseIf”放在“大学”查询下面并添加另一个