0

我想弄清楚在哪里添加我的Response.Redirect(“userAdmin.aspx)到我的代码。我已经尝试了很多不同的变体,但提交按钮什么都不做。谁能帮助?我将不胜感激!在哪里可以添加Respons.Redirect?

这里是我的代码

Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click 

    Dim myReader As Data.SqlClient.SqlDataReader 
    Dim mySqlConnection As Data.SqlClient.SqlConnection 
    Dim mySqlCommand As Data.SqlClient.SqlCommand 
    'Establish the SqlConnection by using the configuration manager to get the connection string in our web.config file. 

    mySqlConnection = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings ("ConnectionString").ToString()) 
    Dim sql As String = "SELECT password FROM MyUsers WHERE username = '" & Me.TextBox1.Text & "'" 
    mySqlCommand = New Data.SqlClient.SqlCommand(sql, mySqlConnection) 



    Try 

     mySqlConnection.Open() 
     myReader = mySqlCommand.ExecuteReader() 

     If (myReader.HasRows) Then 
      myReader.Read() 
      Dim password As String = myReader("password") 
      If (password = Me.TextBox2.Text) Then 
       'Open page with users and roles 
       Dim message As String = "Correct password" 
       Dim style As MsgBoxStyle = MsgBoxStyle.OkOnly 
       Dim title As String = "Authenticated" 
       MsgBox(message, style, title) 

      End If 
     End If 

    Catch ex As Exception 
     Console.WriteLine(ex.ToString()) 
    Finally 
     If Not (myReader Is Nothing) Then 
      myReader.Close() 
     End If 

     If (mySqlConnection.State = Data.ConnectionState.Open) Then 
      mySqlConnection.Close() 
     End If 

    End Try 

End Sub 
+2

asp.net应用程序中的MsgBox和控制台? – Aristos 2013-03-01 08:12:38

回答

0
protected void Button_Click(object sender,ClickEventArgs e) 
    { 
     Response.Redirect("userAdmin.aspx) ; 
    } 

弗里斯特尝试一下本作另一个按钮,看看页面被重定向或不..如果你没有蚂蚁问题然后根据您的标准最后或最后放置

2

您应该在Try-Catch之外使用Response.Redirect,否则您将获得ThreadAbortException。您也可以使用过载Response.Redirect(url, false)

您应该为使用参数 for your sql-command to prevent sql-injection!

Protected Sub butSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSubmit.Click 
    Dim correctPassword As Boolean = False 
    Using mySqlConnection = New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString) 
     Dim sql As String = "SELECT password FROM MyUsers WHERE username = @userName" 
     Using mySqlCommand = New Data.SqlClient.SqlCommand(sql, mySqlConnection) 
      mySqlCommand.Parameters.AddWithValue("@userName", Me.TextBox1.Text) 
      Try 
       mySqlConnection.Open() 
       Using myReader = mySqlCommand.ExecuteReader() 
        If myReader.Read() Then 
         Dim password As String = myReader.GetString(myReader.GetOrdinal("password")) 
         If password = Me.TextBox2.Text Then 
          correctPassword = True 
         End If 
        End If 
       End Using 
      Catch ex As Exception 
       Console.WriteLine(ex.ToString()) 
      End Try 
     End Using 
    End Using 
    If correctPassword Then 
     Response.Redirect("userAdmin.aspx") 
    End If 
End Sub 

我也强烈建议使用ASP.NET-Membership来代替。

+0

谢谢你提供的东西,但它仍然不会导致新的页面,当我输入用户名,“mcobery”和密码,“密码并点击提交看看你自己的这个链接。 ://comweb.uml.edu/spepp18614/project1/Default.aspx – user1690599 2013-03-01 08:28:57

+0

你提供的代码看起来更合乎逻辑,但是我为我的错误感到抱歉,我对此很陌生。 – user1690599 2013-03-01 08:29:40

+0

使用调试器来看看会发生什么。只需在方法中设置一个断点,然后按F10进入下一行检查所有变量值(fe'correctPassword')。 – 2013-03-01 08:30:44

相关问题