简体   繁体   中英

ASP.net - VB NET how to row count for SQL query

Hi folks I am very new to ASP.net and trying to build what I thought was a simple redirect page. I have an IIS basic authentication setup already and upon a successful logon user reach my default.aspx page below. I have a database where I created a copy of user's logon and created them a group_id. What I wanted to do is upon a successful logon users will reach the default page below and check if the user is part of the group_ID 1001 and the answer is yes to redirect them to URL1 and if not redirect them to URL2.

Ive been getting help alot of help from people on this site and giving me examples on different coding language thinking I could easily translate them but keep getting stuck at different steps. Now I am getting the below error and been pulling my hair trying to understand the logic on this. Looking to get some advice from the VB Net experts out there. Would really appreciate help but stuck on this for days now lol.

updated code - March 1ST

%@ Page Language="VB" debug="true" %>
<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "MySql.Data.MySqlClient" %>
<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
    Dim username As String = Convert.ToString(User.Identity.Name.Substring(User.Identity.Name.IndexOf("\") + 1))
    Dim myConnection  As MySqlConnection
    Dim myDataAdapter As MySqlDataAdapter
        Dim strSQL As String
        Dim mySqlCommand As MySqlCommand
        Dim counter As Integer
        Dim isInGroup As Boolean

        myConnection = New MySqlConnection("server=localhost; user id=Directory_Admin; password=IMCisgreat2014; database=imc_directory_tool; pooling=false;")
        strSQL = "SELECT COUNT(*) FROM tbl_staff WHERE username = @username AND 'group_id' = '1001';"
        myDataAdapter = New MySqlDataAdapter(strSQL, myConnection)

        mySqlCommand = New MySqlCommand(strSQL)
        mySqlCommand.Parameters.AddWithValue("@username", username)
        counter = mySqlCommand.ExecuteScalar()
        myConnection.Open()
        If isInGroup = counter > 0 Then
            Response.Redirect("http://www.w3schools.com")
        Else
            Response.Redirect("http://www.google.ca")
        End If
        myConnection.Close()
    End Sub

</script>

<html>
<head>
<title>Simple MySQL Database Query</title>
</head>
<body>

Main page ...

</body>
</html> 

I am getting the following error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Connection must be valid and open.

Line 18:             mySqlCommand = New MySqlCommand(strSQL)
Line 19:             mySqlCommand.Parameters.AddWithValue("@username", username)
Line 20:             counter = mySqlCommand.ExecuteScalar()
Line 21:             If isInGroup = counter > 0 Then
Line 22:                 Response.Redirect("http://www.w3schools.com")

You created a MySqlConnection but never connected your MySqlCommand to it.

mySqlCommand.Connection = mySqlConnection
mySqlCommand.Connection.Open()

Also, after your call to ExecuteScalar don't forget to close the connection with

mySqlCommand.Connection.Close()

or else you will have a connection that is left open and dangling.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM