mysql - VB.NET function return error and exit sub -


i have connection function in vb.net project

public function openmysqlcon() mysqlconnection     dim mmysqlconnection = new mysqlconnection()     try         dim strcondb string = "server='192.168.100.2'; database='mydb';port=3306; uid='epb'; password='hahaha'; pooling=true"         mmysqlconnection = new mysqlconnection         mmysqlconnection.connectionstring = strcondb         mmysqlconnection.open()         openmysqlcon = mmysqlconnection     catch exceptionthaticaught system.exception         openmysqlcon = nothing     end try end function 

and call function in vb project like

private sub frmtest_load(sender object, e eventargs) handles mybase.load     using con=openmysqlcon()         'mycode here     end using end sub 

however when connection not available, throw exception.

how can avoid exception giving msgbox connection not available @ moment, please try again later , exit sub using function?

end sub

the best approach not hold connection in field of form create , initalize wherever need it. connection-pooling ensure no physical connection needs created.

so don't use openmysqlcon method @ code this(getallusers example):

public function getallusers() list(of user)     dim userlist = new list(of user)      try         using mmysqlconnection = new mysqlconnection("server='192.168.100.2'; database='mydb';port=3306; uid='epb'; password='hahaha'; pooling=true")             mmysqlconnection.open()             using command = new mysqlcommand("select * user order username", mmysqlconnection)                 using rdr = command.executereader()                     while rdr.read()                         dim user = new user()                         user.username = rdr.getstring(0)                         userlist.add(user)                     end while                 end using             end using         end using     catch exceptionthaticaught system.exception         messagebox.show("meaningful message here, logging useful too")         return nothing     end try      return userlist end function 

maybe helpful because related(you're reusing connection): executereader requires open , available connection. connection's current state connecting


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -