vb.net - ExecuteReader: Connection property has not been initialized -


can explain why getting error?

executereader: connection property has not been initialized

protected sub btnlogin_click(byval sender object,                 byval e system.eventargs) handles btnlogin.click     dim name = txtusername.text.trim     dim pass = txtpassword.text.trim      try          dim conn sqlconnection         conn = databasefunc.openconnection()          dim sqlst string = "select * adminlog adminname = '" & name & "' , adminpassword = '" & pass & "'"         dim selcmd sqlcommand = new sqlcommand(sqlst, conn)          dim rrecset sqldatareader          rrecset = selcmd.executereader()          if rrecset.read()             session("admin") = rrecset("adminname")             response.redirect("~/adminpages/adminhome.aspx")         else             lblmsg.text = "<p>rong username or password</p>"             txtusername.text = ""             txtpassword.text = ""             txtusername.focus()          end if     catch ex exception         lblmsg.text = ex.message     end try end sub 

databasefunc

imports system imports system.data imports system.data.sqlclient imports microsoft.visualbasic   public class databasefunc     public shared sub ensureopenconnection(byref conn sqlconnection)         try             if conn.state <> data.connectionstate.open                 conn.open()             end if         catch ex exception         end try     end sub       public shared function openconnection() sqlconnection         try             dim connstring string = "server =.\sqlexpress;attachdbfilename=|datadirectory|\blooddb.mdf;integrated security=true;user instance=true"             dim conn new sqlconnection(connstring)             if conn.state <> data.connectionstate.open                 conn.open()             end if             return conn         catch ex exception             return nothing         end try     end function  end class 

change databasefunc code this, , msgbox might give better understanding of error.
having try/catch no actual code inside (like msgbox(ex.tostring) or println(ex.tostring) useless. after , try this, report further information.

public class databasefunc         public shared sub ensureopenconnection(byref conn sqlconnection)             try                 if conn.state <> data.connectionstate.open                     conn.open()                 end if             catch ex exception                msgbox(ex.tostring)             end try         end sub           public shared function openconnection() sqlconnection             try                 dim connstring string = "server =.\sqlexpress;attachdbfilename=|datadirectory|\blooddb.mdf;integrated security=true;user instance=true"                 dim conn new sqlconnection(connstring)                 if conn.state <> data.connectionstate.open                     conn.open()                 end if                 return conn             catch ex exception                 msgbox(ex.tostring)             end try         end function 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -